feat(dashboard): optional filter for projects. Closes #798

This commit is contained in:
Folke Lemaitre 2025-10-20 14:48:04 +02:00
parent 341683b572
commit fe88a07d53
No known key found for this signature in database
GPG key ID: 9B52594D560070AB

View file

@ -881,7 +881,7 @@ end
--- try to restore the session and open the picker if the session is not restored. --- try to restore the session and open the picker if the session is not restored.
--- You can customize the behavior by providing a custom action. --- You can customize the behavior by providing a custom action.
--- Use `opts.dirs` to provide a list of directories to use instead of the git roots. --- Use `opts.dirs` to provide a list of directories to use instead of the git roots.
---@param opts? {limit?:number, dirs?:(string[]|fun():string[]), pick?:boolean, session?:boolean, action?:fun(dir)} ---@param opts? {limit?:number, dirs?:(string[]|fun():string[]), pick?:boolean, session?:boolean, action?:fun(dir), filter?:fun(dir:string):boolean?}
function M.sections.projects(opts) function M.sections.projects(opts)
opts = vim.tbl_extend("force", { pick = true, session = true }, opts or {}) opts = vim.tbl_extend("force", { pick = true, session = true }, opts or {})
local limit = opts.limit or 5 local limit = opts.limit or 5
@ -893,9 +893,11 @@ function M.sections.projects(opts)
for file in M.oldfiles() do for file in M.oldfiles() do
local dir = Snacks.git.get_root(file) local dir = Snacks.git.get_root(file)
if dir and not vim.tbl_contains(dirs, dir) then if dir and not vim.tbl_contains(dirs, dir) then
table.insert(dirs, dir) if not opts.filter or opts.filter(dir) then
if #dirs >= limit then table.insert(dirs, dir)
break if #dirs >= limit then
break
end
end end
end end
end end
@ -903,10 +905,11 @@ function M.sections.projects(opts)
local ret = {} ---@type snacks.dashboard.Item[] local ret = {} ---@type snacks.dashboard.Item[]
for _, dir in ipairs(dirs) do for _, dir in ipairs(dirs) do
ret[#ret + 1] = { if not opts.filter or opts.filter(dir) then
file = dir, ret[#ret + 1] = {
icon = "directory", file = dir,
action = function(self) icon = "directory",
action = function(self)
if opts.action then if opts.action then
return opts.action(dir) return opts.action(dir)
end end
@ -923,7 +926,8 @@ function M.sections.projects(opts)
end end
end, end,
autokey = true, autokey = true,
} }
end
end end
return ret return ret
end end