Skip to content

Commit

Permalink
Merge pull request #408 from aycabta/escape-newlines-in-prompt
Browse files Browse the repository at this point in the history
Escape newlines in prompt
  • Loading branch information
aycabta authored Dec 24, 2021
2 parents af75ccf + 9b209ee commit 380eab8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
26 changes: 13 additions & 13 deletions lib/reline/line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def simplified_rendering?
mode_string
end

private def check_multiline_prompt(buffer, prompt)
private def check_multiline_prompt(buffer)
if @vi_arg
prompt = "(arg: #{@vi_arg}) "
@rerender_all = true
Expand Down Expand Up @@ -121,7 +121,7 @@ def simplified_rendering?
if use_cached_prompt_list
prompt_list = @cached_prompt_list
else
prompt_list = @cached_prompt_list = @prompt_proc.(buffer)
prompt_list = @cached_prompt_list = @prompt_proc.(buffer).map { |pr| pr.gsub("\n", "\\n") }
@prompt_cache_time = Time.now.to_f
end
prompt_list.map!{ prompt } if @vi_arg or @searching_prompt
Expand Down Expand Up @@ -190,7 +190,7 @@ def resize
else
back = 0
new_buffer = whole_lines
prompt, prompt_width, prompt_list = check_multiline_prompt(new_buffer, prompt)
prompt, prompt_width, prompt_list = check_multiline_prompt(new_buffer)
new_buffer.each_with_index do |line, index|
prompt_width = calculate_width(prompt_list[index], true) if @prompt_proc
width = prompt_width + calculate_width(line)
Expand Down Expand Up @@ -260,7 +260,7 @@ def eof?
end

def reset_variables(prompt = '', encoding:)
@prompt = prompt
@prompt = prompt.gsub("\n", "\\n")
@mark_pointer = nil
@encoding = encoding
@is_multiline = false
Expand Down Expand Up @@ -438,7 +438,7 @@ def rerender
show_menu
@menu_info = nil
end
prompt, prompt_width, prompt_list = check_multiline_prompt(whole_lines, prompt)
prompt, prompt_width, prompt_list = check_multiline_prompt(whole_lines)
if @cleared
clear_screen_buffer(prompt, prompt_list, prompt_width)
@cleared = false
Expand All @@ -449,7 +449,7 @@ def rerender
Reline::IOGate.move_cursor_up(@first_line_started_from + @started_from - @scroll_partial_screen)
Reline::IOGate.move_cursor_column(0)
@scroll_partial_screen = nil
prompt, prompt_width, prompt_list = check_multiline_prompt(whole_lines, prompt)
prompt, prompt_width, prompt_list = check_multiline_prompt(whole_lines)
if @previous_line_index
new_lines = whole_lines(index: @previous_line_index, line: @line)
else
Expand Down Expand Up @@ -495,7 +495,7 @@ def rerender
end
line = modify_lines(new_lines)[@line_index]
clear_dialog
prompt, prompt_width, prompt_list = check_multiline_prompt(new_lines, prompt)
prompt, prompt_width, prompt_list = check_multiline_prompt(new_lines)
render_partial(prompt, prompt_width, line, @first_line_started_from)
move_cursor_down(@highest_in_all - (@first_line_started_from + @highest_in_this - 1) - 1)
scroll_down(1)
Expand All @@ -504,7 +504,7 @@ def rerender
else
if not rendered and not @in_pasting
line = modify_lines(whole_lines)[@line_index]
prompt, prompt_width, prompt_list = check_multiline_prompt(whole_lines, prompt)
prompt, prompt_width, prompt_list = check_multiline_prompt(whole_lines)
render_partial(prompt, prompt_width, line, @first_line_started_from)
end
render_dialog((prompt_width + @cursor) % @screen_size.last)
Expand Down Expand Up @@ -783,7 +783,7 @@ def add_dialog_proc(name, p, context = nil)

