Module: Pvectl::Formatters::ColorSupport

Included in:
Presenters::OperationResult, Presenters::SnapshotOperationResult
Defined in:
lib/pvectl/formatters/color_support.rb

Overview

Manages color output based on TTY detection and user flags.

Implements color flag priority:

  1. –no-color flag (highest) -> disabled

  2. –color flag -> enabled

  3. NO_COLOR env var -> disabled (see no-color.org/)

  4. TTY detection (lowest) -> $stdout.tty?

Examples:

Check if colors are enabled

ColorSupport.enabled?(explicit_flag: nil)   # TTY auto-detect
ColorSupport.enabled?(explicit_flag: true)  # forced on
ColorSupport.enabled?(explicit_flag: false) # forced off

Get Pastel instance

pastel = ColorSupport.pastel(explicit_flag: global_options[:color])
puts pastel.green("Success!")

Colorize status value

pastel = ColorSupport.pastel(explicit_flag: true)
ColorSupport.colorize_status("running", pastel) #=> "\e[32mrunning\e[0m"

Constant Summary collapse

STATUS_COLORS =

Status color mapping following kubectl conventions.

Returns:

  • (Hash<String, Symbol>)

    mapping of status to Pastel color method

{
  "running" => :green,
  "stopped" => :red,
  "paused" => :yellow
}.freeze

Class Method Summary collapse

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)

Parameters:

  • status (String, nil)

    status value

  • pastel_instance (Pastel)

    pastel instance

Returns:

  • (String)

    colored text (or “-” if nil, or dim if unknown status)



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:

  1. explicit_flag: false (–no-color) -> disabled

  2. explicit_flag: true (–color) -> enabled

  3. NO_COLOR env var present -> disabled

  4. TTY detection -> $stdout.tty?

Parameters:

  • explicit_flag (Boolean, nil) (defaults to: nil)

    value from –color / –no-color flag

    • true: –color was passed

    • false: –no-color was passed

    • nil: no flag passed, use auto-detection

Returns:

  • (Boolean)

    true if colors should be used



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.

Examples:

pastel = ColorSupport.pastel(explicit_flag: true)
pastel.green("text") #=> "\e[32mtext\e[0m"

Parameters:

  • explicit_flag (Boolean, nil) (defaults to: nil)

    value from –color / –no-color flag

Returns:

  • (Pastel)

    pastel instance with appropriate enabled state



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