aboutsummaryrefslogtreecommitdiffstats
path: root/lib/reline
diff options
context:
space:
mode:
authoraycabta <aycabta@gmail.com>2019-11-01 23:45:07 +0900
committeraycabta <aycabta@gmail.com>2019-11-02 00:11:15 +0900
commitea97933645ad507c3015c7b2ca17035cf3008f96 (patch)
tree76e0189e236ae63f913de12d8502ccb49b1a074e /lib/reline
parent10c2a085480eb8694750e55a613dbbe3eb22a999 (diff)
downloadruby-ea97933645ad507c3015c7b2ca17035cf3008f96.tar.gz
Use prompt_list to calculate height by lines
Diffstat (limited to 'lib/reline')
-rw-r--r--lib/reline/line_editor.rb26
1 files changed, 14 insertions, 12 deletions
diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb
index 0e3544acfe..9ae7e6ee26 100644
--- a/lib/reline/line_editor.rb
+++ b/lib/reline/line_editor.rb
@@ -113,9 +113,7 @@ class Reline::LineEditor
if @line_index.zero?
0
else
- @buffer_of_lines[0..(@line_index - 1)].inject(0) { |result, line|
- result + calculate_height_by_width(prompt_width + calculate_width(line)) # TODO prompt_list
- }
+ calculate_height_by_lines(@buffer_of_lines[0..(@line_index - 1)], prompt_list)
end
if @prompt_proc
prompt = prompt_list[@line_index]
@@ -188,6 +186,16 @@ class Reline::LineEditor
@is_multiline = false
end
+ private def calculate_height_by_lines(lines, prompt_list)
+ result = 0
+ lines.each_with_index { |line, i|
+ prompt = ''
+ prompt = prompt_list[i] if prompt_list and prompt_list[i]
+ result += calculate_height_by_width(calculate_width(prompt + line))
+ }
+ result
+ end
+
private def insert_new_line(cursor_line, next_line)
@line = cursor_line
@buffer_of_lines.insert(@line_index + 1, String.new(next_line, encoding: @encoding))
@@ -346,9 +354,7 @@ class Reline::LineEditor
new_lines = whole_lines
end
prompt, prompt_width, prompt_list = check_multiline_prompt(new_lines, prompt)
- all_height = new_lines.inject(0) { |result, line|
- result + calculate_height_by_width(prompt_width + calculate_width(line)) # TODO prompt_list
- }
+ all_height = calculate_height_by_lines(new_lines, prompt_list)
diff = all_height - @highest_in_all
move_cursor_down(@highest_in_all - @first_line_started_from - @started_from - 1)
if diff > 0
@@ -388,9 +394,7 @@ class Reline::LineEditor
if @line_index.zero?
0
else
- @buffer_of_lines[0..(@line_index - 1)].inject(0) { |result, line|
- result + calculate_height_by_width(prompt_width + calculate_width(line)) # TODO prompt_list
- }
+ calculate_height_by_lines(@buffer_of_lines[0..(@line_index - 1)], prompt_list)
end
if @prompt_proc
prompt = prompt_list[@line_index]
@@ -449,9 +453,7 @@ class Reline::LineEditor
if @line_index.zero?
0
else
- new_buffer[0..(@line_index - 1)].inject(0) { |result, line|
- result + calculate_height_by_width(prompt_width + calculate_width(line)) # TODO prompt_list
- }
+ calculate_height_by_lines(new_buffer[0..(@line_index - 1)], prompt_list)
end
@started_from = calculate_height_by_width(prompt_width + @cursor) - 1
move_cursor_down(@first_line_started_from + @started_from)