From 5d137a7f77ce1b4ca3514565de41fe8a2cf611a4 Mon Sep 17 00:00:00 2001 From: tomoya ishida Date: Sat, 27 May 2023 23:48:45 +0900 Subject: [PATCH] [ruby/reline] Use appropriate dialog height and reduce screen pushup problem (https://github.com/ruby/reline/pull/542) * Provide preferred_dialog_height for dialog positioning * Fix rendering test --- lib/reline.rb | 2 +- lib/reline/line_editor.rb | 10 ++++++ test/reline/yamatanooroti/test_rendering.rb | 40 ++++++++++++--------- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/lib/reline.rb b/lib/reline.rb index a128d7347d..fd70ffc9d0 100644 --- a/lib/reline.rb +++ b/lib/reline.rb @@ -260,7 +260,7 @@ module Reline pos: cursor_pos_to_render, contents: result, scrollbar: true, - height: 15, + height: [15, preferred_dialog_height].min, bg_color: 46, pointer_bg_color: 45, fg_color: 37, diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb index b6304bc040..2e84ca4d0c 100644 --- a/lib/reline/line_editor.rb +++ b/lib/reline/line_editor.rb @@ -562,6 +562,16 @@ class Reline::LineEditor @line_editor.instance_variable_get(:@screen_size).last end + def screen_height + @line_editor.instance_variable_get(:@screen_size).first + end + + def preferred_dialog_height + rest_height = @line_editor.instance_variable_get(:@rest_height) + scroll_partial_screen = @line_editor.instance_variable_get(:@scroll_partial_screen) || 0 + [cursor_pos.y - scroll_partial_screen, rest_height, (screen_height + 6) / 5].max + end + def completion_journey_data @line_editor.instance_variable_get(:@completion_journey_data) end diff --git a/test/reline/yamatanooroti/test_rendering.rb b/test/reline/yamatanooroti/test_rendering.rb index fce8f7474e..1a0f8767ee 100644 --- a/test/reline/yamatanooroti/test_rendering.rb +++ b/test/reline/yamatanooroti/test_rendering.rb @@ -1365,21 +1365,21 @@ begin def test_scroll_at_bottom_for_dialog start_terminal(10, 40, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl --autocomplete}, startup_message: 'Multiline REPL.') - write("\n\n\n\n\n\n") - write("def hoge\n\n\n\n\n\n\nend\C-p\C-p\C-p\C-e") + write("\n\n\n\n\n\n\n\n\n\n\n") + write("def hoge\n\nend\C-p\C-e") write(" S") close assert_screen(<<~'EOC') + prompt> + prompt> + prompt> + prompt> + prompt> prompt> def hoge - prompt> - prompt> - prompt> prompt> S - prompt> String - prompt> Struct - prompt> enSymbol - ScriptError - SyntaxError + prompt> enString █ + Struct ▀ + Symbol EOC end @@ -1433,30 +1433,35 @@ begin def test_clear_dialog_when_just_move_cursor_at_last_line start_terminal(10, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl --autocomplete}, startup_message: 'Multiline REPL.') - write("class A\n 3\nend\n") - write("\C-p\C-p\C-p\C-e\C-hS") + write("class A\n 3\nend\n\n\n") + write("\C-p\C-p\C-e; S") write("\C-n") - write("1") + write(";") close assert_screen(<<~'EOC') prompt> 3 prompt> end => 3 - prompt> class S - prompt> 31 - prompt> end + prompt> + prompt> + prompt> class A + prompt> 3; S + prompt> end; EOC end def test_clear_dialog_when_adding_new_line_to_end_of_buffer start_terminal(10, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl --autocomplete}, startup_message: 'Multiline REPL.') - write("class A\n def a\n 3\n end\nend") + write("class A\n def a\n 3\n 3\n end\nend") write("\n") write("class S") write("\n") write(" 3") close assert_screen(<<~'EOC') + prompt> def a + prompt> 3 + prompt> 3 prompt> end prompt> end => :a @@ -1474,6 +1479,7 @@ begin write(" 3") close assert_screen(<<~'EOC') + prompt> 3 prompt> end prompt> end => :a