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 =
"=>"
SYNTAX_ERROR =
"SyntaxError: compile error\n(irb):%d: %s"
SOURCE_ROOT =
/^#{File.expand_path('../../../', __FILE__)}/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFormatter

Returns a new instance of Formatter.



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

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

Instance Attribute Details

#filter_from_backtraceObject (readonly)

Returns the value of attribute filter_from_backtrace.



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

def filter_from_backtrace
  @filter_from_backtrace
end

#inspectObject

Returns the value of attribute inspect.



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

def inspect
  @inspect
end

#prompt(context) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/irb/formatter.rb', line 30

def prompt(context)
  case @prompt
  when :default then DEFAULT_PROMPT % [context.object.inspect, context.line, context.source.level]
  when :simple  then SIMPLE_PROMPT
  else
    NO_PROMPT
  end
end

Instance Method Details

#exception(exception) ⇒ Object



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

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



64
65
66
67
68
# File 'lib/irb/formatter.rb', line 64

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

#inspect_object(object) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/irb/formatter.rb', line 39

def inspect_object(object)
  if @inspect
    result = object.respond_to?(:pretty_inspect) ? object.pretty_inspect : object.inspect
    result.strip!
    result
  else
    address = object.__id__ * 2
    address += 0x100000000 if address < 0
    "#<#{object.class}:0x%x>" % address
  end
end

#result(object) ⇒ Object



51
52
53
# File 'lib/irb/formatter.rb', line 51

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

#syntax_error(line, message) ⇒ Object



55
56
57
# File 'lib/irb/formatter.rb', line 55

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