| Class | HighLine::ColorScheme |
| In: |
lib/highline/color_scheme.rb
|
| Parent: | Object |
ColorScheme objects encapsulate a named set of colors to be used in the HighLine.colors() method call. For example, by applying a ColorScheme that has a :warning color then the following could be used:
colors("This is a warning", :warning)
A ColorScheme contains named sets of HighLine color constants.
Example: Instantiating a color scheme, applying it to HighLine,
and using it:
ft = HighLine::ColorScheme.new do |cs|
cs[:headline] = [ :bold, :yellow, :on_black ]
cs[:horizontal_line] = [ :bold, :white ]
cs[:even_row] = [ :green ]
cs[:odd_row] = [ :magenta ]
end
HighLine.color_scheme = ft
say("<%= color('Headline', :headline) %>")
say("<%= color('-'*20, :horizontal_line) %>")
i = true
("A".."D").each do |row|
if i then
say("<%= color('#{row}', :even_row ) %>")
else
say("<%= color('#{row}', :odd_row) %>")
end
i = !i
end
Create an instance of HighLine::ColorScheme. The customization can happen as a passed in Hash or via the yielded block. Key‘s are converted to :symbols and values are converted to HighLine constants.
# File lib/highline/color_scheme.rb, line 53
53: def initialize( h = nil )
54: @scheme = Hash.new
55: load_from_hash(h) unless h.nil?
56: yield self if block_given?
57: end
Allow the scheme to be accessed like a Hash.
# File lib/highline/color_scheme.rb, line 72
72: def []( color_tag )
73: @scheme[to_symbol(color_tag)]
74: end
Allow the scheme to be set like a Hash.
# File lib/highline/color_scheme.rb, line 77
77: def []=( color_tag, constants )
78: @scheme[to_symbol(color_tag)] = constants.map { |c| to_constant(c) }
79: end
Does this color scheme include the given tag name?
# File lib/highline/color_scheme.rb, line 67
67: def include?( color_tag )
68: @scheme.keys.include?(to_symbol(color_tag))
69: end
Load multiple colors from key/value pairs.
# File lib/highline/color_scheme.rb, line 60
60: def load_from_hash( h )
61: h.each_pair do |color_tag, constants|
62: self[color_tag] = constants
63: end
64: end
Return a normalized representation of a color setting.
# File lib/highline/color_scheme.rb, line 89
89: def to_constant( v )
90: v = v.to_s if v.is_a?(Symbol)
91: if v.is_a?(String) then
92: HighLine.const_get(v.upcase)
93: else
94: v
95: end
96: end