Class: Choosy::Printing::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/choosy/printing/color.rb

Constant Summary collapse

COLORS =
{
  :black   => 0,
  :red     => 1,
  :green   => 2,
  :yellow  => 3,
  :blue    => 4,
  :magenta => 5, 
  :cyan    => 6,
  :white   => 7,
}
EFFECTS =
{
  :reset     => 0,
  :bright    => 1,
  :bold      => 1,
  :underline => 4,
  :blink     => 5,
  :exchange  => 7,
  :hide      => 8,
  :primary   => 10,
  :normal    => 22
}
KINDS =
{
  :foreground => 30,
  :background => 40
}

Instance Method Summary collapse

Constructor Details

#initializeColor

Returns a new instance of Color.



33
34
35
36
37
38
39
40
41
# File 'lib/choosy/printing/color.rb', line 33

def initialize
  begin
    require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/
    @disabled = false
  rescue LoadError
    # STDERR.puts "You must gem install win32console to use color on Windows"
    disable!
  end
end

Instance Method Details

#color?(col) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/choosy/printing/color.rb', line 51

def color?(col)
  COLORS.has_key?(col.to_sym)
end

#disable!Object



47
48
49
# File 'lib/choosy/printing/color.rb', line 47

def disable!
  @disabled = true
end

#disabled?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/choosy/printing/color.rb', line 43

def disabled?
  @disabled
end

#effect?(effect) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/choosy/printing/color.rb', line 55

def effect?(effect)
  EFFECTS.has_key?(effect.to_sym)
end

#multiple(str, styles) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/choosy/printing/color.rb', line 59

def multiple(str, styles)
  return str if styles.nil? || styles.empty? || disabled?

  originally_nil = str.nil?
  styles.each do |style|
    if color?(style)
      str = bedazzle(COLORS[style] + KINDS[:foreground], str, originally_nil)
    elsif effect?(style)
      str = bedazzle(EFFECTS[style], str, originally_nil)
    end
  end
  str
end