mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Clear IRB::Color escape sequence before newline
because otherwise prompt and other things could be polluted.
This commit is contained in:
parent
7c507345a7
commit
ada64aa9e7
2 changed files with 17 additions and 3 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue