From 1cf9f79342c517e3f12ba614ad874b1d619af5bc Mon Sep 17 00:00:00 2001 From: aycabta Date: Thu, 30 May 2019 14:04:14 +0900 Subject: Clear remaining lines when line number decreased --- lib/reline/line_editor.rb | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb index 9b31ad14e5..d6085e3995 100644 --- a/lib/reline/line_editor.rb +++ b/lib/reline/line_editor.rb @@ -330,17 +330,33 @@ class Reline::LineEditor Reline::IOGate.move_cursor_column((prompt_width + @cursor) % @screen_size.last) return end + new_highest_in_this = calculate_height_by_width(@prompt_width + calculate_width(@line.nil? ? '' : @line)) # FIXME: end of logical line sometimes breaks - if @previous_line_index - new_lines = whole_lines(index: @previous_line_index, line: @line) + if @previous_line_index or new_highest_in_this < @highest_in_this + if @previous_line_index + new_lines = whole_lines(index: @previous_line_index, line: @line) + else + new_lines = whole_lines + end all_height = new_lines.inject(0) { |result, line| result + calculate_height_by_width(@prompt_width + calculate_width(line)) } diff = all_height - @highest_in_all move_cursor_down(@highest_in_all - @first_line_started_from - @started_from - 1) + if diff > 0 + scroll_down(diff) + move_cursor_up(all_height - 1) + elsif diff < 0 + (-diff).times do + Reline::IOGate.move_cursor_column(0) + Reline::IOGate.erase_after_cursor + move_cursor_up(1) + end + move_cursor_up(all_height - 1) + else + move_cursor_up(all_height - 1) + end @highest_in_all = all_height - scroll_down(diff) - move_cursor_up(all_height - 1) back = 0 modify_lines(new_lines).each_with_index do |line, index| height = render_partial(prompt, prompt_width, line, false) @@ -350,8 +366,10 @@ class Reline::LineEditor end end move_cursor_up(back) - @buffer_of_lines[@previous_line_index] = @line - @line = @buffer_of_lines[@line_index] + if @previous_line_index + @buffer_of_lines[@previous_line_index] = @line + @line = @buffer_of_lines[@line_index] + end @first_line_started_from = if @line_index.zero? 0 -- cgit v1.2.3