Module | Wirble::Colorize |
In: |
lib/wirble.rb
|
Add color support to IRB.
DEFAULT_COLORS | = | { # delimiter colors :comma => :blue, :refers => :blue, # container colors (hash and array) :open_hash => :green, :close_hash => :green, :open_array => :green, :close_array => :green, # object colors :open_object => :light_red, :object_class => :white, :object_addr_prefix => :blue, :object_line_prefix => :blue, :close_object => :light_red, # symbol colors :symbol => :yellow, :symbol_prefix => :yellow, # string colors :open_string => :red, :string => :cyan, :close_string => :red, # misc colors :number => :cyan, :keyword => :green, :class => :light_green, :range => :red, } | Default Wirble color scheme. | |
TESTING_COLORS | = | { :comma => :red, :refers => :red, :open_hash => :blue, :close_hash => :blue, :open_array => :green, :close_array => :green, :open_object => :light_red, :object_class => :light_green, :object_addr => :purple, :object_line => :light_purple, :close_object => :light_red, :symbol => :yellow, :symbol_prefix => :yellow, :number => :cyan, :string => :cyan, :keyword => :white, :range => :light_blue, } | Fruity testing colors. |
output_value | -> | non_color_output_value |
non_color_output_value | -> | output_value |
Colorize the results of inspect
# File lib/wirble.rb, line 408 408: def self.colorize(str) 409: begin 410: ret, nocol = '', Color.escape(:nothing) 411: Tokenizer.tokenize(str) do |tok, val| 412: # c = Color.escape(colors[tok]) 413: ret << colorize_string(val, colors[tok]) 414: end 415: ret 416: rescue 417: # catch any errors from the tokenizer (just in case) 418: str 419: end 420: end
Return a string with the given color.
# File lib/wirble.rb, line 400 400: def self.colorize_string(str, color) 401: col, nocol = [color, :nothing].map { |key| Color.escape(key) } 402: col ? "#{col}#{str}#{nocol}" : str 403: end
Get current color map
# File lib/wirble.rb, line 393 393: def self.colors 394: @colors ||= {}.update(DEFAULT_COLORS) 395: end
Set color map to hash
# File lib/wirble.rb, line 386 386: def self.colors=(hash) 387: @colors = hash 388: end
Disable colorized IRB results.
# File lib/wirble.rb, line 446 446: def self.disable 447: ::IRB::Irb.class_eval do 448: alias :output_value :non_color_output_value 449: end 450: end
Enable colorized IRB results.
# File lib/wirble.rb, line 425 425: def self.enable(custom_colors = nil) 426: # if there's a better way to do this, I'm all ears. 427: ::IRB::Irb.class_eval do 428: alias :non_color_output_value :output_value 429: 430: def output_value 431: if @context.inspect? 432: val = Colorize.colorize(@context.last_value.inspect) 433: printf @context.return_format, val 434: else 435: printf @context.return_format, @context.last_value 436: end 437: end 438: end 439: 440: colors = custom_colors if custom_colors 441: end