Skip to content

Commit dffcdb5

Browse files
committed
Enhance colored inspect output
1 parent e7bdcf6 commit dffcdb5

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

lib/irb/color.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def colorize(text, seq)
107107
# If `complete` is false (code is incomplete), this does not warn compile_error.
108108
# This option is needed to avoid warning a user when the compile_error is happening
109109
# because the input is not wrong but just incomplete.
110-
def colorize_code(code, complete: true)
110+
def colorize_code(code, complete: true, ignore_error: false)
111111
return code unless colorable?
112112

113113
symbol_state = SymbolState.new
@@ -118,7 +118,7 @@ def colorize_code(code, complete: true)
118118
in_symbol = symbol_state.scan_token(token)
119119
str.each_line do |line|
120120
line = Reline::Unicode.escape_for_print(line)
121-
if seq = dispatch_seq(token, expr, line, in_symbol: in_symbol)
121+
if seq = dispatch_seq(token, expr, line, in_symbol: in_symbol, ignore_error: ignore_error)
122122
colored << seq.map { |s| "\e[#{s}m" }.join('')
123123
colored << line.sub(/\Z/, clear)
124124
else
@@ -183,9 +183,9 @@ def scan(code, allow_last_error:)
183183
$VERBOSE = verbose
184184
end
185185

186-
def dispatch_seq(token, expr, str, in_symbol:)
186+
def dispatch_seq(token, expr, str, in_symbol:, ignore_error:)
187187
if token == :on_parse_error or token == :compile_error
188-
TOKEN_SEQ_EXPRS[token][0]
188+
ignore_error ? nil : TOKEN_SEQ_EXPRS[token][0]
189189
elsif in_symbol
190190
[YELLOW]
191191
elsif TOKEN_KEYWORDS.fetch(token, []).include?(str)

lib/irb/color_printer.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
require 'pp'
3+
4+
module IRB
5+
class ColorPrinter < ::PP
6+
def self.pp(obj, out = $>, width = 79)
7+
q = ColorPrinter.new(out, width)
8+
q.guard_inspect_key {q.pp obj}
9+
q.flush
10+
out
11+
end
12+
13+
def text(str, width = str.length)
14+
case str
15+
when /\A#</, '=', '>'
16+
super(Color.colorize(str, [:GREEN]), width)
17+
else
18+
super(Color.colorize_code(str, ignore_error: true), width)
19+
end
20+
end
21+
end
22+
end

lib/irb/inspector.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def inspect_value(v)
104104
end
105105

106106
Inspector.def_inspector([false, :to_s, :raw]){|v| v.to_s}
107-
Inspector.def_inspector([true, :p, :inspect]){|v|
107+
Inspector.def_inspector([:p, :inspect]){|v|
108108
begin
109109
result = v.inspect
110110
if IRB.conf[:MAIN_CONTEXT]&.use_colorize? && Color.inspect_colorable?(v)
@@ -116,12 +116,12 @@ def inspect_value(v)
116116
''
117117
end
118118
}
119-
Inspector.def_inspector([:pp, :pretty_inspect], proc{require "pp"}){|v|
120-
result = v.pretty_inspect.chomp
121-
if IRB.conf[:MAIN_CONTEXT]&.use_colorize? && Color.inspect_colorable?(v)
122-
result = Color.colorize_code(result)
119+
Inspector.def_inspector([true, :pp, :pretty_inspect], proc{require "irb/color_printer"}){|v|
120+
if IRB.conf[:MAIN_CONTEXT]&.use_colorize?
121+
IRB::ColorPrinter.pp(v, '')
122+
else
123+
v.pretty_inspect.chomp
123124
end
124-
result
125125
}
126126
Inspector.def_inspector([:yaml, :YAML], proc{require "yaml"}){|v|
127127
begin

0 commit comments

Comments
 (0)