1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/lib/irb/color_printer.rb
Takashi Kokubun 98bd7e87a0 [ruby/irb] 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"`.

https://github.com/ruby/irb/commit/4c74c7d84c
2021-01-07 22:43:40 -08:00

28 lines
590 B
Ruby

# frozen_string_literal: true
require 'pp'
require 'irb/color'
module IRB
class ColorPrinter < ::PP
def self.pp(obj, out = $>, width = 79)
q = ColorPrinter.new(out, width)
q.guard_inspect_key {q.pp obj}
q.flush
out << "\n"
end
def text(str, width = nil)
unless str.is_a?(String)
str = str.inspect
end
width ||= str.length
case str
when /\A#</, '=', '>'
super(Color.colorize(str, [:GREEN]), width)
else
super(Color.colorize_code(str, ignore_error: true), width)
end
end
end
end