Fix folder path truncation in filemanager with utf8 chars (#3599)

pull/3605/head
Robert 7 years ago committed by poire-z
parent d163f8281d
commit d5d12998f6

@ -48,13 +48,12 @@ end
local function truncatePath(text)
local screen_width = Screen:getWidth()
local face = Font:getFace("xx_smallinfofont")
local reversed_text = string.reverse(text)
local txt_width = RenderText:sizeUtf8Text(0, screen_width, face, reversed_text, nil, false).x
-- we want to truncate text on the left, so work with the reverse of text (which is fine as we don't use kerning)
local reversed_text = require("util").utf8Reverse(text)
local txt_width = RenderText:sizeUtf8Text(0, screen_width, face, reversed_text, false, false).x
if screen_width - 2 * Size.padding.small < txt_width then
reversed_text = RenderText:truncateTextByWidth(reversed_text, face, screen_width - 2 * Size.padding.small, nil, false)
-- string.reverse() breaks "…" so we delete it
reversed_text = reversed_text:sub(1, -5)
text = "" .. string.reverse(reversed_text)
reversed_text = RenderText:truncateTextByWidth(reversed_text, face, screen_width - 2 * Size.padding.small, false, false)
text = require("util").utf8Reverse(reversed_text)
end
return text
end

@ -129,6 +129,13 @@ function util.lastIndexOf(string, ch)
if i == nil then return -1 else return i - 1 end
end
--- Reverse the individual greater-than-single-byte characters
-- @string string to reverse
-- Taken from https://github.com/blitmap/lua-utf8-simple#utf8reverses
function util.utf8Reverse(text)
text = text:gsub('[%z\1-\127\194-\244][\128-\191]*', function (c) return #c > 1 and c:reverse() end)
return text:reverse()
end
--- Splits string into a list of UTF-8 characters.
---- @string text the string to be split.

Loading…
Cancel
Save