2013-10-29 00:36:00 -04:00
|
|
|
# PP subclass for streaming inspect output in color.
|
|
|
|
class Pry
|
|
|
|
class ColorPrinter < ::PP
|
|
|
|
OBJ_COLOR = begin
|
|
|
|
code = CodeRay::Encoders::Terminal::TOKEN_COLORS[:keyword]
|
|
|
|
if code.start_with? "\e"
|
|
|
|
code
|
|
|
|
else
|
|
|
|
"\e[0m\e[0;#{code}m"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-30 11:17:03 -04:00
|
|
|
CodeRay::Encoders::Terminal::TOKEN_COLORS[:comment][:self] = "\e[1;34m"
|
|
|
|
|
2013-10-29 00:36:00 -04:00
|
|
|
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 = str.length)
|
|
|
|
# Don't recolorize output with color [Issue #751]
|
2014-05-01 04:14:33 -04:00
|
|
|
if str.include?("\e[")
|
|
|
|
super "#{str}\e[0m", width
|
2013-10-29 00:36:00 -04:00
|
|
|
elsif str.start_with?('#<') || str == '=' || str == '>'
|
2014-05-01 04:14:33 -04:00
|
|
|
super highlight_object_literal(str), width
|
2013-10-29 00:36:00 -04:00
|
|
|
else
|
2014-05-01 04:14:33 -04:00
|
|
|
super CodeRay.scan(str, :ruby).term, width
|
|
|
|
end
|
2013-10-29 00:36:00 -04:00
|
|
|
end
|
2013-11-02 18:09:39 -04:00
|
|
|
|
|
|
|
def pp(obj)
|
|
|
|
super
|
2013-11-02 18:46:38 -04:00
|
|
|
rescue => e
|
|
|
|
raise if e.is_a? Pry::Pager::StopPaging
|
2015-01-12 15:18:59 -05:00
|
|
|
begin
|
|
|
|
str = obj.inspect
|
|
|
|
rescue Exception
|
|
|
|
# Read the class name off of the singleton class to provide a default
|
|
|
|
# inspect.
|
|
|
|
singleton = class << obj; self; end
|
|
|
|
ancestors = Pry::Method.safe_send(singleton, :ancestors)
|
|
|
|
klass = ancestors.reject { |k| k == singleton }.first
|
|
|
|
obj_id = obj.__id__.to_s(16) rescue 0
|
|
|
|
str = "#<#{klass}:0x#{obj_id}>"
|
|
|
|
end
|
2013-11-02 18:09:39 -04:00
|
|
|
|
2014-05-01 04:14:33 -04:00
|
|
|
text highlight_object_literal(str)
|
2013-11-02 18:09:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def highlight_object_literal(object_literal)
|
|
|
|
"#{OBJ_COLOR}#{object_literal}\e[0m"
|
|
|
|
end
|
2013-10-29 00:36:00 -04:00
|
|
|
end
|
|
|
|
end
|