From 215549117136eae526bc61f4aa71a922fc27b1fc Mon Sep 17 00:00:00 2001 From: Jack Stefani Date: Sun, 4 Oct 2026 20:25:47 -0400 Subject: [PATCH] util: fix infinite loop in trim_string_to_width when width was less than "..." the loop never ended. break out of loop when gsub cannot make progress. note: avoided empty string test in while condition as this misses edge case of unmatchable stray UTF-8 continuation bytes. clarify in ldoc that width is in pixels. fixes #1636 --- lua/lib/util.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lua/lib/util.lua b/lua/lib/util.lua index ca4d7105f..edfd9bd47 100644 --- a/lua/lib/util.lua +++ b/lua/lib/util.lua @@ -96,12 +96,14 @@ end --- trim string to a display width -- @tparam string s string to trim --- @tparam number width maximum width +-- @tparam number width maximum width in pixels -- @treturn string trimmed string util.trim_string_to_width = function(s, width) if _norns.screen_text_extents(s) > width then while _norns.screen_text_extents(s .. "...") > width do - s = string.gsub(s, "[^\128-\191][\128-\191]*$", "") + local n + s, n = string.gsub(s, "[^\128-\191][\128-\191]*$", "") + if n == 0 then break end end s = s .. "..." end