Skip to content

Commit

Permalink
correctly represent trailing spaces and newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
myk002 committed Jul 1, 2024
1 parent a677340 commit c29c348
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
20 changes: 16 additions & 4 deletions library/lua/dfhack.lua
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,15 @@ function string:trim()
return self:match('^%s*(.-)%s*$')
end

local function insert_wrapped_line(wrapped_text, words, opts)
local wrapped_line = table.concat(words, '')
if not opts.keep_trailing_spaces then
local content, eol = wrapped_line:match('(.-)%s*(\n?)$')
wrapped_line = content .. eol
end
table.insert(wrapped_text, wrapped_line)
end

local function wrap_word(word, width, wrapped_text, words, cur_line_len, opts)
local word_len = #word
-- word fits within the current line
Expand All @@ -632,21 +641,21 @@ local function wrap_word(word, width, wrapped_text, words, cur_line_len, opts)
-- trimmed word fits on the current line and ends it
if cur_line_len + #trimmed_word <= width then
table.insert(words, trimmed_word)
table.insert(wrapped_text, table.concat(words, ''))
insert_wrapped_line(wrapped_text, words, opts)
return {}, 0
end
end
-- word needs to go on the next line, but is not itself longer
-- than the specified width
if word_len <= width then
table.insert(wrapped_text, table.concat(words, ''))
insert_wrapped_line(wrapped_text, words, opts)
return {word}, word_len
end
-- word is too long to fit on one line and needs to be split up
local emitted_chars, trimmed_word_len = 0, #trimmed_word
repeat
if #words > 0 then
table.insert(wrapped_text, table.concat(words, ''))
insert_wrapped_line(wrapped_text, words, opts)
end
local word_frag = word:sub(emitted_chars + 1, emitted_chars + width)
words, cur_line_len = {word_frag}, #word_frag
Expand Down Expand Up @@ -674,7 +683,10 @@ function string:wrap(width, opts)
for word in line:gmatch('%S+%s*') do
words, cur_line_len = wrap_word(word, width, wrapped_text, words, cur_line_len, opts)
end
table.insert(wrapped_text, table.concat(words, ''))
insert_wrapped_line(wrapped_text, words, opts)
end
if self:sub(#self) == '\n' then
table.insert(wrapped_text, '')
end
return opts.return_as_table and wrapped_text or table.concat(wrapped_text, '\n')
end
Expand Down
22 changes: 12 additions & 10 deletions test/library/string.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ end

function test.wrap()
expect.eq('', (''):wrap())
expect.eq(' ', (' '):wrap())
expect.eq('', (' '):wrap())
expect.eq('\n', (' '):wrap(3))
expect.eq('\n', (' '):wrap(3))

Expand All @@ -72,19 +72,20 @@ function test.wrap()

expect.eq('hello world', ('hello world'):wrap(20))
expect.eq('hello world', ('hello world'):wrap(20))
expect.eq(' hello world ', (' hello world '):wrap(20))
expect.eq(' hello world ', (' hello world '):wrap(20))
expect.eq('hello world \nhow are you?',('hello world how are you?'):wrap(12))
expect.eq(' hello world', (' hello world '):wrap(20))
expect.eq(' hello world', (' hello world '):wrap(20))
expect.eq('hello world\nhow are you?',('hello world how are you?'):wrap(12))
expect.eq('hello\nworld', ('hello world'):wrap(5))
expect.eq('hello\nworld\n\n', ('hello world\n'):wrap(5))
expect.eq('hello\nworld', ('hello world'):wrap(5))
expect.eq(' \nhello\nworld', (' hello world '):wrap(5))
expect.eq('hello \nworld', ('hello world'):wrap(8))
expect.eq('hel\nlo \nwor\nld', ('hello world'):wrap(3))
expect.eq('\nhello\nworld', (' hello world '):wrap(5))
expect.eq('hello\nworld', ('hello world'):wrap(8))
expect.eq('hel\nlo\nwor\nld', ('hello world'):wrap(3))
expect.eq('hel\nloo\nwor\nldo', ('helloo worldo'):wrap(3))

expect.eq('hel\nloo\n \nwor\nldo', ('helloo worldo'):wrap(3, {keep_trailing_spaces=true}))

expect.table_eq({'hel', 'lo ', 'wor', 'ld'}, ('hello world'):wrap(3, {return_as_table=true}))
expect.table_eq({'hel', 'lo', 'wor', 'ld'}, ('hello world'):wrap(3, {return_as_table=true}))

expect.error_match('expected width > 0', function() ('somestr'):wrap(0) end)

Expand Down Expand Up @@ -134,8 +135,9 @@ function test.wrap()
'is a ',
'sim\n',
'ple ',
'text'
}, ('this is a sim\nple text'):wrap(7, journal_opts),
'text\n',
''
}, ('this is a sim\nple text\n'):wrap(7, journal_opts),
'take into account existing new line')

expect.table_eq({
Expand Down

0 comments on commit c29c348

Please sign in to comment.