Class: HighLine::ColorScheme

Inherits:
Object
  • Object
show all
Defined in:
lib/highline/color_scheme.rb

Overview

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

Direct Known Subclasses

SampleColorScheme

Instance Method Summary collapse

Constructor Details

#initialize(h = nil) {|_self| ... } ⇒ ColorScheme

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.

Yields:

  • (_self)

Yield Parameters:



53
54
55
56
57
# File 'lib/highline/color_scheme.rb', line 53

def initialize( h = nil )
  @scheme = Hash.new
  load_from_hash(h) unless h.nil?
  yield self if block_given?
end

Instance Method Details

#[](color_tag) ⇒ Object

Allow the scheme to be accessed like a Hash.



72
73
74
# File 'lib/highline/color_scheme.rb', line 72

def []( color_tag )
  @scheme[to_symbol(color_tag)]
end

#[]=(color_tag, constants) ⇒ Object

Allow the scheme to be set like a Hash.



77
78
79
# File 'lib/highline/color_scheme.rb', line 77

def []=( color_tag, constants )
  @scheme[to_symbol(color_tag)] = constants.map { |c| to_constant(c) }
end

#include?(color_tag) ⇒ Boolean

Does this color scheme include the given tag name?

Returns:

  • (Boolean)


67
68
69
# File 'lib/highline/color_scheme.rb', line 67

def include?( color_tag )
  @scheme.keys.include?(to_symbol(color_tag))
end

#load_from_hash(h) ⇒ Object

Load multiple colors from key/value pairs.



60
61
62
63
64
# File 'lib/highline/color_scheme.rb', line 60

def load_from_hash( h )
  h.each_pair do |color_tag, constants|
    self[color_tag] = constants
  end
end