[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
This commit is contained in:
Nobuyoshi Nakada 2021-04-08 01:01:16 +09:00 коммит произвёл git
Родитель 687ab5dcad
Коммит 8fdc45c894
2 изменённых файлов: 30 добавлений и 8 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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)