Skip to content

Commit

Permalink
Reline.delete_text removes the current line in multiline
Browse files Browse the repository at this point in the history
  • Loading branch information
aycabta committed Mar 22, 2021
1 parent 43ac03c commit da90c09
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
30 changes: 26 additions & 4 deletions lib/reline/line_editor.rb
Expand Up @@ -1291,10 +1291,32 @@ def insert_text(text)

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)
Expand Down
13 changes: 13 additions & 0 deletions test/reline/test_within_pipe.rb
Expand Up @@ -59,4 +59,17 @@ def test_macro_commands_for_editing
@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

0 comments on commit da90c09

Please sign in to comment.