Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions spec/System/TestCommon_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,22 @@ describe("Common", function()
common.classes.StupidClass = nil
end)
end)
it("WrapString wraps before the word that overflows the width", function()
-- Count visible characters so widths are character counts
local drawStringWidth = _G.DrawStringWidth
_G.DrawStringWidth = function(height, font, text)
return #StripEscapes(text)
end
finally(function()
_G.DrawStringWidth = drawStringWidth
end)
local function wrap(str, width)
return copyTable(main:WrapString(str, 16, width))
end

assert.are.same({ "aa bb", "cc dd" }, wrap("aa bb cc dd", 5))
assert.are.same({ "abc", "de" }, wrap("abc de", 3))
assert.are.same({ "^7abcd", "^xFF0000efgh", "ij" }, wrap("^7abcd ^xFF0000efgh ij", 4))
assert.are.same({ "abcdefgh", "xy" }, wrap("abcdefgh xy", 3))
end)
end)
24 changes: 10 additions & 14 deletions src/Modules/Main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1811,24 +1811,20 @@ do
local wrapTable = { }
function main:WrapString(str, height, width)
wipeTable(wrapTable)
local lineStart = 1
local lastSpace, lastBreak
local lineStart, searchFrom = 1, 1
local previousWordEnd
while true do
local s, e = str:find("%s+", lastSpace)
if not s then
s = #str + 1
e = #str + 1
local spaceStart, spaceEnd = str:find("%s+", searchFrom)
local wordEnd = spaceStart and spaceStart - 1 or #str
if previousWordEnd and previousWordEnd >= lineStart and DrawStringWidth(height, "VAR", str:sub(lineStart, wordEnd)) > width then
t_insert(wrapTable, str:sub(lineStart, previousWordEnd))
lineStart = searchFrom
end
if s > #str then
t_insert(wrapTable, str:sub(lineStart, -1))
if not spaceStart or spaceEnd == #str then
t_insert(wrapTable, str:sub(lineStart, wordEnd))
break
end
lastBreak = s - 1
lastSpace = e + 1
if DrawStringWidth(height, "VAR", str:sub(lineStart, s - 1)) > width then
t_insert(wrapTable, str:sub(lineStart, lastBreak))
lineStart = lastSpace
end
previousWordEnd, searchFrom = wordEnd, spaceEnd + 1
end
return wrapTable
end
Expand Down