[前][次][番号順一覧][スレッド一覧]

ruby-changes:55832

From: Takashi <ko1@a...>
Date: Sat, 25 May 2019 23:14:37 +0900 (JST)
Subject: [ruby-changes:55832] Takashi Kokubun: e2db9f4cc3 (trunk): Add and use Reline::Unicode.escape_for_print

https://git.ruby-lang.org/ruby.git/commit/?id=e2db9f4cc3

From e2db9f4cc35dacdca1c0d1f1ae79772dae093c34 Mon Sep 17 00:00:00 2001
From: Takashi Kokubun <takashikkbn@g...>
Date: Sat, 25 May 2019 06:52:10 -0700
Subject: Add and use Reline::Unicode.escape_for_print


diff --git a/lib/irb/color.rb b/lib/irb/color.rb
index 30a8fb5..8cee483 100644
--- a/lib/irb/color.rb
+++ b/lib/irb/color.rb
@@ -1,4 +1,5 @@ https://github.com/ruby/ruby/blob/trunk/lib/irb/color.rb#L1
 # frozen_string_literal: true
+require 'reline'
 require 'ripper'
 
 module IRB # :nodoc:
@@ -80,11 +81,11 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/color.rb#L81
         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
diff --git a/lib/irb/input-method.rb b/lib/irb/input-method.rb
index 9dd8ca2..64b1e01 100644
--- a/lib/irb/input-method.rb
+++ b/lib/irb/input-method.rb
@@ -222,12 +222,17 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/input-method.rb#L222
       end
       Reline.completion_append_character = nil
       Reline.completion_proc = IRB::InputCompletor::CompletionProc
-      if IRB.conf[:USE_COLORIZE]
-        Reline.output_modifier_proc = proc do |output|
-          next unless IRB::Color.colorable?
-          IRB::Color.colorize_code(output)
+      Reline.output_modifier_proc =
+        if IRB.conf[:USE_COLORIZE]
+          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
-      end
       Reline.dig_perfect_match_proc = IRB::InputCompletor::PerfectMatchedProc
     end
 
diff --git a/lib/reline/unicode.rb b/lib/reline/unicode.rb
index bdb182f..b12c204 100644
--- a/lib/reline/unicode.rb
+++ b/lib/reline/unicode.rb
@@ -55,6 +55,17 @@ class Reline::Unicode https://github.com/ruby/ruby/blob/trunk/lib/reline/unicode.rb#L55
     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, ^[, ...
diff --git a/test/irb/test_color.rb b/test/irb/test_color.rb
index 61b4ea4..e61e3d3 100644
--- a/test/irb/test_color.rb
+++ b/test/irb/test_color.rb
@@ -35,6 +35,7 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_color.rb#L35
         "'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
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]