From c0469a12b9cb27cea219c2ca28a482d77bb5de69 Mon Sep 17 00:00:00 2001 From: tomoya ishida Date: Fri, 12 Jul 2024 21:21:20 +0900 Subject: [PATCH] Fix rendering bug of nomultiline prompt (#730) Fix bug of `print('a'*10); Reline.readline('>')` wrong rendering --- lib/reline.rb | 2 +- lib/reline/line_editor.rb | 8 ++------ test/reline/yamatanooroti/test_rendering.rb | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/reline.rb b/lib/reline.rb index 720df286a1..b851144627 100644 --- a/lib/reline.rb +++ b/lib/reline.rb @@ -331,7 +331,7 @@ def readline(prompt = '', add_hist = false) end end - line_editor.print_nomultiline_prompt(prompt) + line_editor.print_nomultiline_prompt line_editor.update_dialogs line_editor.rerender diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb index 439dbb9727..9c97415050 100644 --- a/lib/reline/line_editor.rb +++ b/lib/reline/line_editor.rb @@ -490,13 +490,9 @@ def render_full_content @output.puts lines.map { |l| "#{l}\r\n" }.join end - def print_nomultiline_prompt(prompt) - return unless prompt && !@is_multiline - + def print_nomultiline_prompt # Readline's test `TestRelineAsReadline#test_readline` requires first output to be prompt, not cursor reset escape sequence. - @rendered_screen.lines = [[[0, Reline::Unicode.calculate_width(prompt, true), prompt]]] - @rendered_screen.cursor_y = 0 - @output.write prompt + @output.write @prompt if @prompt && !@is_multiline end def render_differential diff --git a/test/reline/yamatanooroti/test_rendering.rb b/test/reline/yamatanooroti/test_rendering.rb index 3722a28620..883fcf53fb 100644 --- a/test/reline/yamatanooroti/test_rendering.rb +++ b/test/reline/yamatanooroti/test_rendering.rb @@ -1823,6 +1823,24 @@ def test_exit_with_ctrl_d EOC end + def test_print_before_readline + code = <<~RUBY + puts 'Multiline REPL.' + 2.times do + print 'a' * 10 + Reline.readline '>' + end + RUBY + start_terminal(6, 30, ['ruby', "-I#{@pwd}/lib", '-rreline', '-e', code], startup_message: 'Multiline REPL.') + write "x\n" + close + assert_screen(<<~EOC) + Multiline REPL. + >x + > + EOC + end + def test_thread_safe start_terminal(6, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl --auto-indent}, startup_message: 'Multiline REPL.') write("[Thread.new{Reline.readline'>'},Thread.new{Reline.readmultiline('>'){true}}].map(&:join).size\n")