Skip to content

Commit

Permalink
abbreviate title based on utf8 characters
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Jan 15, 2024
1 parent 98c6a49 commit 457bb5f
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lua/dyn_menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,27 @@ local function escape_codec(str)
else return str end
end

-- from http://lua-users.org/wiki/LuaUnicode
local UTF8_PATTERN = '[%z\1-\127\194-\244][\128-\191]*'

-- return a substring based on utf8 characters
-- like string.sub, but negative index is not supported
local function utf8_sub(s, i, j)
local t = {}
local idx = 1
for match in s:gmatch(UTF8_PATTERN) do
if j and idx > j then break end
if idx >= i then t[#t + 1] = match end
idx = idx + 1
end
return table.concat(t)
end

-- abbreviate title if it's too long
local function abbr_title(str)
if not str or str == '' then return '' end
if o.max_title_length > 0 and str:len() > o.max_title_length then
return str:sub(1, o.max_title_length) .. '...'
return utf8_sub(str, 1, o.max_title_length) .. '...'
end
return str
end
Expand Down

0 comments on commit 457bb5f

Please sign in to comment.