2020-12-29 01:16:08 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'pp'
|
2021-01-08 01:32:47 -05:00
|
|
|
require 'irb/color'
|
2020-12-29 01:16:08 -05:00
|
|
|
|
|
|
|
module IRB
|
|
|
|
class ColorPrinter < ::PP
|
2021-01-19 11:56:51 -05:00
|
|
|
class << self
|
|
|
|
def pp(obj, out = $>, width = screen_width)
|
|
|
|
q = ColorPrinter.new(out, width)
|
|
|
|
q.guard_inspect_key {q.pp obj}
|
|
|
|
q.flush
|
|
|
|
out << "\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def screen_width
|
|
|
|
Reline.get_screen_size.last
|
|
|
|
rescue Errno::EINVAL # in `winsize': Invalid argument - <STDIN>
|
|
|
|
79
|
|
|
|
end
|
2020-12-29 01:16:08 -05:00
|
|
|
end
|
|
|
|
|
2021-03-19 22:08:39 -04:00
|
|
|
def pp(obj)
|
|
|
|
if obj.is_a?(String)
|
|
|
|
# Avoid calling Ruby 2.4+ String#pretty_print that splits a string by "\n"
|
|
|
|
text(obj.inspect)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-04 05:38:22 -05:00
|
|
|
def text(str, width = nil)
|
|
|
|
unless str.is_a?(String)
|
|
|
|
str = str.inspect
|
|
|
|
end
|
|
|
|
width ||= str.length
|
|
|
|
|
2020-12-29 01:16:08 -05:00
|
|
|
case str
|
|
|
|
when /\A#</, '=', '>'
|
|
|
|
super(Color.colorize(str, [:GREEN]), width)
|
|
|
|
else
|
|
|
|
super(Color.colorize_code(str, ignore_error: true), width)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|