Skip to content

Commit

Permalink
[ruby/irb] Fix % escape in prompt format
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng authored and matzbot committed Apr 18, 2024
1 parent 8124049 commit ff599ae
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/irb.rb
Expand Up @@ -1457,7 +1457,7 @@ def truncate_prompt_main(str) # :nodoc:
end

def format_prompt(format, ltype, indent, line_no) # :nodoc:
format.gsub(/%([0-9]+)?([a-zA-Z])/) do
format.gsub(/%([0-9]+)?([a-zA-Z%])/) do
case $2
when "N"
@context.irb_name
Expand Down Expand Up @@ -1490,7 +1490,7 @@ def format_prompt(format, ltype, indent, line_no) # :nodoc:
line_no.to_s
end
when "%"
"%"
"%" unless $1
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/irb/test_context.rb
Expand Up @@ -662,6 +662,14 @@ def main.inspect; raise ArgumentError; end
assert_equal("irb(!ArgumentError)>", irb.send(:format_prompt, 'irb(%M)>', nil, 1, 1))
end

def test_prompt_format
main = 'main'
irb = IRB::Irb.new(IRB::WorkSpace.new(main), TestInputMethod.new)
assert_equal('%% main %m %main %%m >', irb.send(:format_prompt, '%%%% %m %%m %%%m %%%%m %l', '>', 1, 1))
assert_equal('42,%i, 42,%3i,042,%03i', irb.send(:format_prompt, '%i,%%i,%3i,%%3i,%03i,%%03i', nil, 42, 1))
assert_equal('42,%n, 42,%3n,042,%03n', irb.send(:format_prompt, '%n,%%n,%3n,%%3n,%03n,%%03n', nil, 1, 42))
end

def test_lineno
input = TestInputMethod.new([
"\n",
Expand Down

0 comments on commit ff599ae

Please sign in to comment.