Skip to content

Commit

Permalink
Fix client title truncation (#39)
Browse files Browse the repository at this point in the history
Truncate at a UTF-8 character boundary.
  • Loading branch information
glenvt18 authored Apr 8, 2020
1 parent 1ba83dd commit d7c8390
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
-- }}}

Expand Down

0 comments on commit d7c8390

Please sign in to comment.