ruby-changes:64657
From: Takashi <ko1@a...>
Date: Tue, 29 Dec 2020 16:30:05 +0900 (JST)
Subject: [ruby-changes:64657] f30a3a0629 (master): [ruby/irb] Do not colorize partially-correct inspect
https://git.ruby-lang.org/ruby.git/commit/?id=f30a3a0629 From f30a3a06290387dd19c08b48cc7c31a18b359693 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun <takashikkbn@g...> Date: Mon, 28 Dec 2020 23:20:26 -0800 Subject: [ruby/irb] Do not colorize partially-correct inspect This is to prevent a yellow-mixed output for ActiveSupport::TimeWithZone. Follows up https://github.com/ruby/irb/pull/159 and https://github.com/ruby/ruby/pull/3967. https://github.com/ruby/irb/commit/a5804c3560bb1de3ea8e40002635bff87f6a2825 diff --git a/lib/irb/color.rb b/lib/irb/color.rb index e3f4a1d..eb157be 100644 --- a/lib/irb/color.rb +++ b/lib/irb/color.rb @@ -71,6 +71,9 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/color.rb#L71 end private_constant :TOKEN_SEQ_EXPRS + ERROR_TOKENS = TOKEN_SEQ_EXPRS.keys.select { |k| k.to_s.end_with?('error') } + private_constant :ERROR_TOKENS + class << self def colorable? $stdout.tty? && supported? && (/mswin|mingw/ =~ RUBY_PLATFORM || (ENV.key?('TERM') && ENV['TERM'] != 'dumb')) @@ -119,6 +122,11 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/color.rb#L122 length = 0 scan(code, allow_last_error: !complete) do |token, str, expr| + # IRB::ColorPrinter skips colorizing fragments with any invalid token + if ignore_error && ERROR_TOKENS.include?(token) + return Reline::Unicode.escape_for_print(code) + end + in_symbol = symbol_state.scan_token(token) str.each_line do |line| line = Reline::Unicode.escape_for_print(line) @@ -188,7 +196,7 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/color.rb#L196 end def dispatch_seq(token, expr, str, in_symbol:, ignore_error:) - if token == :on_parse_error or token == :compile_error + if ERROR_TOKENS.include?(token) ignore_error ? nil : TOKEN_SEQ_EXPRS[token][0] elsif in_symbol [YELLOW] -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/