module Brice::Colours

Add colour support to IRb.

Set your own colours with config.colours.opt = { :colours => { ... } } or modify the default scheme (DEFAULT_COLOURS) with config.colours.opt = { :colours => Brice::Colours::DEFAULT_COLOURS.merge(...) }.

Constants

DEFAULT_COLOURS

Default IRb colour scheme.

TESTING_COLOURS

Fruity testing colours.

Public Instance Methods

colourize(str) click to toggle source

Colourize the results of inspect

# File lib/brice/colours.rb, line 190
def colourize(str)
  res = ''

  Tokenizer.tokenize(str) { |token, value|
    res << colourize_string(value, colours[token])
  }

  res
rescue => err
  warn "#{err.backtrace.first}: #{err} (#{err.class})"
  str
end
colourize_string(str, colour) click to toggle source

Return a string with the given colour.

# File lib/brice/colours.rb, line 185
def colourize_string(str, colour)
  (col = Colour[colour]) ? "#{col}#{str}#{Colour[:reset]}" : str
end
colours() click to toggle source

Get current colour map

# File lib/brice/colours.rb, line 180
def colours
  @colours ||= DEFAULT_COLOURS.dup
end
colours=(hash) click to toggle source

Set colour map to hash

# File lib/brice/colours.rb, line 175
def colours=(hash)
  @colours = hash
end
disable() click to toggle source
# File lib/brice/colours.rb, line 103
def disable
  disable_irb
  disable_pp
end
disable_irb() click to toggle source

Disable colourized IRb results.

# File lib/brice/colours.rb, line 124
def disable_irb
  IRB::Inspector.class_eval {
    if method_defined?(:inspect_value_without_colour)
      alias_method :inspect_value, :inspect_value_without_colour
    end
  }
end
disable_pp() click to toggle source
# File lib/brice/colours.rb, line 162
def disable_pp
  PP.class_eval {
    if method_defined?(:pp_without_colour)
      alias_method :pp, :pp_without_colour
    end

    if method_defined?(:singleline_pp_without_colour)
      alias_method :singleline_pp, :singleline_pp_without_colour
    end
  }
end
enable_irb() click to toggle source

Enable colourized IRb results.

# File lib/brice/colours.rb, line 109
def enable_irb
  IRB::Inspector.class_eval {
    unless method_defined?(:inspect_value_with_colour)
      alias_method :inspect_value_without_colour, :inspect_value

      def inspect_value_with_colour(value)
        Colours.colourize(inspect_value_without_colour(value))
      end
    end

    alias_method :inspect_value, :inspect_value_with_colour
  }
end
enable_pp() click to toggle source
# File lib/brice/colours.rb, line 132
def enable_pp
  require 'pp'

  class << PP
    unless method_defined?(:pp_with_colour)
      alias_method :pp_without_colour, :pp

      def pp_with_colour(obj, out = $>, width = 79)
        res = pp_without_colour(obj, str = '', width)
        out << Colours.colourize(str)
        res
      end

      alias_method :pp, :pp_with_colour
    end

    unless method_defined?(:singleline_pp_with_colour)
      alias_method :singleline_pp_without_colour, :singleline_pp

      def singleline_pp_with_colour(obj, out = $>)
        res = singleline_pp_without_colour(obj, str = '')
        out << Colours.colourize(str)
        res
      end

      alias_method :singleline_pp, :singleline_pp_with_colour
    end
  end
end
init(opt = {}) click to toggle source
# File lib/brice/colours.rb, line 96
def init(opt = {})
  enable_irb if Brice.opt(opt, :irb, STDOUT.tty?)
  enable_pp  if Brice.opt(opt, :pp,  STDOUT.tty?)

  self.colours = Brice.opt(opt, :colours, colours)
end
inspect_value_with_colour(value) click to toggle source
# File lib/brice/colours.rb, line 114
def inspect_value_with_colour(value)
  Colours.colourize(inspect_value_without_colour(value))
end