diff --git a/library/lua/dfhack.lua b/library/lua/dfhack.lua index 671029015..e476ffa11 100644 --- a/library/lua/dfhack.lua +++ b/library/lua/dfhack.lua @@ -439,6 +439,7 @@ end -- multiple lines. If width is not specified, 72 is used. function string:wrap(width) width = width or 72 + if width <= 0 then error('expected width > 0; got: '..tostring(width)) end local wrapped_text = {} for line in self:gmatch('[^\n]*') do local line_start_pos = 1 diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index 8a091804c..b8782bd04 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -558,7 +558,8 @@ WrappedLabel.ATTRS{ } function WrappedLabel:getWrappedText(width) - if not self.text_to_wrap then return nil end + -- 0 width can happen if the parent has 0 width + if not self.text_to_wrap or width <= 0 then return nil end local text_to_wrap = getval(self.text_to_wrap) if type(text_to_wrap) == 'table' then text_to_wrap = table.concat(text_to_wrap, NEWLINE) diff --git a/test/library/string.lua b/test/library/string.lua index d45be4b3f..d22f262bf 100644 --- a/test/library/string.lua +++ b/test/library/string.lua @@ -67,6 +67,8 @@ function test.wrap() expect.eq('hel\nlo\nwor\nld', ('hello world'):wrap(3)) expect.eq('hel\nloo\nwor\nldo', ('helloo worldo'):wrap(3)) expect.eq('', (''):wrap()) + + expect.error_match('expected width > 0', function() ('somestr'):wrap(0) end) end function test.escape_pattern()