Module: Pvectl::Formatters::ColorSupport
- Defined in:
- lib/pvectl/formatters/color_support.rb
Overview
Manages color output based on TTY detection and user flags.
Implements color flag priority:
-
–no-color flag (highest) -> disabled
-
–color flag -> enabled
-
NO_COLOR env var -> disabled (see no-color.org/)
-
TTY detection (lowest) -> $stdout.tty?
Constant Summary collapse
- STATUS_COLORS =
Status color mapping following kubectl conventions.
{ "running" => :green, "stopped" => :red, "paused" => :yellow }.freeze
Class Method Summary collapse
-
.colorize_status(status, pastel_instance) ⇒ String
Colors status text according to kubectl conventions.
-
.enabled?(explicit_flag: nil) ⇒ Boolean
Determines if color output should be enabled.
-
.pastel(explicit_flag: nil) ⇒ Pastel
Returns a Pastel instance configured based on color settings.
Class Method Details
.colorize_status(status, pastel_instance) ⇒ String
Colors status text according to kubectl conventions.
Status colors:
-
running -> green
-
stopped -> red
-
paused -> yellow
-
unknown status -> dim (gray)
81 82 83 84 85 86 |
# File 'lib/pvectl/formatters/color_support.rb', line 81 def colorize_status(status, pastel_instance) return "-" if status.nil? color = STATUS_COLORS[status.to_s.downcase] color ? pastel_instance.public_send(color, status) : pastel_instance.dim(status) end |
.enabled?(explicit_flag: nil) ⇒ Boolean
Determines if color output should be enabled.
Priority order:
-
explicit_flag: false (–no-color) -> disabled
-
explicit_flag: true (–color) -> enabled
-
NO_COLOR env var present -> disabled
-
TTY detection -> $stdout.tty?
49 50 51 52 53 54 55 |
# File 'lib/pvectl/formatters/color_support.rb', line 49 def enabled?(explicit_flag: nil) return false if explicit_flag == false return true if explicit_flag == true return false if ENV.key?("NO_COLOR") $stdout.tty? end |
.pastel(explicit_flag: nil) ⇒ Pastel
Returns a Pastel instance configured based on color settings.
65 66 67 68 |
# File 'lib/pvectl/formatters/color_support.rb', line 65 def pastel(explicit_flag: nil) require "pastel" Pastel.new(enabled: enabled?(explicit_flag: explicit_flag)) end |