Skip to content

Commit 08eee25

Browse files
authored
Fix % escape in prompt format (#927)
1 parent e3f5875 commit 08eee25

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

‎lib/irb.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,7 +1457,7 @@ def truncate_prompt_main(str) # :nodoc:
14571457
end
14581458

14591459
def format_prompt(format, ltype, indent, line_no) # :nodoc:
1460-
format.gsub(/%([0-9]+)?([a-zA-Z])/) do
1460+
format.gsub(/%([0-9]+)?([a-zA-Z%])/) do
14611461
case $2
14621462
when "N"
14631463
@context.irb_name
@@ -1490,7 +1490,7 @@ def format_prompt(format, ltype, indent, line_no) # :nodoc:
14901490
line_no.to_s
14911491
end
14921492
when "%"
1493-
"%"
1493+
"%" unless $1
14941494
end
14951495
end
14961496
end

‎test/irb/test_context.rb‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,14 @@ def main.inspect; raise ArgumentError; end
662662
assert_equal("irb(!ArgumentError)>", irb.send(:format_prompt, 'irb(%M)>', nil, 1, 1))
663663
end
664664

665+
def test_prompt_format
666+
main = 'main'
667+
irb = IRB::Irb.new(IRB::WorkSpace.new(main), TestInputMethod.new)
668+
assert_equal('%% main %m %main %%m >', irb.send(:format_prompt, '%%%% %m %%m %%%m %%%%m %l', '>', 1, 1))
669+
assert_equal('42,%i, 42,%3i,042,%03i', irb.send(:format_prompt, '%i,%%i,%3i,%%3i,%03i,%%03i', nil, 42, 1))
670+
assert_equal('42,%n, 42,%3n,042,%03n', irb.send(:format_prompt, '%n,%%n,%3n,%%3n,%03n,%%03n', nil, 1, 42))
671+
end
672+
665673
def test_lineno
666674
input = TestInputMethod.new([
667675
"\n",

0 commit comments

Comments
 (0)