From ada64aa9e7f1e087b9fd536d9bc512de401f42a0 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Sat, 25 May 2019 07:48:58 -0700 Subject: [PATCH] Clear IRB::Color escape sequence before newline because otherwise prompt and other things could be polluted. --- lib/irb/color.rb | 2 +- test/irb/test_color.rb | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/irb/color.rb b/lib/irb/color.rb index 8cee483261..ad5b203010 100644 --- a/lib/irb/color.rb +++ b/lib/irb/color.rb @@ -82,7 +82,7 @@ module IRB # :nodoc: Ripper.lex(code).each do |(_line, _col), token, str, expr| if seq = dispatch_seq(token, expr, str) Reline::Unicode.escape_for_print(str).each_line do |line| - colored << "#{seq.map { |s| "\e[#{s}m" }.join('')}#{line}#{clear}" + colored << "#{seq.map { |s| "\e[#{s}m" }.join('')}#{line.sub(/\n?\z/, "#{clear}\\0")}" end else colored << Reline::Unicode.escape_for_print(str) diff --git a/test/irb/test_color.rb b/test/irb/test_color.rb index e61e3d3cd8..1d7d74e151 100644 --- a/test/irb/test_color.rb +++ b/test/irb/test_color.rb @@ -32,12 +32,13 @@ module TestIRB '"##@var]"' => "#{RED}\"#{CLEAR}#{RED}##{CLEAR}#{RED}##{CLEAR}@var#{RED}]#{CLEAR}#{RED}\"#{CLEAR}", '"foo#{a} #{b}"' => "#{RED}\"#{CLEAR}#{RED}foo#{CLEAR}#{RED}\#{#{CLEAR}a#{RED}}#{CLEAR}#{RED} #{CLEAR}#{RED}\#{#{CLEAR}b#{RED}}#{CLEAR}#{RED}\"#{CLEAR}", '/r#{e}g/' => "#{RED}#{BOLD}/#{CLEAR}#{RED}r#{CLEAR}#{RED}\#{#{CLEAR}e#{RED}}#{CLEAR}#{RED}g#{CLEAR}#{RED}#{BOLD}/#{CLEAR}", - "'a\nb'" => "#{RED}'#{CLEAR}#{RED}a\n#{CLEAR}#{RED}b#{CLEAR}#{RED}'#{CLEAR}", + "'a\nb'" => "#{RED}'#{CLEAR}#{RED}a#{CLEAR}\n#{RED}b#{CLEAR}#{RED}'#{CLEAR}", "4.5.6" => "4.5.6", "[1]]]" => "[1]]]", "\e[0m\n" => "^[[#{BLUE}#{BOLD}0#{CLEAR}m\n", }.each do |code, result| - assert_equal(result, with_term { IRB::Color.colorize_code(code) }, "Case: colorize_code(#{code.dump})") + actual = with_term { IRB::Color.colorize_code(code) } + assert_equal(result, actual, "Case: colorize_code(#{code.dump})\nResult: #{humanized_literal(actual)}") end end @@ -75,5 +76,18 @@ module TestIRB $stdout = stdout ENV.replace(env) if env end + + def humanized_literal(str) + str + .gsub(CLEAR, '@@@{CLEAR}') + .gsub(BOLD, '@@@{BOLD}') + .gsub(UNDERLINE, '@@@{UNDERLINE}') + .gsub(RED, '@@@{RED}') + .gsub(GREEN, '@@@{GREEN}') + .gsub(BLUE, '@@@{BLUE}') + .gsub(MAGENTA, '@@@{MAGENTA}') + .gsub(CYAN, '@@@{CYAN}') + .dump.gsub(/@@@/, '#') + end end end