Class: IRB::Formatter
- Inherits:
-
Object
- Object
- IRB::Formatter
- Defined in:
- lib/irb/formatter.rb
Direct Known Subclasses
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.('../../../', __FILE__)}")
Instance Attribute Summary collapse
-
#auto_indent ⇒ Object
Returns the value of attribute auto_indent.
-
#filter_from_backtrace ⇒ Object
readonly
Returns the value of attribute filter_from_backtrace.
-
#inspect ⇒ Object
Returns the value of attribute inspect.
- #prompt(context, ignore_auto_indent = false, level = nil) ⇒ Object
Instance Method Summary collapse
- #exception(exception) ⇒ Object
- #filter_backtrace(backtrace) ⇒ Object
- #indentation(level) ⇒ Object
-
#initialize ⇒ Formatter
constructor
A new instance of Formatter.
- #inspect_object(object) ⇒ Object
- #minimal_inspect_object(object) ⇒ Object
- #reindent_last_line(context) ⇒ Object
- #result(object) ⇒ Object
- #syntax_error(line, message) ⇒ Object
Constructor Details
#initialize ⇒ Formatter
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_indent ⇒ Object
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_backtrace ⇒ Object (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 |
#inspect ⇒ Object
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.}\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, ) SYNTAX_ERROR % [line, ] end |