Module: Brice::Colours

Extended by:
Colours
Included in:
Colours
Defined in:
lib/brice/colours.rb

Overview

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(...) }.

Defined Under Namespace

Modules: Colour Classes: Tokenizer

Constant Summary collapse

DEFAULT_COLOURS =

Default IRb colour scheme.

{
  # delimiter colours
  comma:              :blue,
  refers:             :blue,

  # container colours (hash and array)
  open_hash:          :green,
  close_hash:         :green,
  open_array:         :green,
  close_array:        :green,

  # object colours
  open_object:        :light_red,
  object_class:       :white,
  object_addr_prefix: :blue,
  object_line_prefix: :blue,
  close_object:       :light_red,

  # symbol colours
  symbol:             :yellow,
  symbol_prefix:      :yellow,

  # string colours
  open_string:        :red,
  string:             :cyan,
  close_string:       :red,

  # misc colours
  number:             :cyan,
  keyword:            :green,
  class:              :light_green,
  range:              :red,
  unknown:            :green
}
TESTING_COLOURS =

Fruity testing colours.

{
  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
}

Instance Method Summary collapse

Instance Method Details

#colourize(str) ⇒ Object

Colourize the results of inspect


192
193
194
195
196
197
198
199
# File 'lib/brice/colours.rb', line 192

def colourize(str)
 ''.tap { |res| Tokenizer.tokenize(str.to_s) { |token, value|
    res << colourize_string(value, colours[token])
  } }
rescue => err
  Brice.error(self, __method__, err)
  str
end

#colourize_string(str, colour) ⇒ Object

Return a string with the given colour.


187
188
189
# File 'lib/brice/colours.rb', line 187

def colourize_string(str, colour)
  (col = Colour[colour]) ? "#{col}#{str}#{Colour[:reset]}" : str
end

#coloursObject

Get current colour map


182
183
184
# File 'lib/brice/colours.rb', line 182

def colours
  @colours ||= DEFAULT_COLOURS.dup
end

#colours=(hash) ⇒ Object

Set colour map to hash


177
178
179
# File 'lib/brice/colours.rb', line 177

def colours=(hash)
  @colours = hash
end

#disableObject


105
106
107
108
# File 'lib/brice/colours.rb', line 105

def disable
  disable_irb
  disable_pp
end

#disable_irbObject

Disable colourized IRb results.


126
127
128
129
130
131
132
# File 'lib/brice/colours.rb', line 126

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_ppObject


164
165
166
167
168
169
170
171
172
173
174
# File 'lib/brice/colours.rb', line 164

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_irbObject

Enable colourized IRb results.


111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/brice/colours.rb', line 111

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_ppObject


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/brice/colours.rb', line 134

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 = {}) ⇒ Object


96
97
98
99
100
101
102
103
# File 'lib/brice/colours.rb', line 96

def init(opt = {})
  require 'ripper'

  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