fix: Multi byte truncate for displayer (#464)

This is needed for calling strdisplaywidth() from Lua loop.

See https://github.com/nvim-telescope/telescope.nvim/issues/414
See a1ed941a78/src/nvim/eval/funcs.c (L9845-L9858)

Co-authored-by: Simon Hauser <Simon-Hauser@outlook.de>
This commit is contained in:
JINNOUCHI Yasushi
2021-01-31 18:55:17 +09:00
committed by GitHub
parent 4e0dfa2e70
commit 1ca1e7ccba
4 changed files with 138 additions and 9 deletions

View File

@@ -1,13 +1,27 @@
local utils = require('telescope.utils')
local entry_display = {}
local function truncate(str, len)
entry_display.truncate = function(str, len)
str = tostring(str) -- We need to make sure its an actually a string and not a number
-- TODO: This doesn't handle multi byte chars...
if vim.fn.strdisplaywidth(str) > len then
str = str:sub(1, len - 1)
str = str .. ""
if utils.strdisplaywidth(str) <= len then
return str
end
return str
local charlen = 0
local cur_len = 0
local result = ''
local len_of_dots = utils.strdisplaywidth('')
while true do
local part = utils.strcharpart(str, charlen, 1)
cur_len = cur_len + utils.strdisplaywidth(part)
if (cur_len + len_of_dots) > len then
result = result .. ''
break
end
result = result .. part
charlen = charlen + 1
end
return result
end
entry_display.create = function(configuration)
@@ -18,9 +32,9 @@ entry_display.create = function(configuration)
local format_str = "%" .. justify .. v.width .. "s"
table.insert(generator, function(item)
if type(item) == 'table' then
return string.format(format_str, truncate(item[1], v.width)), item[2]
return string.format(format_str, entry_display.truncate(item[1], v.width)), item[2]
else
return string.format(format_str, truncate(item, v.width))
return string.format(format_str, entry_display.truncate(item, v.width))
end
end)
else