mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
34 lines
839 B
Ruby
34 lines
839 B
Ruby
|
# 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)
|
||
|
super *if !Pry.color
|
||
|
[str, width]
|
||
|
# Don't recolorize output with color [Issue #751]
|
||
|
elsif str.include?("\e[")
|
||
|
["#{str}\e[0m", width]
|
||
|
elsif str.start_with?('#<') || str == '=' || str == '>'
|
||
|
["#{OBJ_COLOR}#{str}\e[0m", width]
|
||
|
else
|
||
|
[CodeRay.scan(str, :ruby).term, width]
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|