ruby-changes:65644
From: aycabta <ko1@a...>
Date: Wed, 24 Mar 2021 15:43:55 +0900 (JST)
Subject: [ruby-changes:65644] 4b33d860e8 (master): [ruby/reline] Reline.delete_text removes the current line in multiline
https://git.ruby-lang.org/ruby.git/commit/?id=4b33d860e8 From 4b33d860e84f0a5efeefbf8a68324801a0215a08 Mon Sep 17 00:00:00 2001 From: aycabta <aycabta@g...> Date: Tue, 23 Mar 2021 00:56:32 +0900 Subject: [ruby/reline] Reline.delete_text removes the current line in multiline https://github.com/ruby/reline/commit/da90c094a1 --- lib/reline/line_editor.rb | 30 ++++++++++++++++++++++++++---- test/reline/test_within_pipe.rb | 13 +++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb index 12a2bde..870b89b 100644 --- a/lib/reline/line_editor.rb +++ b/lib/reline/line_editor.rb @@ -1291,10 +1291,32 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L1291 def delete_text(start = nil, length = nil) if start.nil? and length.nil? - @line&.clear - @byte_pointer = 0 - @cursor = 0 - @cursor_max = 0 + if @is_multiline + if @buffer_of_lines.size == 1 + @line&.clear + @byte_pointer = 0 + @cursor = 0 + @cursor_max = 0 + elsif @line_index == (@buffer_of_lines.size - 1) and @line_index > 0 + @buffer_of_lines.pop + @line_index -= 1 + @line = @buffer_of_lines[@line_index] + @byte_pointer = 0 + @cursor = 0 + @cursor_max = calculate_width(@line) + elsif @line_index < (@buffer_of_lines.size - 1) + @buffer_of_lines.delete_at(@line_index) + @line = @buffer_of_lines[@line_index] + @byte_pointer = 0 + @cursor = 0 + @cursor_max = calculate_width(@line) + end + else + @line&.clear + @byte_pointer = 0 + @cursor = 0 + @cursor_max = 0 + end elsif not start.nil? and not length.nil? if @line before = @line.byteslice(0, start) diff --git a/test/reline/test_within_pipe.rb b/test/reline/test_within_pipe.rb index 4a46c9c..36a2f1e 100644 --- a/test/reline/test_within_pipe.rb +++ b/test/reline/test_within_pipe.rb @@ -59,4 +59,17 @@ class Reline::WithinPipeTest < Reline::TestCase https://github.com/ruby/ruby/blob/trunk/test/reline/test_within_pipe.rb#L59 @writer.write("abcde\C-b\C-b\C-b\C-x\C-d\C-x\C-h\C-x\C-v\C-a\C-f\C-f EF\C-x\C-t gh\C-x\M-t\C-b\C-b\C-b\C-b\C-b\C-b\C-b\C-b\C-x\M-u\C-x\M-l\C-x\M-c\n") assert_equal "a\C-aDE gh Fe", Reline.readmultiline(&proc{ true }) end + + def test_delete_text_in_multiline + @writer.write("abc\ndef\nxyz\n") + result = Reline.readmultiline(&proc{ |str| + if str.include?('xyz') + Reline.delete_text + true + else + false + end + }) + assert_equal "abc\ndef", result + end end -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/