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
192 def colourize(str)
193  ''.tap { |res| Tokenizer.tokenize(str.to_s) { |token, value|
194     res << colourize_string(value, colours[token])
195   } }
196 rescue => err
197   Brice.error(self, __method__, err)
198   str
199 end
colourize_string(str, colour) click to toggle source

Return a string with the given colour.

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

Get current colour map

    # File lib/brice/colours.rb
182 def colours
183   @colours ||= DEFAULT_COLOURS.dup
184 end
colours=(hash) click to toggle source

Set colour map to hash

    # File lib/brice/colours.rb
177 def colours=(hash)
178   @colours = hash
179 end
disable() click to toggle source
    # File lib/brice/colours.rb
105 def disable
106   disable_irb
107   disable_pp
108 end
disable_irb() click to toggle source

Disable colourized IRb results.

    # File lib/brice/colours.rb
126 def disable_irb
127   IRB::Inspector.class_eval {
128     if method_defined?(:inspect_value_without_colour)
129       alias_method :inspect_value, :inspect_value_without_colour
130     end
131   }
132 end
disable_pp() click to toggle source
    # File lib/brice/colours.rb
164 def disable_pp
165   PP.class_eval {
166     if method_defined?(:pp_without_colour)
167       alias_method :pp, :pp_without_colour
168     end
169 
170     if method_defined?(:singleline_pp_without_colour)
171       alias_method :singleline_pp, :singleline_pp_without_colour
172     end
173   }
174 end
enable_irb() click to toggle source

Enable colourized IRb results.

    # File lib/brice/colours.rb
111 def enable_irb
112   IRB::Inspector.class_eval {
113     unless method_defined?(:inspect_value_with_colour)
114       alias_method :inspect_value_without_colour, :inspect_value
115 
116       def inspect_value_with_colour(value)
117         Colours.colourize(inspect_value_without_colour(value))
118       end
119     end
120 
121     alias_method :inspect_value, :inspect_value_with_colour
122   }
123 end
enable_pp() click to toggle source
    # File lib/brice/colours.rb
134 def enable_pp
135   require 'pp'
136 
137   class << PP
138     unless method_defined?(:pp_with_colour)
139       alias_method :pp_without_colour, :pp
140 
141       def pp_with_colour(obj, out = $>, width = 79)
142         res = pp_without_colour(obj, str = '', width)
143         out << Colours.colourize(str)
144         res
145       end
146 
147       alias_method :pp, :pp_with_colour
148     end
149 
150     unless method_defined?(:singleline_pp_with_colour)
151       alias_method :singleline_pp_without_colour, :singleline_pp
152 
153       def singleline_pp_with_colour(obj, out = $>)
154         res = singleline_pp_without_colour(obj, str = '')
155         out << Colours.colourize(str)
156         res
157       end
158 
159       alias_method :singleline_pp, :singleline_pp_with_colour
160     end
161   end
162 end
init(opt = {}) click to toggle source
    # File lib/brice/colours.rb
 96 def init(opt = {})
 97   require 'ripper'
 98 
 99   enable_irb if Brice.opt(opt, :irb, STDOUT.tty?)
100   enable_pp  if Brice.opt(opt, :pp,  STDOUT.tty?)
101 
102   self.colours = Brice.opt(opt, :colours, colours)
103 end
inspect_value_with_colour(value) click to toggle source
    # File lib/brice/colours.rb
116 def inspect_value_with_colour(value)
117   Colours.colourize(inspect_value_without_colour(value))
118 end