Skip to content

Commit

Permalink
Treat prompt correctly when Reline.prompt_proc isn't set
Browse files Browse the repository at this point in the history
  • Loading branch information
aycabta committed Sep 11, 2020
1 parent 0e791b6 commit 9c9ba0e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/reline/line_editor.rb
Expand Up @@ -133,7 +133,7 @@ def reset(prompt = '', encoding:)
if @line_index.zero?
0
else
calculate_height_by_lines(@buffer_of_lines[0..(@line_index - 1)], prompt_list)
calculate_height_by_lines(@buffer_of_lines[0..(@line_index - 1)], prompt_list || prompt)
end
if @prompt_proc
prompt = prompt_list[@line_index]
Expand Down Expand Up @@ -207,10 +207,10 @@ def multiline_off
@is_multiline = false
end

private def calculate_height_by_lines(lines, prompt_list)
private def calculate_height_by_lines(lines, prompt)
result = 0
prompt_list = prompt.is_a?(Array) ? prompt : nil
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, true) + calculate_width(line))
}
Expand Down Expand Up @@ -343,7 +343,7 @@ def rerender
new_lines = whole_lines
end
prompt, prompt_width, prompt_list = check_multiline_prompt(new_lines, prompt)
all_height = calculate_height_by_lines(new_lines, prompt_list)
all_height = calculate_height_by_lines(new_lines, prompt_list || prompt)
diff = all_height - @highest_in_all
move_cursor_down(@highest_in_all - @first_line_started_from - @started_from - 1)
if diff > 0
Expand Down Expand Up @@ -383,7 +383,7 @@ def rerender
if @line_index.zero?
0
else
calculate_height_by_lines(@buffer_of_lines[0..(@line_index - 1)], prompt_list)
calculate_height_by_lines(@buffer_of_lines[0..(@line_index - 1)], prompt_list || prompt)
end
if @prompt_proc
prompt = prompt_list[@line_index]
Expand Down Expand Up @@ -442,7 +442,7 @@ def rerender
if @line_index.zero?
0
else
calculate_height_by_lines(new_buffer[0..(@line_index - 1)], prompt_list)
calculate_height_by_lines(new_buffer[0..(@line_index - 1)], prompt_list || prompt)
end
@started_from = calculate_height_by_width(prompt_width + @cursor) - 1
move_cursor_down(@first_line_started_from + @started_from)
Expand Down
16 changes: 16 additions & 0 deletions test/reline/yamatanooroti/test_rendering.rb
Expand Up @@ -100,6 +100,22 @@ def test_finish_autowrapped_line_in_the_middle_of_lines
EOC
end

def test_finish_autowrapped_line_in_the_middle_of_multilines
start_terminal(30, 16, %W{ruby -I#{@pwd}/lib #{@pwd}/bin/multiline_repl})
sleep 0.5
write("<<~EOM\n ABCDEFG\nEOM\n")
close
assert_screen(<<~'EOC')
Multiline REPL.
prompt> <<~EOM
prompt> ABCDEF
G
prompt> EOM
=> "ABCDEFG\n"
prompt>
EOC
end

def test_prompt
File.open(@inputrc_file, 'w') do |f|
f.write <<~'LINES'
Expand Down

0 comments on commit 9c9ba0e

Please sign in to comment.