From d834bd0303853f259fc387aa1b7c2be8b9bf6735 Mon Sep 17 00:00:00 2001 From: glenvt18 Date: Tue, 7 Apr 2020 23:47:49 +0300 Subject: [PATCH] Fix client title truncation Truncate at a UTF-8 character boundary. --- init.lua | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/init.lua b/init.lua index 3a1843c..d7c5979 100644 --- a/init.lua +++ b/init.lua @@ -275,14 +275,27 @@ local get_object_name = function (o) end cyclefocus.get_object_name = get_object_name +local utf8_truncate = function (s, length) + if length == 0 then + return s + end + local n = 0 + for i = 1, s:len() do + local b = s:byte(i) + if b < 0x80 or b >= 0xc0 then + n = n + 1 + if n > length then + return s:sub(1, i - 1) .. '…' + end + end + end + return s +end cyclefocus.get_client_title = function (c, current) --luacheck: no unused args -- Use get_object_name to handle .name=nil. local title = cyclefocus.get_object_name(c) - if #title > 80 then - title = title:sub(1, 80) .. '…' - end - return title + return utf8_truncate(title, 80) end -- }}}