Skip to content

Commit da90c09

Browse files
committed
Reline.delete_text removes the current line in multiline
1 parent 43ac03c commit da90c09

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

lib/reline/line_editor.rb

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,10 +1291,32 @@ def insert_text(text)
12911291

12921292
def delete_text(start = nil, length = nil)
12931293
if start.nil? and length.nil?
1294-
@line&.clear
1295-
@byte_pointer = 0
1296-
@cursor = 0
1297-
@cursor_max = 0
1294+
if @is_multiline
1295+
if @buffer_of_lines.size == 1
1296+
@line&.clear
1297+
@byte_pointer = 0
1298+
@cursor = 0
1299+
@cursor_max = 0
1300+
elsif @line_index == (@buffer_of_lines.size - 1) and @line_index > 0
1301+
@buffer_of_lines.pop
1302+
@line_index -= 1
1303+
@line = @buffer_of_lines[@line_index]
1304+
@byte_pointer = 0
1305+
@cursor = 0
1306+
@cursor_max = calculate_width(@line)
1307+
elsif @line_index < (@buffer_of_lines.size - 1)
1308+
@buffer_of_lines.delete_at(@line_index)
1309+
@line = @buffer_of_lines[@line_index]
1310+
@byte_pointer = 0
1311+
@cursor = 0
1312+
@cursor_max = calculate_width(@line)
1313+
end
1314+
else
1315+
@line&.clear
1316+
@byte_pointer = 0
1317+
@cursor = 0
1318+
@cursor_max = 0
1319+
end
12981320
elsif not start.nil? and not length.nil?
12991321
if @line
13001322
before = @line.byteslice(0, start)

test/reline/test_within_pipe.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,17 @@ def test_macro_commands_for_editing
5959
@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")
6060
assert_equal "a\C-aDE gh Fe", Reline.readmultiline(&proc{ true })
6161
end
62+
63+
def test_delete_text_in_multiline
64+
@writer.write("abc\ndef\nxyz\n")
65+
result = Reline.readmultiline(&proc{ |str|
66+
if str.include?('xyz')
67+
Reline.delete_text
68+
true
69+
else
70+
false
71+
end
72+
})
73+
assert_equal "abc\ndef", result
74+
end
6275
end

0 commit comments

Comments
 (0)