From 457bb5fa52e00fa4f2f6b5d7c0cd6ea164eda8e7 Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Mon, 15 Jan 2024 12:42:20 +0800 Subject: [PATCH] abbreviate title based on utf8 characters --- lua/dyn_menu.lua | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lua/dyn_menu.lua b/lua/dyn_menu.lua index 72cb0c1..f8af223 100644 --- a/lua/dyn_menu.lua +++ b/lua/dyn_menu.lua @@ -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