private def reset_dialog(dialog, old_dialog)
return if dialog.lines_backup.nil? or old_dialog.contents.nil?
prompt, prompt_width, prompt_list = check_multiline_prompt(dialog.lines_backup[:lines], prompt)
prompt, prompt_width, prompt_list = check_multiline_prompt(dialog.lines_backup[:lines])
visual_lines = []
visual_start = nil
dialog.lines_backup[:lines].each_with_index { |l, i|
Expand Down Expand Up @@ -886,7 +886,7 @@ def add_dialog_proc(name, p, context = nil)
private def clear_each_dialog(dialog)
dialog.trap_key = nil
return unless dialog.contents
prompt, prompt_width, prompt_list = check_multiline_prompt(dialog.lines_backup[:lines], prompt)
prompt, prompt_width, prompt_list = check_multiline_prompt(dialog.lines_backup[:lines])
visual_lines = []
visual_lines_under_dialog = []
visual_start = nil
Expand Down Expand Up @@ -971,7 +971,7 @@ def add_dialog_proc(name, p, context = nil)
end

def just_move_cursor
prompt, prompt_width, prompt_list = check_multiline_prompt(@buffer_of_lines, prompt)
prompt, prompt_width, prompt_list = check_multiline_prompt(@buffer_of_lines)
move_cursor_up(@started_from)
new_first_line_started_from =
if @line_index.zero?
Expand Down Expand Up @@ -1008,7 +1008,7 @@ def just_move_cursor
else
new_lines = whole_lines
end
prompt, prompt_width, prompt_list = check_multiline_prompt(new_lines, prompt)
prompt, prompt_width, prompt_list = check_multiline_prompt(new_lines)
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)
Expand Down Expand Up @@ -1055,7 +1055,7 @@ def just_move_cursor
Reline::IOGate.move_cursor_column(0)
back = 0
new_buffer = whole_lines
prompt, prompt_width, prompt_list = check_multiline_prompt(new_buffer, prompt)
prompt, prompt_width, prompt_list = check_multiline_prompt(new_buffer)
new_buffer.each_with_index do |line, index|
prompt_width = calculate_width(prompt_list[index], true) if @prompt_proc
width = prompt_width + calculate_width(line)
Expand Down
8 changes: 8 additions & 0 deletions test/reline/yamatanooroti/multiline_repl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ opt.on('--broken-dynamic-prompt') {
opt.on('--dynamic-prompt-returns-empty') {
Reline.prompt_proc = proc { |l| [] }
}
opt.on('--dynamic-prompt-with-newline') {
Reline.prompt_proc = proc { |lines|
range = lines.size > 1 ? (0..(lines.size - 2)) : (0..0)
lines[range].each_with_index.map { |l, i|
'[%04d\n]> ' % i
}
}
}
opt.on('--auto-indent') {
AutoIndent.new
}
Expand Down
25 changes: 25 additions & 0 deletions test/reline/yamatanooroti/test_rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,31 @@ def test_clear_dialog_in_pasting
EOC
end

def test_prompt_with_newline
ENV['RELINE_TEST_PROMPT'] = "::\n> "
start_terminal(5, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl}, startup_message: 'Multiline REPL.')
write("def hoge\n 3\nend")
close
assert_screen(<<~'EOC')
Multiline REPL.
::\n> def hoge
::\n> 3
::\n> end
EOC
end

def test_dynamic_prompt_with_newline
start_terminal(5, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl --dynamic-prompt-with-newline}, startup_message: 'Multiline REPL.')
write("def hoge\n 3\nend")
close
assert_screen(<<~'EOC')
Multiline REPL.
[0000\n]> def hoge
[0001\n]> 3
[0001\n]> end
EOC
end

def write_inputrc(content)
File.open(@inputrc_file, 'w') do |f|
f.write content
Expand Down

0 comments on commit 380eab8

Please sign in to comment.