Module: Wirble::Colorize

Defined in:
lib/wirble.rb

Overview

Add color support to IRB.

Defined Under Namespace

Modules: Color, Tokenizer

Constant Summary collapse

DEFAULT_COLORS =

Default Wirble color scheme.

{
  # 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,
}
TESTING_COLORS =

Fruity 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,
}

Class Method Summary collapse

Class Method Details

.colorize(str) ⇒ Object

Colorize the results of inspect



408
409
410
411
412
413
414
415
416
417
418
419
420
# 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) ⇒ Object

Return a string with the given color.



400
401
402
403
# 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

.colorsObject

Get current color map



393
394
395
# File 'lib/wirble.rb', line 393

def self.colors
  @colors ||= {}.update(DEFAULT_COLORS)
end

.colors=(hash) ⇒ Object

Set color map to hash



386
387
388
# File 'lib/wirble.rb', line 386

def self.colors=(hash)
  @colors = hash
end

.disableObject

Disable colorized IRB results.



446
447
448
449
450
# 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) ⇒ Object

Enable colorized IRB results.



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# 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