1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[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.

402e3f1907
This commit is contained in:
Nobuyoshi Nakada 2021-04-08 01:01:16 +09:00 committed by git
parent 687ab5dcad
commit 8fdc45c894
2 changed files with 30 additions and 8 deletions

View file

@ -101,22 +101,22 @@ module IRB # :nodoc:
end end
end end
def clear def clear(colorable: colorable?)
return '' unless colorable? return '' unless colorable
"\e[#{CLEAR}m" "\e[#{CLEAR}m"
end end
def colorize(text, seq) def colorize(text, seq, colorable: colorable?)
return text unless colorable? return text unless colorable
seq = seq.map { |s| "\e[#{const_get(s)}m" }.join('') seq = seq.map { |s| "\e[#{const_get(s)}m" }.join('')
"#{seq}#{text}#{clear}" "#{seq}#{text}#{clear(colorable: colorable)}"
end end
# If `complete` is false (code is incomplete), this does not warn compile_error. # 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 # This option is needed to avoid warning a user when the compile_error is happening
# because the input is not wrong but just incomplete. # because the input is not wrong but just incomplete.
def colorize_code(code, complete: true, ignore_error: false) def colorize_code(code, complete: true, ignore_error: false, colorable: colorable?)
return code unless colorable? return code unless colorable
symbol_state = SymbolState.new symbol_state = SymbolState.new
colored = +'' colored = +''
@ -134,7 +134,7 @@ module IRB # :nodoc:
line = Reline::Unicode.escape_for_print(line) line = Reline::Unicode.escape_for_print(line)
if seq = dispatch_seq(token, expr, line, in_symbol: in_symbol) if seq = dispatch_seq(token, expr, line, in_symbol: in_symbol)
colored << seq.map { |s| "\e[#{s}m" }.join('') colored << seq.map { |s| "\e[#{s}m" }.join('')
colored << line.sub(/\Z/, clear) colored << line.sub(/\Z/, clear(colorable: colorable))
else else
colored << line colored << line
end end

View file

@ -33,6 +33,8 @@ module TestIRB
assert_equal_with_term(result, text, seq: seq) 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, 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
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: true, tty: false)
assert_equal_with_term(code, code, complete: false, 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 else
assert_equal_with_term(code, code) assert_equal_with_term(code, code)
end end
@ -148,6 +158,10 @@ module TestIRB
assert_equal_with_term(result, code, complete: true) 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, 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
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, 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? unless complete_option_supported?
assert_equal_with_term(result, code, complete: true) 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, 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
else else
assert_equal_with_term(code, code) assert_equal_with_term(code, code)