Skip to content

Commit 0e64136

Browse files
authored
Colorize command input (#983)
1 parent 9a4487a commit 0e64136

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/irb/input-method.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,13 @@ def initialize(completor)
270270
proc do |output, complete: |
271271
next unless IRB::Color.colorable?
272272
lvars = IRB.CurrentContext&.local_variables || []
273-
IRB::Color.colorize_code(output, complete: complete, local_variables: lvars)
273+
if IRB.CurrentContext&.parse_command(output)
274+
name, sep, arg = output.split(/(\s+)/, 2)
275+
arg = IRB::Color.colorize_code(arg, complete: complete, local_variables: lvars)
276+
"#{IRB::Color.colorize(name, [:BOLD])}\e[m#{sep}#{arg}"
277+
else
278+
IRB::Color.colorize_code(output, complete: complete, local_variables: lvars)
279+
end
274280
end
275281
else
276282
proc do |output|

test/irb/test_input_method.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module TestIRB
88
class InputMethodTest < TestCase
99
def setup
1010
@conf_backup = IRB.conf.dup
11+
IRB.init_config(nil)
1112
IRB.conf[:LC_MESSAGES] = IRB::Locale.new
1213
save_encodings
1314
end
@@ -33,6 +34,21 @@ def test_initialization
3334
assert_not_nil Reline.dig_perfect_match_proc
3435
end
3536

37+
def test_colorize
38+
original_colorable = IRB::Color.method(:colorable?)
39+
IRB::Color.instance_eval { undef :colorable? }
40+
IRB::Color.define_singleton_method(:colorable?) { true }
41+
workspace = IRB::WorkSpace.new(binding)
42+
input_method = IRB::RelineInputMethod.new(IRB::RegexpCompletor.new)
43+
IRB.conf[:MAIN_CONTEXT] = IRB::Irb.new(workspace, input_method).context
44+
assert_equal "\e[1m$\e[0m\e[m", Reline.output_modifier_proc.call('$', complete: false)
45+
assert_equal "\e[1m$\e[0m\e[m \e[34m\e[1m1\e[0m + \e[34m\e[1m2\e[0m", Reline.output_modifier_proc.call('$ 1 + 2', complete: false)
46+
assert_equal "\e[32m\e[1m$a\e[0m", Reline.output_modifier_proc.call('$a', complete: false)
47+
ensure
48+
IRB::Color.instance_eval { undef :colorable? }
49+
IRB::Color.define_singleton_method(:colorable?, original_colorable)
50+
end
51+
3652
def test_initialization_without_use_autocomplete
3753
original_show_doc_proc = Reline.dialog_proc(:show_doc)&.dialog_proc
3854
empty_proc = Proc.new {}

0 commit comments

Comments
 (0)