Skip to content

Commit 4c74c7d

Browse files
committed
Make IRB::ColorPrinter.pp compatible with PP.pp
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"`.
1 parent f846169 commit 4c74c7d

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

lib/irb/color_printer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def self.pp(obj, out = $>, width = 79)
88
q = ColorPrinter.new(out, width)
99
q.guard_inspect_key {q.pp obj}
1010
q.flush
11-
out
11+
out << "\n"
1212
end
1313

1414
def text(str, width = nil)

lib/irb/inspector.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def inspect_value(v)
116116
}
117117
Inspector.def_inspector([true, :pp, :pretty_inspect], proc{require "irb/color_printer"}){|v|
118118
if IRB.conf[:MAIN_CONTEXT]&.use_colorize?
119-
IRB::ColorPrinter.pp(v, '')
119+
IRB::ColorPrinter.pp(v, '').chomp
120120
else
121121
v.pretty_inspect.chomp
122122
end

test/irb/test_color.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ def test_color_printer
158158
skip 'Ripper::Lexer#scan is supported in Ruby 2.7+'
159159
end
160160
{
161-
1 => "#{BLUE}#{BOLD}1#{CLEAR}",
162-
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}",
163-
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}]",
161+
1 => "#{BLUE}#{BOLD}1#{CLEAR}\n",
162+
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",
163+
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",
164164
}.each do |object, result|
165165
actual = with_term { IRB::ColorPrinter.pp(object, '') }
166166
assert_equal(result, actual, "Case: IRB::ColorPrinter.pp(#{object.inspect}, '')")

0 commit comments

Comments
 (0)