In Files

Files

Wirble::Colorize

Add color support to IRB.

Constants

DEFAULT_COLORS

Default Wirble color scheme.

TESTING_COLORS

Fruity testing colors.

Public Class Methods

colorize(str) click to toggle source

Colorize the results of inspect

# File lib/wirble.rb, line 408
def self.colorize(str)
  begin
    ret, nocol = '', Color.escape(:nothing)
    Tokenizer.tokenize(str) do |tok, val|
      # c = Color.escape(colors[tok])
      ret << colorize_string(val, colors[tok])
    end
    ret
  rescue
    # catch any errors from the tokenizer (just in case)
    str
  end
end
colorize_string(str, color) click to toggle source

Return a string with the given color.

# File lib/wirble.rb, line 400
def self.colorize_string(str, color)
  col, nocol = [color, :nothing].map { |key| Color.escape(key) }
  col ? "#{col}#{str}#{nocol}" : str
end
colors() click to toggle source

Get current color map

# File lib/wirble.rb, line 393
def self.colors
  @colors ||= {}.update(DEFAULT_COLORS)
end
colors=(hash) click to toggle source

Set color map to hash

# File lib/wirble.rb, line 386
def self.colors=(hash)
  @colors = hash
end
disable() click to toggle source

Disable colorized IRB results.

# File lib/wirble.rb, line 446
def self.disable
  ::IRB::Irb.class_eval do
    alias :output_value  :non_color_output_value
  end
end
enable(custom_colors = nil) click to toggle source

Enable colorized IRB results.

# File lib/wirble.rb, line 425
def self.enable(custom_colors = nil)
  # if there's a better way to do this, I'm all ears.
  ::IRB::Irb.class_eval do
    alias :non_color_output_value  :output_value

    def output_value
      if @context.inspect?
        val = Colorize.colorize(@context.last_value.inspect)
        printf @context.return_format, val
      else
        printf @context.return_format, @context.last_value
      end
    end
  end

  colors = custom_colors if custom_colors
end

Public Instance Methods

output_value() click to toggle source
# File lib/wirble.rb, line 430
def output_value
  if @context.inspect?
    val = Colorize.colorize(@context.last_value.inspect)
    printf @context.return_format, val
  else
    printf @context.return_format, @context.last_value
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.