Skip to content

Commit 7481f97

Browse files
committed
optimize wordwrap function
refs lunarmodulesGH-47 integrates Tieske's change for optimizing the wordwrap routine
1 parent 076fc27 commit 7481f97

File tree

3 files changed

+18
-32
lines changed

3 files changed

+18
-32
lines changed

spec/utils/wordwrap_spec.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ describe("utils::wordwrap", function()
77
local expected, result
88

99
result = subject(text, 10)
10-
expected = "123456789\n123456789\n123456789!"
10+
expected = { "123456789", "123456789", "123456789!" }
1111
assert.is.same(result, expected)
1212

1313
-- exact length + 1 overflow
1414
result = subject(text, 9)
15-
expected = "123456789\n123456789\n123456789\n!"
15+
expected = { "123456789", "123456789", "123456789", "!" }
1616
assert.is.same(result, expected)
1717

18-
result = subject(text, 9, nil, true)
19-
expected = "123456789\n123456789\n123456789!"
18+
result = subject(text, 9, true)
19+
expected = { "123456789", "123456789", "123456789!" }
2020
assert.is.same(result, expected)
2121

2222
result = subject(text, 8)
23-
expected = "12345678\n9\n12345678\n9\n12345678\n9!"
23+
expected = { "12345678", "9", "12345678", "9", "12345678", "9!" }
2424
assert.is.same(result, expected)
2525
end)
2626
end)

src/cliargs/printer.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ local function create_printer(get_parser_state)
9292

9393
local function append(label, desc)
9494
label = " " .. label .. string.rep(" ", col1 - (#label + 2))
95-
desc = wordwrap(desc, col2) -- word-wrap
95+
desc = table.concat(wordwrap(desc, col2), "\n") -- word-wrap
9696
desc = desc:gsub("\n", "\n" .. string.rep(" ", col1)) -- add padding
9797

9898
msg = msg .. label .. desc .. "\n"

src/cliargs/utils/wordwrap.lua

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,34 @@ local split = require('cliargs.utils.split')
33
local function buildline(words, size, overflow)
44
-- if overflow is set, a word longer than size, will overflow the size
55
-- otherwise it will be chopped in line-length pieces
6-
local line = ""
7-
if string.len(words[1]) > size then
6+
local line = {}
7+
if #words[1] > size then
88
-- word longer than line
99
if overflow then
10-
line = words[1]
10+
line[1] = words[1]
1111
table.remove(words, 1)
1212
else
13-
line = words[1]:sub(1, size)
13+
line[1] = words[1]:sub(1, size)
1414
words[1] = words[1]:sub(size + 1, -1)
1515
end
1616
else
17-
while words[1] and (#line + string.len(words[1]) + 1 <= size) or (line == "" and #words[1] == size) do
18-
if line == "" then
19-
line = words[1]
20-
else
21-
line = line .. " " .. words[1]
22-
end
17+
local len = 0
18+
while words[1] and (len + #words[1] + 1 <= size) or (len == 0 and #words[1] == size) do
19+
line[#line+1] = words[1]
20+
len = len + #words[1] + 1
2321
table.remove(words, 1)
2422
end
2523
end
26-
return line, words
24+
return table.concat(line, " "), words
2725
end
2826

29-
local function wordwrap(str, size, pad, overflow)
27+
local function wordwrap(str, size, overflow)
3028
-- if overflow is set, then words longer than a line will overflow
3129
-- otherwise, they'll be chopped in pieces
32-
pad = pad or 0
33-
34-
local line
35-
local out = ""
36-
local padstr = string.rep(" ", pad)
37-
local words = split(str, ' ')
38-
30+
local out, words = {}, split(str, ' ')
3931
while words[1] do
40-
line, words = buildline(words, size, overflow)
41-
if out == "" then
42-
out = padstr .. line
43-
else
44-
out = out .. "\n" .. padstr .. line
45-
end
32+
out[#out+1], words = buildline(words, size, overflow)
4633
end
47-
4834
return out
4935
end
5036

0 commit comments

Comments
 (0)