-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor_printer.rb
55 lines (48 loc) · 1.29 KB
/
color_printer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true
require 'pp'
require_relative 'color'
module IRB
class ColorPrinter < ::PP
METHOD_RESPOND_TO = Object.instance_method(:respond_to?)
METHOD_INSPECT = Object.instance_method(:inspect)
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
end
def pp(obj)
if String === obj
# Avoid calling Ruby 2.4+ String#pretty_print that splits a string by "\n"
text(obj.inspect)
elsif !METHOD_RESPOND_TO.bind(obj).call(:inspect)
text(METHOD_INSPECT.bind(obj).call)
else
super
end
end
def text(str, width = nil)
unless str.is_a?(String)
str = str.inspect
end
width ||= str.length
case str
when ''
when ',', '=>', '[', ']', '{', '}', '..', '...', /\A@\w+\z/
super(str, width)
when /\A#</, '=', '>'
super(Color.colorize(str, [:GREEN]), width)
else
super(Color.colorize_code(str, ignore_error: true), width)
end
end
end
end