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
223 224 225 226 227 228 |
# File 'lib/epitools/colored.rb', line 223 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
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/epitools/colored.rb', line 175 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
208 209 210 |
# File 'lib/epitools/colored.rb', line 208 def colors @@colors ||= COLORS.keys.sort end |
#disable! ⇒ Object
263 264 265 |
# File 'lib/epitools/colored.rb', line 263 def disable! @@is_tty = false end |
#enable! ⇒ Object Also known as: force!
243 244 245 |
# File 'lib/epitools/colored.rb', line 243 def enable! @@is_tty = true end |
#enable_temporarily(&block) ⇒ Object
252 253 254 255 256 257 258 |
# File 'lib/epitools/colored.rb', line 252 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?
233 234 235 |
# File 'lib/epitools/colored.rb', line 233 def enabled? @@is_tty end |
#extra(extra_name) ⇒ Object
215 216 217 218 |
# File 'lib/epitools/colored.rb', line 215 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
195 196 197 198 199 200 201 202 203 |
# File 'lib/epitools/colored.rb', line 195 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
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 317 318 319 |
# File 'lib/epitools/colored.rb', line 278 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
270 271 272 273 |
# File 'lib/epitools/colored.rb', line 270 def valid_tag?(tag) VALID_COLORS.include?(tag) or (tag =~ /^\d+$/ and BBS_COLOR_TABLE.include?(tag.to_i) ) end |