Skip to content

Commit 9b209ee

Browse files
committed
Escape newline(s) in dynamic prompt
1 parent b545459 commit 9b209ee

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

lib/reline/line_editor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def simplified_rendering?
121121
if use_cached_prompt_list
122122
prompt_list = @cached_prompt_list
123123
else
124-
prompt_list = @cached_prompt_list = @prompt_proc.(buffer)
124+
prompt_list = @cached_prompt_list = @prompt_proc.(buffer).map { |pr| pr.gsub("\n", "\\n") }
125125
@prompt_cache_time = Time.now.to_f
126126
end
127127
prompt_list.map!{ prompt } if @vi_arg or @searching_prompt

test/reline/yamatanooroti/multiline_repl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ opt.on('--broken-dynamic-prompt') {
2727
opt.on('--dynamic-prompt-returns-empty') {
2828
Reline.prompt_proc = proc { |l| [] }
2929
}
30+
opt.on('--dynamic-prompt-with-newline') {
31+
Reline.prompt_proc = proc { |lines|
32+
range = lines.size > 1 ? (0..(lines.size - 2)) : (0..0)
33+
lines[range].each_with_index.map { |l, i|
34+
'[%04d\n]> ' % i
35+
}
36+
}
37+
}
3038
opt.on('--auto-indent') {
3139
AutoIndent.new
3240
}

test/reline/yamatanooroti/test_rendering.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,18 @@ def test_prompt_with_newline
12411241
EOC
12421242
end
12431243

1244+
def test_dynamic_prompt_with_newline
1245+
start_terminal(5, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl --dynamic-prompt-with-newline}, startup_message: 'Multiline REPL.')
1246+
write("def hoge\n 3\nend")
1247+
close
1248+
assert_screen(<<~'EOC')
1249+
Multiline REPL.
1250+
[0000\n]> def hoge
1251+
[0001\n]> 3
1252+
[0001\n]> end
1253+
EOC
1254+
end
1255+
12441256
def write_inputrc(content)
12451257
File.open(@inputrc_file, 'w') do |f|
12461258
f.write content

0 commit comments

Comments
 (0)