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

ruby-changes:65967

From: Nobuyoshi <ko1@a...>
Date: Mon, 26 Apr 2021 21:15:06 +0900 (JST)
Subject: [ruby-changes:65967] 8fdc45c894 (master): [ruby/irb] Added `colorable` keyword option

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

From 8fdc45c8941da7559eb61666284c38b7f72ccfbf Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Thu, 8 Apr 2021 01:01:16 +0900
Subject: [ruby/irb] Added `colorable` keyword option

Currently `IRB::Color.colorize` and `IRB::Color.colorize_code`
refer `$stdin.tty?` internally.
This patch adds `colorable` keyword option which overrides it.

https://github.com/ruby/irb/commit/402e3f1907
---
 lib/irb/color.rb       | 16 ++++++++--------
 test/irb/test_color.rb | 22 ++++++++++++++++++++++
 2 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/lib/irb/color.rb b/lib/irb/color.rb
index fce4d53..40e9e04 100644
--- a/lib/irb/color.rb
+++ b/lib/irb/color.rb
@@ -101,22 +101,22 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/color.rb#L101
         end
       end
 
-      def clear
-        return '' unless colorable?
+      def clear(colorable: colorable?)
+        return '' unless colorable
         "\e[#{CLEAR}m"
       end
 
-      def colorize(text, seq)
-        return text unless colorable?
+      def colorize(text, seq, colorable: colorable?)
+        return text unless colorable
         seq = seq.map { |s| "\e[#{const_get(s)}m" }.join('')
-        "#{seq}#{text}#{clear}"
+        "#{seq}#{text}#{clear(colorable: colorable)}"
       end
 
       # If `complete` is false (code is incomplete), this does not warn compile_error.
       # This option is needed to avoid warning a user when the compile_error is happening
       # because the input is not wrong but just incomplete.
-      def colorize_code(code, complete: true, ignore_error: false)
-        return code unless colorable?
+      def colorize_code(code, complete: true, ignore_error: false, colorable: colorable?)
+        return code unless colorable
 
         symbol_state = SymbolState.new
         colored = +''
@@ -134,7 +134,7 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/color.rb#L134
             line = Reline::Unicode.escape_for_print(line)
             if seq = dispatch_seq(token, expr, line, in_symbol: in_symbol)
               colored << seq.map { |s| "\e[#{s}m" }.join('')
-              colored << line.sub(/\Z/, clear)
+              colored << line.sub(/\Z/, clear(colorable: colorable))
             else
               colored << line
             end
diff --git a/test/irb/test_color.rb b/test/irb/test_color.rb
index 29725c5..ba99b12 100644
--- a/test/irb/test_color.rb
+++ b/test/irb/test_color.rb
@@ -33,6 +33,8 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_color.rb#L33
         assert_equal_with_term(result, text, seq: seq)
 
         assert_equal_with_term(text, text, seq: seq, tty: false)
+        assert_equal_with_term(text, text, seq: seq, colorable: false)
+        assert_equal_with_term(result, text, seq: seq, tty: false, colorable: true)
       end
     end
 
@@ -129,6 +131,14 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_color.rb#L131
 
           assert_equal_with_term(code, code, complete: true, tty: false)
           assert_equal_with_term(code, code, complete: false, tty: false)
+
+          assert_equal_with_term(code, code, complete: true, colorable: false)
+
+          assert_equal_with_term(code, code, complete: false, colorable: false)
+
+          assert_equal_with_term(result, code, complete: true, tty: false, colorable: true)
+
+          assert_equal_with_term(result, code, complete: false, tty: false, colorable: true)
         else
           assert_equal_with_term(code, code)
         end
@@ -148,6 +158,10 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_color.rb#L158
         assert_equal_with_term(result, code, complete: true)
 
         assert_equal_with_term(code, code, complete: true, tty: false)
+
+        assert_equal_with_term(code, code, complete: true, colorable: false)
+
+        assert_equal_with_term(result, code, complete: true, tty: false, colorable: true)
       end
     end
 
@@ -162,10 +176,18 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_color.rb#L176
 
           assert_equal_with_term(code, code, complete: false, tty: false)
 
+          assert_equal_with_term(code, code, complete: false, colorable: false)
+
+          assert_equal_with_term(result, code, complete: false, tty: false, colorable: true)
+
           unless complete_option_supported?
             assert_equal_with_term(result, code, complete: true)
 
             assert_equal_with_term(code, code, complete: true, tty: false)
+
+            assert_equal_with_term(code, code, complete: true, colorable: false)
+
+            assert_equal_with_term(result, code, complete: true, tty: false, colorable: true)
           end
         else
           assert_equal_with_term(code, code)
-- 
cgit v1.1


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

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