Module: Colored
Overview
require ‘Win32/Console/ANSI’ if RUBY_PLATFORM =~ /win32/
Constant Summary collapse
- COLORS =
{ 'black' => 30, 'red' => 31, 'green' => 32, 'yellow' => 33, 'blue' => 34, 'magenta' => 35, 'purple' => 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 =
begin normal = COLORS.keys lights = normal.map { |fore| "light_#{fore}" } brights = normal.map { |fore| "bright_#{fore}" } on_backgrounds = normal.map { |fore| normal.map { |back| "#{fore}_on_#{back}" } }.flatten Set.new(normal + lights + brights + on_backgrounds + ["grey", "gray"]) end
- @@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!)
- #enable_temporarily(&block) ⇒ Object
- #enabled? ⇒ Boolean (also: #is_tty?)
- #extra(extra_name) ⇒ Object
- #highlight(pattern, color = :light_yellow, &block) ⇒ Object
- #tagged_colors ⇒ Object
- #valid_tag?(tag) ⇒ Boolean
Instance Method Details
#color(color_name) ⇒ Object
220 221 222 223 224 225 |
# File 'lib/upm/colored.rb', line 220 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
172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/upm/colored.rb', line 172 def colorize(string=nil, = {}) if string == nil return self.tagged_colors end if @@is_tty colored = [color([:foreground]), color("on_#{[:background]}"), extra([:extra])].compact * '' colored << string colored << extra(:clear) else string end end |
#colors ⇒ Object
205 206 207 |
# File 'lib/upm/colored.rb', line 205 def colors @@colors ||= COLORS.keys.sort end |
#disable! ⇒ Object
260 261 262 |
# File 'lib/upm/colored.rb', line 260 def disable! @@is_tty = false end |
#enable! ⇒ Object Also known as: force!
240 241 242 |
# File 'lib/upm/colored.rb', line 240 def enable! @@is_tty = true end |
#enable_temporarily(&block) ⇒ Object
249 250 251 252 253 254 255 |
# File 'lib/upm/colored.rb', line 249 def enable_temporarily(&block) last_state = @@is_tty @@is_tty = true block.call @@is_tty = last_state end |
#enabled? ⇒ Boolean Also known as: is_tty?
230 231 232 |
# File 'lib/upm/colored.rb', line 230 def enabled? @@is_tty end |
#extra(extra_name) ⇒ Object
212 213 214 215 |
# File 'lib/upm/colored.rb', line 212 def extra(extra_name) extra_name = extra_name.to_s "\e[#{EXTRAS[extra_name]}m" if EXTRAS[extra_name] end |
#highlight(pattern, color = :light_yellow, &block) ⇒ Object
192 193 194 195 196 197 198 199 200 |
# File 'lib/upm/colored.rb', line 192 def highlight(pattern, color=:light_yellow, &block) pattern = Regexp.new(Regexp.escape(pattern)) if pattern.is_a? String if block_given? gsub(pattern, &block) else gsub(pattern) { |match| match.send(color) } end end |
#tagged_colors ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/upm/colored.rb', line 275 def tagged_colors stack = [] # split the string into tags and literal strings tokens = self.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
267 268 269 270 |
# File 'lib/upm/colored.rb', line 267 def valid_tag?(tag) VALID_COLORS.include?(tag) or (tag =~ /^\d+$/ and BBS_COLOR_TABLE.include?(tag.to_i) ) end |