mirror of
https://github.com/folke/snacks.nvim
synced 2025-12-23 08:47:57 +00:00
feat(dashboard): optional filter for projects. Closes #798
This commit is contained in:
parent
341683b572
commit
fe88a07d53
1 changed files with 13 additions and 9 deletions
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue