Class: IRB::ColoredFormatter

Inherits:
Formatter show all
Defined in:
lib/irb/ext/colorize.rb

Defined Under Namespace

Modules: Color

Constant Summary collapse

TYPE_ALIASES =
{
  :on_comma           => :comma,
  :refers             => :operator, # Wirble compat
  :on_op              => :operator,
  
  :on_lbrace          => :open_hash,
  :on_rbrace          => :close_hash,
  :on_lbracket        => :open_array,
  :on_rbracket        => :close_array,
  
  :on_ident           => :symbol,
  :on_symbeg          => :symbol_prefix,
  
  :on_tstring_beg     => :open_string,
  :on_tstring_content => :string,
  :on_tstring_end     => :close_string,
  
  :on_int             => :number,
  :on_float           => :number,
  :on_kw              => :keyword,
  :on_const           => :constant,
  :class              => :constant # Wirble compat
}
COLOR_SCHEMES =

# object colors :open_object => :dark_gray, :object_class => :purple, :object_addr_prefix => :blue, :object_line_prefix => :blue, :close_object => :dark_gray,

{
  :dark_background => {
    # :prompt             => :green,
    # :result_prefix      => :light_purple,
    
    :comma              => :blue,
    :operator           => :blue,
    
    :open_hash          => :green,
    :close_hash         => :green,
    :open_array         => :green,
    :close_array        => :green,
    
    :symbol_prefix      => :yellow, # hmm ident...
    :symbol             => :yellow,
    
    :open_string        => :red,
    :string             => :cyan,
    :close_string       => :red,
    
    :number             => :cyan,
    :keyword            => :yellow,
    :constant           => :light_green
  },
  :light_background => {
    :comma              => :purple,
    :operator           => :blue,
    
    :open_hash          => :red,
    :close_hash         => :red,
    :open_array         => :red,
    :close_array        => :red,
    
    :symbol_prefix      => :black,
    :symbol             => :light_gray,
    
    :open_string        => :blue,
    :string             => :dark_gray,
    :close_string       => :blue,
    
    :number             => :black,
    :keyword            => :brown,
    :constant           => :red
  },
  :fresh => {
    :prompt             => :green,
    :result_prefix      => :light_purple,
    
    :comma              => :red,
    :operator           => :red,
    
    :open_hash          => :blue,
    :close_hash         => :blue,
    :open_array         => :green,
    :close_array        => :green,
    
    :symbol_prefix      => :yellow,
    :symbol             => :yellow,
    
    :number             => :cyan,
    :string             => :cyan,
    :keyword            => :white
  }
}

Constants inherited from Formatter

Formatter::DEFAULT_PROMPT, Formatter::INDENTATION, Formatter::NO_PROMPT, Formatter::RESULT_PREFIX, Formatter::SIMPLE_PROMPT, Formatter::SOURCE_ROOT, Formatter::SYNTAX_ERROR

Instance Attribute Summary collapse

Attributes inherited from Formatter

#auto_indent, #filter_from_backtrace, #inspect

Instance Method Summary collapse

Methods inherited from Formatter

#exception, #filter_backtrace, #indentation, #inspect_object, #minimal_inspect_object, #reindent_last_line, #syntax_error

Constructor Details

#initializeColoredFormatter

Returns a new instance of ColoredFormatter.



145
146
147
148
# File 'lib/irb/ext/colorize.rb', line 145

def initialize
  super
  self.color_scheme = :dark_background
end

Instance Attribute Details

#color_schemeObject

Returns the value of attribute color_scheme.



143
144
145
# File 'lib/irb/ext/colorize.rb', line 143

def color_scheme
  @color_scheme
end

#colorsObject (readonly)

Returns the value of attribute colors.



143
144
145
# File 'lib/irb/ext/colorize.rb', line 143

def colors
  @colors
end

Instance Method Details

#color(type) ⇒ Object



155
156
157
158
# File 'lib/irb/ext/colorize.rb', line 155

def color(type)
  type = TYPE_ALIASES[type] if TYPE_ALIASES.has_key?(type)
  @colors[type]
end

#colorize(str) ⇒ Object



168
169
170
# File 'lib/irb/ext/colorize.rb', line 168

def colorize(str)
  Ripper.lex(str).map { |_, type, token| colorize_token(type, token) }.join
end

#colorize_token(type, token) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/irb/ext/colorize.rb', line 160

def colorize_token(type, token)
  if color = color(type)
    "#{Color.escape(color)}#{token}#{Color::CLEAR}"
  else
    token
  end
end

#prompt(context, ignore_auto_indent = false, level = nil) ⇒ Object



172
173
174
# File 'lib/irb/ext/colorize.rb', line 172

def prompt(context, ignore_auto_indent = false, level = nil)
  colorize_token(:prompt, super)
end

#result(object) ⇒ Object



180
181
182
# File 'lib/irb/ext/colorize.rb', line 180

def result(object)
  "#{result_prefix} #{colorize(inspect_object(object))}"
end

#result_prefixObject



176
177
178
# File 'lib/irb/ext/colorize.rb', line 176

def result_prefix
  colorize_token(:result_prefix, Formatter::RESULT_PREFIX)
end