mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Add and use Reline::Unicode.escape_for_print
This commit is contained in:
parent
e691b4da5d
commit
e2db9f4cc3
4 changed files with 25 additions and 7 deletions
|
@ -1,4 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
require 'reline'
|
||||
require 'ripper'
|
||||
|
||||
module IRB # :nodoc:
|
||||
|
@ -80,11 +81,11 @@ module IRB # :nodoc:
|
|||
length = 0
|
||||
Ripper.lex(code).each do |(_line, _col), token, str, expr|
|
||||
if seq = dispatch_seq(token, expr, str)
|
||||
str.each_line do |line|
|
||||
Reline::Unicode.escape_for_print(str).each_line do |line|
|
||||
colored << "#{seq.map { |s| "\e[#{s}m" }.join('')}#{line}#{clear}"
|
||||
end
|
||||
else
|
||||
colored << str
|
||||
colored << Reline::Unicode.escape_for_print(str)
|
||||
end
|
||||
length += str.length
|
||||
end
|
||||
|
|
|
@ -222,11 +222,16 @@ module IRB
|
|||
end
|
||||
Reline.completion_append_character = nil
|
||||
Reline.completion_proc = IRB::InputCompletor::CompletionProc
|
||||
Reline.output_modifier_proc =
|
||||
if IRB.conf[:USE_COLORIZE]
|
||||
Reline.output_modifier_proc = proc do |output|
|
||||
proc do |output|
|
||||
next unless IRB::Color.colorable?
|
||||
IRB::Color.colorize_code(output)
|
||||
end
|
||||
else
|
||||
proc do |output|
|
||||
Reline::Unicode.escape_for_print(output)
|
||||
end
|
||||
end
|
||||
Reline.dig_perfect_match_proc = IRB::InputCompletor::PerfectMatchedProc
|
||||
end
|
||||
|
|
|
@ -55,6 +55,17 @@ class Reline::Unicode
|
|||
end
|
||||
end
|
||||
|
||||
def self.escape_for_print(str)
|
||||
str.chars.map! { |gr|
|
||||
escaped = EscapedPairs[gr.ord]
|
||||
if escaped && gr != "\n"
|
||||
escaped
|
||||
else
|
||||
gr
|
||||
end
|
||||
}.join
|
||||
end
|
||||
|
||||
def self.get_mbchar_width(mbchar)
|
||||
case mbchar.encode(Encoding::UTF_8)
|
||||
when *EscapedChars # ^ + char, such as ^M, ^H, ^[, ...
|
||||
|
|
|
@ -35,6 +35,7 @@ module TestIRB
|
|||
"'a\nb'" => "#{RED}'#{CLEAR}#{RED}a\n#{CLEAR}#{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})")
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue