diff options
Diffstat (limited to 'lib/irb/color_printer.rb')
-rw-r--r-- | lib/irb/color_printer.rb | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/lib/irb/color_printer.rb b/lib/irb/color_printer.rb index 30c6825750..31644aa7f9 100644 --- a/lib/irb/color_printer.rb +++ b/lib/irb/color_printer.rb @@ -1,9 +1,12 @@ # frozen_string_literal: true require 'pp' -require 'irb/color' +require_relative 'color' module IRB class ColorPrinter < ::PP + METHOD_RESPOND_TO = Object.instance_method(:respond_to?) + METHOD_INSPECT = Object.instance_method(:inspect) + class << self def pp(obj, out = $>, width = screen_width) q = ColorPrinter.new(out, width) @@ -22,9 +25,11 @@ module IRB end def pp(obj) - if obj.is_a?(String) + if String === obj # Avoid calling Ruby 2.4+ String#pretty_print that splits a string by "\n" text(obj.inspect) + elsif !METHOD_RESPOND_TO.bind(obj).call(:inspect) + text(METHOD_INSPECT.bind(obj).call) else super end @@ -37,6 +42,9 @@ module IRB width ||= str.length case str + when '' + when ',', '=>', '[', ']', '{', '}', '..', '...', /\A@\w+\z/ + super(str, width) when /\A#</, '=', '>' super(Color.colorize(str, [:GREEN]), width) else |