Skip to content

Commit e4403d4

Browse files
authored
fix(editline): format empty strings (#133)
If string to format is too small it will fail to format it.
1 parent 0932ebf commit e4403d4

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

spec/09-editline-format_spec.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,21 @@ describe("EditLine:", function()
393393
end)
394394

395395

396+
it("formats an empty string", function()
397+
line = EditLine(""):goto_index(0)
398+
local result, row, col = testwrap({
399+
width = 60,
400+
first_width = 60,
401+
wordwrap = false,
402+
pad = false,
403+
pad_last = false
404+
})
405+
assert.are.same({"|"}, result)
406+
assert.are.equal(1, row)
407+
assert.are.equal(1, col)
408+
end)
409+
410+
396411
it("formats a string with padding", function()
397412
assert.are.same({
398413
'Hello, this is a simple 🚀 test string to check the formatti|',
@@ -726,6 +741,21 @@ describe("EditLine:", function()
726741
end)
727742

728743

744+
it("formats an empty string", function()
745+
line = EditLine(""):goto_index(0)
746+
local result, row, col = testwrap({
747+
width = 60,
748+
first_width = 60,
749+
wordwrap = true,
750+
pad = false,
751+
pad_last = false
752+
})
753+
assert.are.same({"|"}, result)
754+
assert.are.equal(1, row)
755+
assert.are.equal(1, col)
756+
end)
757+
758+
729759
it("formats a string with padding", function()
730760
assert.are.same({
731761
'Hello, this is a simple 🚀 test string to check the | ',

src/terminal/editline.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,15 @@ do
578578
self:goto_home()
579579

580580
local size = #self.chars
581-
while self.cursor_idx < size do
581+
if size == 0 then
582+
-- empty line, special case
583+
lines[1] = EditLine("")
584+
line_cols[1] = 0
585+
cur_line = 1
586+
cur_col = width - first_width + 1
587+
end
588+
589+
while self.cursor_idx <= size do
582590
local line, cols = wrapper(self, target_size)
583591
-- print("line:", "'"..tostring(line).."'", cols)
584592
lines[#lines + 1] = line

0 commit comments

Comments
 (0)