[前][次][番号順一覧][スレッド一覧]

ruby-changes:70556

From: aycabta <ko1@a...>
Date: Sat, 25 Dec 2021 00:33:07 +0900 (JST)
Subject: [ruby-changes:70556] c815c0be46 (master): [ruby/reline] Escape newline(s) in dynamic prompt

https://git.ruby-lang.org/ruby.git/commit/?id=c815c0be46

From c815c0be46b83c1c5c50a88cd1452635208fbee4 Mon Sep 17 00:00:00 2001
From: aycabta <aycabta@g...>
Date: Fri, 24 Dec 2021 20:07:55 +0900
Subject: [ruby/reline] Escape newline(s) in dynamic prompt

https://github.com/ruby/reline/commit/9b209ee1ea
---
 lib/reline/line_editor.rb                   |  2 +-
 test/reline/yamatanooroti/multiline_repl    |  8 ++++++++
 test/reline/yamatanooroti/test_rendering.rb | 12 ++++++++++++
 3 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb
index 5968af708bc..90cc4b2db2f 100644
--- a/lib/reline/line_editor.rb
+++ b/lib/reline/line_editor.rb
@@ -121,7 +121,7 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L121
       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
diff --git a/test/reline/yamatanooroti/multiline_repl b/test/reline/yamatanooroti/multiline_repl
index 1f2a0237243..3c47184b8ef 100755
--- a/test/reline/yamatanooroti/multiline_repl
+++ b/test/reline/yamatanooroti/multiline_repl
@@ -27,6 +27,14 @@ opt.on('--broken-dynamic-prompt') { https://github.com/ruby/ruby/blob/trunk/test/reline/yamatanooroti/multiline_repl#L27
 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
 }
diff --git a/test/reline/yamatanooroti/test_rendering.rb b/test/reline/yamatanooroti/test_rendering.rb
index 8ddf95c1ba3..43c24f64125 100644
--- a/test/reline/yamatanooroti/test_rendering.rb
+++ b/test/reline/yamatanooroti/test_rendering.rb
@@ -1241,6 +1241,18 @@ begin https://github.com/ruby/ruby/blob/trunk/test/reline/yamatanooroti/test_rendering.rb#L1241
       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
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]