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
|
|
|
|
|
|
|
|
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)
|
2013-11-09 22:14:20 -05:00
|
|
|
super(*if !Pry.color
|
2013-10-29 00:36:00 -04:00
|
|
|
[str, width]
|
|
|
|
# Don't recolorize output with color [Issue #751]
|
|
|
|
elsif str.include?("\e[")
|
|
|
|
["#{str}\e[0m", width]
|
|
|
|
elsif str.start_with?('#<') || str == '=' || str == '>'
|
2013-11-02 18:09:39 -04:00
|
|
|
[highlight_object_literal(str), width]
|
2013-10-29 00:36:00 -04:00
|
|
|
else
|
|
|
|
[CodeRay.scan(str, :ruby).term, width]
|
2013-11-09 22:14:20 -05:00
|
|
|
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
|
|
|
|
|
2013-11-02 18:09:39 -04:00
|
|
|
# Read the class name off of the singleton class to provide a default
|
|
|
|
# inspect.
|
2014-01-19 21:00:25 -05:00
|
|
|
singleton = class << obj; self; end
|
|
|
|
ancestors = Pry::Method.safe_send(singleton, :ancestors)
|
|
|
|
klass = ancestors.reject { |k| k == singleton }.first
|
2013-11-02 18:09:39 -04:00
|
|
|
obj_id = obj.__id__.to_s(16) rescue 0
|
|
|
|
str = "#<#{klass}:0x#{obj_id}>"
|
|
|
|
|
|
|
|
text(Pry.color ? highlight_object_literal(str) : str)
|
|
|
|
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
|