Class: CLI::UI::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/ui/color.rb

Defined Under Namespace

Classes: InvalidColorName

Constant Summary collapse

RED =
new('31', :red)
GREEN =
new('32', :green)
YELLOW =
new('33', :yellow)
BLUE =

default blue is low-contrast against black in some default terminal color scheme

new('94', :blue)
MAGENTA =
new('35', :magenta)
CYAN =
new('36', :cyan)
RESET =
new('0',  :reset)
BOLD =
new('1',  :bold)
WHITE =
new('97', :white)
MAP =
{
  red:     RED,
  green:   GREEN,
  yellow:  YELLOW,
  blue:    BLUE,
  magenta: MAGENTA,
  cyan:    CYAN,
  reset:   RESET,
  bold:    BOLD,
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sgr, name) ⇒ Color

Creates a new color mapping Signatures can be found here: en.wikipedia.org/wiki/ANSI_escape_code#Colors

Attributes

  • sgr - The color signature

  • name - The name of the color



17
18
19
20
21
# File 'lib/cli/ui/color.rb', line 17

def initialize(sgr, name)
  @sgr  = sgr
  @code = CLI::UI::ANSI.sgr(sgr)
  @name = name
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



6
7
8
# File 'lib/cli/ui/color.rb', line 6

def code
  @code
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/cli/ui/color.rb', line 6

def name
  @name
end

#sgrObject (readonly)

Returns the value of attribute sgr.



6
7
8
# File 'lib/cli/ui/color.rb', line 6

def sgr
  @sgr
end

Class Method Details

.availableObject

All available colors by name



74
75
76
# File 'lib/cli/ui/color.rb', line 74

def self.available
  MAP.keys
end

.lookup(name) ⇒ Object

Looks up a color code by name

Raises

Raises a InvalidColorName if the color is not available You likely need to add it to the MAP or you made a typo

Returns

Returns a color code



66
67
68
69
70
# File 'lib/cli/ui/color.rb', line 66

def self.lookup(name)
  MAP.fetch(name)
rescue KeyError
  raise InvalidColorName, name
end