summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2021-04-08 01:01:16 +0900
committergit <svn-admin@ruby-lang.org>2021-04-26 21:14:52 +0900
commit8fdc45c8941da7559eb61666284c38b7f72ccfbf (patch)
tree4c0d5ca78dd559d8b76a7c3c643b565e14eb4e79
parent687ab5dcad7bb8069776330ca6a62640290d14f5 (diff)
[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
-rw-r--r--lib/irb/color.rb16
-rw-r--r--test/irb/test_color.rb22
2 files changed, 30 insertions, 8 deletions
diff --git a/lib/irb/color.rb b/lib/irb/color.rb
index fce4d53191..40e9e04c97 100644
--- a/lib/irb/color.rb
+++ b/lib/irb/color.rb
@@ -101,22 +101,22 @@ module IRB # :nodoc:
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:
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 29725c530e..ba99b1253f 100644
--- a/test/irb/test_color.rb
+++ b/test/irb/test_color.rb
@@ -33,6 +33,8 @@ module TestIRB
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
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
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
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)