Skip to content

Commit a5804c3

Browse files
committed
Do not colorize partially-correct inspect
This is to prevent a yellow-mixed output for ActiveSupport::TimeWithZone. Follows up #159 and ruby/ruby#3967.
1 parent 4e9c7c3 commit a5804c3

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

lib/irb/color.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ module Color
7171
end
7272
private_constant :TOKEN_SEQ_EXPRS
7373

74+
ERROR_TOKENS = TOKEN_SEQ_EXPRS.keys.select { |k| k.to_s.end_with?('error') }
75+
private_constant :ERROR_TOKENS
76+
7477
class << self
7578
def colorable?
7679
$stdout.tty? && supported? && (/mswin|mingw/ =~ RUBY_PLATFORM || (ENV.key?('TERM') && ENV['TERM'] != 'dumb'))
@@ -119,6 +122,11 @@ def colorize_code(code, complete: true, ignore_error: false)
119122
length = 0
120123

121124
scan(code, allow_last_error: !complete) do |token, str, expr|
125+
# IRB::ColorPrinter skips colorizing fragments with any invalid token
126+
if ignore_error && ERROR_TOKENS.include?(token)
127+
return Reline::Unicode.escape_for_print(code)
128+
end
129+
122130
in_symbol = symbol_state.scan_token(token)
123131
str.each_line do |line|
124132
line = Reline::Unicode.escape_for_print(line)
@@ -188,7 +196,7 @@ def scan(code, allow_last_error:)
188196
end
189197

190198
def dispatch_seq(token, expr, str, in_symbol:, ignore_error:)
191-
if token == :on_parse_error or token == :compile_error
199+
if ERROR_TOKENS.include?(token)
192200
ignore_error ? nil : TOKEN_SEQ_EXPRS[token][0]
193201
elsif in_symbol
194202
[YELLOW]

0 commit comments

Comments
 (0)