Skip to content

Commit

Permalink
Merge pull request #143 from dyphire/utf8
Browse files Browse the repository at this point in the history
abbreviate title based on utf8 characters
  • Loading branch information
jonniek authored Jul 26, 2024
2 parents c315e3c + 69dfb0b commit 7d95e23
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions playlistmanager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,28 @@ local filename_replace_functions = {
hex_to_char = function(x) return string.char(tonumber(x, 16)) 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)
if i > j then
return s
end

local t = {}
local idx = 1
for char in s:gmatch(UTF8_PATTERN) do
if i <= idx and idx <= j then
local width = #char > 2 and 2 or 1
idx = idx + width
t[#t + 1] = char
end
end
return table.concat(t)
end

--strip a filename based on its extension or protocol according to rules in settings
function stripfilename(pathfile, media_title)
if pathfile == nil then return '' end
Expand All @@ -541,8 +563,9 @@ function stripfilename(pathfile, media_title)
end
end
end
if settings.slice_longfilenames and tmp:len()>settings.slice_longfilenames_amount+5 then
tmp = tmp:sub(1, settings.slice_longfilenames_amount).." ..."
local tmp_clip = utf8_sub(tmp, 1, settings.slice_longfilenames_amount)
if tmp ~= tmp_clip then
tmp = tmp_clip .. "..."
end
return tmp
end
Expand Down

0 comments on commit 7d95e23

Please sign in to comment.