Class: IRB::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/irb/formatter.rb

Direct Known Subclasses

ColoredFormatter

Constant Summary collapse

DEFAULT_PROMPT =
"irb(%s):%03d:%d> "
SIMPLE_PROMPT =
">> "
NO_PROMPT =
""
RESULT_PREFIX =
"=>"
INDENTATION =
"  "
SYNTAX_ERROR =
"SyntaxError: compile error\n(irb):%d: %s"
SOURCE_ROOT =
Regexp.new("^#{File.expand_path('../../../', __FILE__)}")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFormatter

Returns a new instance of Formatter.



26
27
28
29
30
31
# File 'lib/irb/formatter.rb', line 26

def initialize
  @prompt      = :default
  @inspect     = true
  @auto_indent = true
  @filter_from_backtrace = [SOURCE_ROOT]
end

Instance Attribute Details

#auto_indentObject

Returns the value of attribute auto_indent.



23
24
25
# File 'lib/irb/formatter.rb', line 23

def auto_indent
  @auto_indent
end

#filter_from_backtraceObject (readonly)

Returns the value of attribute filter_from_backtrace.



24
25
26
# File 'lib/irb/formatter.rb', line 24

def filter_from_backtrace
  @filter_from_backtrace
end

#inspectObject

Returns the value of attribute inspect.



22
23
24
# File 'lib/irb/formatter.rb', line 22

def inspect
  @inspect
end

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



37
38
39
40
41
42
43
44
45
46
# File 'lib/irb/formatter.rb', line 37

def prompt(context, ignore_auto_indent = false, level = nil)
  level ||= context.level
  prompt = case @prompt
  when :default then DEFAULT_PROMPT % [context.object.inspect, context.line, level]
  when :simple  then SIMPLE_PROMPT
  else
    NO_PROMPT
  end
  @auto_indent && !ignore_auto_indent ? "#{prompt}#{indentation(level)}" : prompt
end

Instance Method Details

#exception(exception) ⇒ Object



93
94
95
96
# File 'lib/irb/formatter.rb', line 93

def exception(exception)
  backtrace = $DEBUG ? exception.backtrace : filter_backtrace(exception.backtrace)
  "#{exception.class.name}: #{exception.message}\n\t#{backtrace.join("\n\t")}"
end

#filter_backtrace(backtrace) ⇒ Object



98
99
100
101
102
# File 'lib/irb/formatter.rb', line 98

def filter_backtrace(backtrace)
  backtrace.reject do |line|
    @filter_from_backtrace.any? { |pattern| pattern.match(line) }
  end
end

#indentation(level) ⇒ Object



33
34
35
# File 'lib/irb/formatter.rb', line 33

def indentation(level)
  INDENTATION * level
end

#inspect_object(object) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/irb/formatter.rb', line 48

def inspect_object(object)
  if @inspect
    result = object.respond_to?(:pretty_inspect) ? object.pretty_inspect : object.inspect
    result.strip!
    result
  else
    minimal_inspect_object(object)
  end
end

#minimal_inspect_object(object) ⇒ Object



58
59
60
61
62
# File 'lib/irb/formatter.rb', line 58

def minimal_inspect_object(object)
  address = object.__id__ * 2
  address += 0x100000000 if address < 0
  "#<#{object.class}:0x%x>" % address
end

#reindent_last_line(context) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/irb/formatter.rb', line 64

def reindent_last_line(context)
  unless @auto_indent
    yield
    nil
  else
    source    = context.source
    old_level = source.level
    yield
    if line = source.buffer[-1]
      # only if the level raises do we use the new value
      level = source.level < old_level ? source.level : old_level
      new_line = "#{indentation(level)}#{line.lstrip}"
      # don't return anything if the new line and level are the same
      unless line == new_line && level == old_level
        source.buffer[-1] = new_line
        [prompt(context, true, level), new_line]
      end
    end
  end
end

#result(object) ⇒ Object



85
86
87
# File 'lib/irb/formatter.rb', line 85

def result(object)
  "#{RESULT_PREFIX} #{inspect_object(object)}"
end

#syntax_error(line, message) ⇒ Object



89
90
91
# File 'lib/irb/formatter.rb', line 89

def syntax_error(line, message)
  SYNTAX_ERROR % [line, message]
end