Class: Choosy::Printing::Color
- Inherits:
-
Object
- Object
- Choosy::Printing::Color
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
}
- FOREGROUND =
30
- BACKGROUND =
40
Instance Method Summary
collapse
Constructor Details
#initialize ⇒ Color
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
disable!
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Dynamically handle colors and effects
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/choosy/printing/color.rb', line 78
def method_missing(method, *args, &block)
if disabled?
return args[0] || ""
end
if color?(method)
raise ArgumentError.new("too many arguments to Color##{method} (max 2)") if args.length > 2
offset = find_state(method, args[1])
bedazzle(COLORS[method] + offset, args[0])
elsif effect?(method)
raise ArgumentError.new("too many arguments to Color##{method} (max 1)") if args.length > 1
bedazzle(EFFECTS[method], args[0])
else
raise NoMethodError.new("undefined method '#{method}' for Color")
end
end
|
Instance Method Details
#color?(col) ⇒ 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
43
44
45
|
# File 'lib/choosy/printing/color.rb', line 43
def disabled?
@disabled
end
|
#effect?(effect) ⇒ 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] + FOREGROUND, str, originally_nil)
elsif effect?(style)
str = bedazzle(EFFECTS[style], str, originally_nil)
end
end
str
end
|
#respond_to?(method) ⇒ Boolean
73
74
75
|
# File 'lib/choosy/printing/color.rb', line 73
def respond_to?(method)
color?(method) || effect?(method)
end
|