Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colorize command input #983

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/irb/input-method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,13 @@ def initialize(completor)
proc do |output, complete: |
next unless IRB::Color.colorable?
lvars = IRB.CurrentContext&.local_variables || []
IRB::Color.colorize_code(output, complete: complete, local_variables: lvars)
if IRB.CurrentContext&.parse_command(output)
name, sep, arg = output.split(/(\s+)/, 2)
arg = IRB::Color.colorize_code(arg, complete: complete, local_variables: lvars)
"#{IRB::Color.colorize(name, [:BOLD])}\e[m#{sep}#{arg}"
else
IRB::Color.colorize_code(output, complete: complete, local_variables: lvars)
end
end
else
proc do |output|
Expand Down
16 changes: 16 additions & 0 deletions test/irb/test_input_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module TestIRB
class InputMethodTest < TestCase
def setup
@conf_backup = IRB.conf.dup
IRB.init_config(nil)
IRB.conf[:LC_MESSAGES] = IRB::Locale.new
save_encodings
end
Expand All @@ -33,6 +34,21 @@ def test_initialization
assert_not_nil Reline.dig_perfect_match_proc
end

def test_colorize
original_colorable = IRB::Color.method(:colorable?)
IRB::Color.instance_eval { undef :colorable? }
IRB::Color.define_singleton_method(:colorable?) { true }
workspace = IRB::WorkSpace.new(binding)
input_method = IRB::RelineInputMethod.new(IRB::RegexpCompletor.new)
IRB.conf[:MAIN_CONTEXT] = IRB::Irb.new(workspace, input_method).context
assert_equal "\e[1m$\e[0m\e[m", Reline.output_modifier_proc.call('$', complete: false)
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)
assert_equal "\e[32m\e[1m$a\e[0m", Reline.output_modifier_proc.call('$a', complete: false)
ensure
IRB::Color.instance_eval { undef :colorable? }
IRB::Color.define_singleton_method(:colorable?, original_colorable)
end

def test_initialization_without_use_autocomplete
original_show_doc_proc = Reline.dialog_proc(:show_doc)&.dialog_proc
empty_proc = Proc.new {}
Expand Down
Loading