Skip to content

Commit 242f762

Browse files
committed
feat(picker): allow complex titles
fixes #1111
1 parent 70afc42 commit 242f762

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

lua/snacks/picker/core/picker.lua

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -415,17 +415,35 @@ function M:update_titles()
415415
if win.opts.title then
416416
local tpl = win.meta.title_tpl or win.opts.title
417417
win.meta.title_tpl = tpl
418-
local ret = {} ---@type snacks.picker.Text[]
419-
local title = Snacks.picker.util.tpl(tpl, data)
420-
if title:find("{flags}", 1, true) then
421-
title = title:gsub("{flags}", "")
418+
tpl = type(tpl) == "string" and { { tpl, "FloatTitle" } } or tpl
419+
---@cast tpl -string
420+
421+
local has_flags = false
422+
local is_empty = true
423+
local ret = {}
424+
for chunk in ipairs(tpl) do
425+
local text = chunk[1]
426+
if text:find("{flags}", 1, true) then
427+
text = text:gsub("{flags}", "")
428+
has_flags = true
429+
end
430+
text = vim.trim(Snacks.picker.util.tpl(text, data)):gsub("%s+", " ")
431+
if text ~= "" then
432+
is_empty = false
433+
-- HACK: add extra space when last char is non word like an icon
434+
text = text:sub(-1):match("[%w%p]") and text or text .. " "
435+
end
436+
ret[#ret + 1] = {
437+
text,
438+
chunk[2],
439+
}
440+
end
441+
if has_flags then
422442
vim.list_extend(ret, toggles)
423443
end
424-
title = vim.trim(title):gsub("%s+", " ")
425-
if title ~= "" then
426-
-- HACK: add extra space when last char is non word like an icon
427-
title = title:sub(-1):match("[%w%p]") and title or title .. " "
428-
table.insert(ret, 1, { " " .. title .. " ", "FloatTitle" })
444+
if not is_empty then
445+
table.insert(ret, 1, { " ", "FloatTitle" })
446+
table.insert(ret, { " ", "FloatTitle" })
429447
end
430448
win:set_title(ret)
431449
end

0 commit comments

Comments
 (0)