Skip to content

Commit

Permalink
[ruby/reline] Use appropriate dialog height and reduce screen pushup
Browse files Browse the repository at this point in the history
problem
(ruby/reline#542)

* Provide preferred_dialog_height for dialog positioning

* Fix rendering test
  • Loading branch information
tompng authored and matzbot committed May 27, 2023
1 parent bf1bc53 commit 5d137a7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/reline.rb
Expand Up @@ -260,7 +260,7 @@ def get_screen_size
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,
Expand Down
10 changes: 10 additions & 0 deletions lib/reline/line_editor.rb
Expand Up @@ -562,6 +562,16 @@ def screen_width
@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
Expand Down
36 changes: 21 additions & 15 deletions test/reline/yamatanooroti/test_rendering.rb
Expand Up @@ -1365,21 +1365,21 @@ def test_autocomplete_old_dialog_width_greater_than_dialog_width

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> def hoge
prompt>
prompt>
prompt>
prompt>
prompt>
prompt> def hoge
prompt> S
prompt> String
prompt> Struct
prompt> enSymbol
ScriptError
SyntaxError
prompt> enString █
Struct ▀
Symbol
EOC
end

Expand Down Expand Up @@ -1433,30 +1433,35 @@ def test_lines_passed_to_dynamic_prompt

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
Expand All @@ -1474,6 +1479,7 @@ def test_insert_newline_in_the_middle_of_buffer_just_after_dialog
write(" 3")
close
assert_screen(<<~'EOC')
prompt> 3
prompt> end
prompt> end
=> :a
Expand Down

0 comments on commit 5d137a7

Please sign in to comment.