Module: Colored
Overview
cute.
>> "this is red".red
>> "this is red with a blue background (read: ugly)".red_on_blue
>> "this is red with an underline".red.underline
>> "this is really bold and really blue".bold.blue
>> Colored.red "This is red" # but this part is mostly untested
Constant Summary collapse
- COLORS =
{ 'black' => 30, 'red' => 31, 'green' => 32, 'yellow' => 33, 'blue' => 34, 'magenta' => 35, 'cyan' => 36, 'white' => 37 }
- EXTRAS =
{ 'clear' => 0, 'bold' => 1, 'light' => 1, 'underline' => 4, 'reversed' => 7 }
- BBS_COLOR_TABLE =
{ 0 => :black, 1 => :blue, 2 => :green, 3 => :cyan, 4 => :red, 5 => :magenta, 6 => :yellow, 7 => :white, 8 => :light_black, 9 => :light_blue, 10 => :light_green, 11 => :light_cyan, 12 => :light_red, 13 => :light_magenta, 14 => :light_yellow, 15 => :light_white, }
- VALID_COLORS =
Set.new( COLORS.keys + COLORS.map { |k,v| "light_#{k}" } + COLORS.map { |k,v| "bold_#{k}" } )
- @@is_tty =
STDOUT.isatty
Instance Method Summary collapse
- #color(color_name) ⇒ Object
- #colorize(string = nil, options = {}) ⇒ Object
- #colors ⇒ Object
- #disable! ⇒ Object
- #enable! ⇒ Object (also: #force!)
- #extra(extra_name) ⇒ Object
- #is_tty? ⇒ Boolean
- #tagged_colors(string) ⇒ Object
- #valid_tag?(tag) ⇒ Boolean
Instance Method Details
#color(color_name) ⇒ Object
133 134 135 136 137 138 |
# File 'lib/delicious-cli/colored.rb', line 133 def color(color_name) background = color_name.to_s =~ /on_/ color_name = color_name.to_s.sub('on_', '') return unless color_name && COLORS[color_name] "\e[#{COLORS[color_name] + (background ? 10 : 0)}m" end |
#colorize(string = nil, options = {}) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/delicious-cli/colored.rb', line 110 def colorize(string=nil, = {}) if string == nil return tagged_colors(self) end if @@is_tty colored = [color([:foreground]), color("on_#{options[:background]}"), extra([:extra])].compact * '' colored << string colored << extra(:clear) else string end end |
#colors ⇒ Object
124 125 126 |
# File 'lib/delicious-cli/colored.rb', line 124 def colors @@colors ||= COLORS.keys.sort end |
#disable! ⇒ Object
148 149 150 |
# File 'lib/delicious-cli/colored.rb', line 148 def disable! @@is_tty = false end |
#enable! ⇒ Object Also known as: force!
142 143 144 |
# File 'lib/delicious-cli/colored.rb', line 142 def enable! @@is_tty = true end |
#extra(extra_name) ⇒ Object
128 129 130 131 |
# File 'lib/delicious-cli/colored.rb', line 128 def extra(extra_name) extra_name = extra_name.to_s "\e[#{EXTRAS[extra_name]}m" if EXTRAS[extra_name] end |
#is_tty? ⇒ Boolean
152 153 154 |
# File 'lib/delicious-cli/colored.rb', line 152 def is_tty? @@is_tty end |
#tagged_colors(string) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/delicious-cli/colored.rb', line 178 def tagged_colors(string) stack = [] # split the string into tags and literal strings tokens = string.split(/(<\/?[\w\d_]+>)/) tokens.delete_if { |token| token.size == 0 } result = "" tokens.each do |token| # token is an opening tag! if /<([\w\d_]+)>/ =~ token and valid_tag?($1) stack.push $1 # token is a closing tag! elsif /<\/([\w\d_]+)>/ =~ token and valid_tag?($1) # if this color is on the stack somwehere... if pos = stack.rindex($1) # close the tag by removing it from the stack stack.delete_at pos else raise "Error: tried to close an unopened color tag -- #{token}" end # token is a literal string! else color = (stack.last || "white") color = BBS_COLOR_TABLE[color.to_i] if color =~ /^\d+$/ result << token.send(color) end end result end |
#valid_tag?(tag) ⇒ Boolean
159 160 161 162 163 164 165 |
# File 'lib/delicious-cli/colored.rb', line 159 def valid_tag?(tag) VALID_COLORS.include?(tag) or ( string =~ /^\d+$/ and BBS_COLOR_TABLE.include?(tag.to_i) ) end |