Skip to content

Commit

Permalink
[ruby/irb] Added colorable keyword option
Browse files Browse the repository at this point in the history
Currently `IRB::Color.colorize` and `IRB::Color.colorize_code`
refer `$stdin.tty?` internally.
This patch adds `colorable` keyword option which overrides it.

ruby/irb@402e3f1907
  • Loading branch information
nobu authored and matzbot committed Apr 26, 2021
1 parent 687ab5d commit 8fdc45c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/irb/color.rb
Expand Up @@ -101,22 +101,22 @@ def inspect_colorable?(obj, seen: {}.compare_by_identity)
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 = +''
Expand All @@ -134,7 +134,7 @@ def colorize_code(code, complete: true, ignore_error: false)
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
Expand Down
22 changes: 22 additions & 0 deletions test/irb/test_color.rb
Expand Up @@ -33,6 +33,8 @@ def test_colorize
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

Expand Down Expand Up @@ -129,6 +131,14 @@ def test_colorize_code

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
Expand All @@ -148,6 +158,10 @@ def test_colorize_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, colorable: false)

assert_equal_with_term(result, code, complete: true, tty: false, colorable: true)
end
end

Expand All @@ -162,10 +176,18 @@ def test_colorize_code_complete_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?
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)
Expand Down

0 comments on commit 8fdc45c

Please sign in to comment.