From 08eee25d28365e4fb2e11432c0535932f586cca3 Mon Sep 17 00:00:00 2001 From: tomoya ishida Date: Thu, 18 Apr 2024 23:46:36 +0900 Subject: [PATCH] Fix % escape in prompt format (#927) --- lib/irb.rb | 4 ++-- test/irb/test_context.rb | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/irb.rb b/lib/irb.rb index f7019ab0e..d6afd6e51 100644 --- a/lib/irb.rb +++ b/lib/irb.rb @@ -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 @@ -1490,7 +1490,7 @@ def format_prompt(format, ltype, indent, line_no) # :nodoc: line_no.to_s end when "%" - "%" + "%" unless $1 end end end diff --git a/test/irb/test_context.rb b/test/irb/test_context.rb index aff4b5b67..cd3f2c8f6 100644 --- a/test/irb/test_context.rb +++ b/test/irb/test_context.rb @@ -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",