fix(picker.input): limit search in statuscolumn to 20 chars

This commit is contained in:
Folke Lemaitre 2025-01-14 14:30:32 +01:00
parent bba68a9631
commit 4c9bcbc8e8
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
2 changed files with 21 additions and 4 deletions

View file

@ -76,6 +76,9 @@ function M:statuscolumn()
end
local pattern = self.picker.opts.live and self.filter.pattern or self.filter.search
if pattern ~= "" then
if #pattern > 20 then
pattern = Snacks.picker.util.truncate(pattern, 20)
end
add(pattern, "SnacksPickerInputSearch")
end
add(self.picker.opts.prompt or "", "SnacksPickerPrompt")

View file

@ -30,17 +30,31 @@ end
---@param text string
---@param width number
---@param opts? {align?: "left" | "right", truncate?: boolean}
---@param opts? {align?: "left" | "right" | "center", truncate?: boolean}
function M.align(text, width, opts)
opts = opts or {}
opts.align = opts.align or "left"
local tw = vim.api.nvim_strwidth(text)
if tw > width then
return opts.truncate and (vim.fn.strcharpart(text, 0, width - 1) .. "") or text
end
if opts.align == "right" then
return (" "):rep(width - tw) .. text
local left = math.floor((width - tw) / 2)
local right = width - tw - left
if opts.align == "left" then
left, right = 0, width - tw
elseif opts.align == "right" then
left, right = width - tw, 0
end
return text .. (" "):rep(width - tw)
return (" "):rep(left) .. text .. (" "):rep(right)
end
---@param text string
---@param width number
function M.truncate(text, width)
if vim.api.nvim_strwidth(text) > width then
return vim.fn.strcharpart(text, 0, width - 1) .. ""
end
return text
end
-- Stops visual mode and returns the selected text