console.lua: assume non-ASCII characters span 2 cells for truncation

This prevents line wraps in select mode with terminal output which hide
the top items (OSD output no longer uses truncate_utf8 since the
previous commit). This is not ideal as even accented letters are assumed
to be 2 cells wide. The alternative is using \e[?7l and \e[?7h to
disable and enable text wrapping, but it won't work in Windows console
with VT processing disabled.
This commit is contained in:
Guido Cella 2024-06-24 19:41:59 +02:00 committed by Kacper Michajłow
parent 06cfdc80a6
commit 01330dba71
1 changed files with 8 additions and 0 deletions

View File

@ -154,8 +154,16 @@ local function truncate_utf8(str, max_length)
local len = 0
local pos = 1
while pos <= #str do
local last_pos = pos
pos = next_utf8(str, pos)
len = len + 1
if pos > last_pos + 1 then
if len == max_length - 1 then
pos = prev_utf8(str, pos)
else
len = len + 1
end
end
if len == max_length - 1 then
return str:sub(1, pos - 1) .. ''
end