Skip to content

Commit

Permalink
Make IRB::ColorPrinter.pp compatible with PP.pp
Browse files Browse the repository at this point in the history
The incompatible interface is not helpful, again if you want to use it
as a standalone library, falling it back to PP.

Original PP.pp also ends with `out << "\n"`.
  • Loading branch information
k0kubun committed Jan 8, 2021
1 parent f846169 commit 4c74c7d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/irb/color_printer.rb
Expand Up @@ -8,7 +8,7 @@ def self.pp(obj, out = $>, width = 79)
q = ColorPrinter.new(out, width)
q.guard_inspect_key {q.pp obj}
q.flush
out
out << "\n"
end

def text(str, width = nil)
Expand Down
2 changes: 1 addition & 1 deletion lib/irb/inspector.rb
Expand Up @@ -116,7 +116,7 @@ def inspect_value(v)
}
Inspector.def_inspector([true, :pp, :pretty_inspect], proc{require "irb/color_printer"}){|v|
if IRB.conf[:MAIN_CONTEXT]&.use_colorize?
IRB::ColorPrinter.pp(v, '')
IRB::ColorPrinter.pp(v, '').chomp
else
v.pretty_inspect.chomp
end
Expand Down
6 changes: 3 additions & 3 deletions test/irb/test_color.rb
Expand Up @@ -158,9 +158,9 @@ def test_color_printer
skip 'Ripper::Lexer#scan is supported in Ruby 2.7+'
end
{
1 => "#{BLUE}#{BOLD}1#{CLEAR}",
Struct.new('IRBTestColorPrinter', :a).new('test') => "#{GREEN}#<struct Struct::IRBTestColorPrinter#{CLEAR} a#{GREEN}=#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}#{RED}test#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}#{GREEN}>#{CLEAR}",
Ripper::Lexer.new('1').scan => "[#{GREEN}#<Ripper::Lexer::Elem:#{CLEAR} on_int@1:0 END token: #{RED}#{BOLD}\"#{CLEAR}#{RED}1#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}#{GREEN}>#{CLEAR}]",
1 => "#{BLUE}#{BOLD}1#{CLEAR}\n",
Struct.new('IRBTestColorPrinter', :a).new('test') => "#{GREEN}#<struct Struct::IRBTestColorPrinter#{CLEAR} a#{GREEN}=#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}#{RED}test#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}#{GREEN}>#{CLEAR}\n",
Ripper::Lexer.new('1').scan => "[#{GREEN}#<Ripper::Lexer::Elem:#{CLEAR} on_int@1:0 END token: #{RED}#{BOLD}\"#{CLEAR}#{RED}1#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}#{GREEN}>#{CLEAR}]\n",
}.each do |object, result|
actual = with_term { IRB::ColorPrinter.pp(object, '') }
assert_equal(result, actual, "Case: IRB::ColorPrinter.pp(#{object.inspect}, '')")
Expand Down

0 comments on commit 4c74c7d

Please sign in to comment.