Skip to content

Commit 402e3f1

Browse files
committed
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.
1 parent ede1289 commit 402e3f1

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

lib/irb/color.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,22 @@ def inspect_colorable?(obj, seen: {}.compare_by_identity)
101101
end
102102
end
103103

104-
def clear
105-
return '' unless colorable?
104+
def clear(colorable: colorable?)
105+
return '' unless colorable
106106
"\e[#{CLEAR}m"
107107
end
108108

109-
def colorize(text, seq)
110-
return text unless colorable?
109+
def colorize(text, seq, colorable: colorable?)
110+
return text unless colorable
111111
seq = seq.map { |s| "\e[#{const_get(s)}m" }.join('')
112-
"#{seq}#{text}#{clear}"
112+
"#{seq}#{text}#{clear(colorable: colorable)}"
113113
end
114114

115115
# If `complete` is false (code is incomplete), this does not warn compile_error.
116116
# This option is needed to avoid warning a user when the compile_error is happening
117117
# because the input is not wrong but just incomplete.
118-
def colorize_code(code, complete: true, ignore_error: false)
119-
return code unless colorable?
118+
def colorize_code(code, complete: true, ignore_error: false, colorable: colorable?)
119+
return code unless colorable
120120

121121
symbol_state = SymbolState.new
122122
colored = +''
@@ -134,7 +134,7 @@ def colorize_code(code, complete: true, ignore_error: false)
134134
line = Reline::Unicode.escape_for_print(line)
135135
if seq = dispatch_seq(token, expr, line, in_symbol: in_symbol)
136136
colored << seq.map { |s| "\e[#{s}m" }.join('')
137-
colored << line.sub(/\Z/, clear)
137+
colored << line.sub(/\Z/, clear(colorable: colorable))
138138
else
139139
colored << line
140140
end

test/irb/test_color.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def test_colorize
3333
assert_equal_with_term(result, text, seq: seq)
3434

3535
assert_equal_with_term(text, text, seq: seq, tty: false)
36+
assert_equal_with_term(text, text, seq: seq, colorable: false)
37+
assert_equal_with_term(result, text, seq: seq, tty: false, colorable: true)
3638
end
3739
end
3840

@@ -129,6 +131,14 @@ def test_colorize_code
129131

130132
assert_equal_with_term(code, code, complete: true, tty: false)
131133
assert_equal_with_term(code, code, complete: false, tty: false)
134+
135+
assert_equal_with_term(code, code, complete: true, colorable: false)
136+
137+
assert_equal_with_term(code, code, complete: false, colorable: false)
138+
139+
assert_equal_with_term(result, code, complete: true, tty: false, colorable: true)
140+
141+
assert_equal_with_term(result, code, complete: false, tty: false, colorable: true)
132142
else
133143
assert_equal_with_term(code, code)
134144
end
@@ -148,6 +158,10 @@ def test_colorize_code_complete_true
148158
assert_equal_with_term(result, code, complete: true)
149159

150160
assert_equal_with_term(code, code, complete: true, tty: false)
161+
162+
assert_equal_with_term(code, code, complete: true, colorable: false)
163+
164+
assert_equal_with_term(result, code, complete: true, tty: false, colorable: true)
151165
end
152166
end
153167

@@ -162,10 +176,18 @@ def test_colorize_code_complete_false
162176

163177
assert_equal_with_term(code, code, complete: false, tty: false)
164178

179+
assert_equal_with_term(code, code, complete: false, colorable: false)
180+
181+
assert_equal_with_term(result, code, complete: false, tty: false, colorable: true)
182+
165183
unless complete_option_supported?
166184
assert_equal_with_term(result, code, complete: true)
167185

168186
assert_equal_with_term(code, code, complete: true, tty: false)
187+
188+
assert_equal_with_term(code, code, complete: true, colorable: false)
189+
190+
assert_equal_with_term(result, code, complete: true, tty: false, colorable: true)
169191
end
170192
else
171193
assert_equal_with_term(code, code)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy