summaryrefslogtreecommitdiff
path: root/lib/irb/color_printer.rb
diff options
context:
space:
mode:
authorTakashi Kokubun <takashikkbn@gmail.com>2020-12-28 22:16:08 -0800
committerTakashi Kokubun <takashikkbn@gmail.com>2020-12-28 23:01:00 -0800
commitc715fb46c2d8eab48323a6008c5dbca550ebe2e0 (patch)
tree0f3edfd82629a620f08dd4e05f4f979f13648275 /lib/irb/color_printer.rb
parent1ffb267c5c782ea15d01beb90d031274ca88cd8e (diff)
[ruby/irb] Enhance colored inspect output
https://github.com/ruby/irb/commit/dffcdb5269
Diffstat (limited to 'lib/irb/color_printer.rb')
-rw-r--r--lib/irb/color_printer.rb22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/irb/color_printer.rb b/lib/irb/color_printer.rb
new file mode 100644
index 0000000000..187c337187
--- /dev/null
+++ b/lib/irb/color_printer.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+require 'pp'
+
+module IRB
+ class ColorPrinter < ::PP
+ def self.pp(obj, out = $>, width = 79)
+ q = ColorPrinter.new(out, width)
+ q.guard_inspect_key {q.pp obj}
+ q.flush
+ out
+ end
+
+ def text(str, width = str.length)
+ case str
+ when /\A#</, '=', '>'
+ super(Color.colorize(str, [:GREEN]), width)
+ else
+ super(Color.colorize_code(str, ignore_error: true), width)
+ end
+ end
+ end
+end