Module: Command::Results::Formatter::Styler

Included in:
StrategyFormatter::FormatStrategy
Defined in:
lib/command-set/formatter/base.rb

Constant Summary collapse

Foregrounds =
{ 
    'black'   => 30,
    'red'     => 31, 
    'green'   => 32, 
    'yellow'  => 33,
    'blue'    => 34,
    'magenta' => 35,
    'cyan'    => 36,
    'white'   => 37
}
Backgrounds =
{}
Extras =
{
    'clear'     => 0, 
    'bold'      => 1,
    'underline' => 4,
    'reversed'  => 7
}

Instance Method Summary collapse

Instance Method Details

#code_for(kind, name) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/command-set/formatter/base.rb', line 51

def code_for(kind, name)
  if kind.has_key?(name.to_s)
      "\e[#{kind[name.to_s]}m"
  else
      ""
  end
end

#style(text, options) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/command-set/formatter/base.rb', line 34

def style(text, options)
  options ||= {}
  if options.key? :format_advice
    options = options.merge(options[:format_advice]) 
  end
  aliased = {
    :foreground => options[:color],
    :extra => options[:text_style]
  }
  options = aliased.merge(options)
  markup = code_for(Foregrounds, options[:foreground]) + 
    code_for(Backgrounds, options[:background]) + 
    code_for(Extras, options[:extra])
  return text if markup.empty?
  return markup + text + code_for(Extras, "clear")
end