LazyVim.LazyVim/lua/lazyvim/plugins/extras/util/chezmoi.lua
Thomas Vandal 759a19e785
feat(chezmoi): enhance fzf-lua chezmoi picker and add snacks.dasbhoard entry (#5275)
## Description

<!-- Describe the big picture of your changes to communicate to the
maintainers
  why we should accept this pull request. -->

This PR enhances the fzf-lua picker for chezmoi by using the built-in
`files` picker and by opening the selected file with `ChezmoiEdit` and
`vimcmd_entry` to parse the entry. This simplifies the code and adds
icons and file preview.

The PR also replaces the config entry in snacks.dashboard with the
chezmoi picker, similar to what was already implemented for
dashboard-nvim

## Screenshots

<!-- Add screenshots of the changes if applicable. -->

Current picker:


![image](https://github.com/user-attachments/assets/d0be9279-f199-4349-8055-04d8a351c6f9)


New picker:


![image](https://github.com/user-attachments/assets/c68fcb6a-79fa-4f65-8c48-60ce64450350)

## Checklist

- [x] I've read the
[CONTRIBUTING](https://github.com/LazyVim/LazyVim/blob/main/CONTRIBUTING.md)
guidelines.
2025-02-21 14:28:05 +01:00

155 lines
4.1 KiB
Lua

local pick_chezmoi = function()
if LazyVim.pick.picker.name == "telescope" then
require("telescope").extensions.chezmoi.find_files()
elseif LazyVim.pick.picker.name == "fzf" then
local fzf_lua = require("fzf-lua")
local actions = {
["enter"] = function(selected)
fzf_lua.actions.vimcmd_entry("ChezmoiEdit", selected, { cwd = os.getenv("HOME") })
end,
}
fzf_lua.files({ cmd = "chezmoi managed --include=files,symlinks", actions = actions })
elseif LazyVim.pick.picker.name == "snacks" then
local results = require("chezmoi.commands").list({
args = {
"--path-style",
"absolute",
"--include",
"files",
"--exclude",
"externals",
},
})
local items = {}
for _, czFile in ipairs(results) do
table.insert(items, {
text = czFile,
file = czFile,
})
end
---@type snacks.picker.Config
local opts = {
items = items,
confirm = function(picker, item)
picker:close()
require("chezmoi.commands").edit({
targets = { item.text },
args = { "--watch" },
})
end,
}
Snacks.picker.pick(opts)
end
end
return {
{
-- highlighting for chezmoi files template files
"alker0/chezmoi.vim",
init = function()
vim.g["chezmoi#use_tmp_buffer"] = 1
vim.g["chezmoi#source_dir_path"] = os.getenv("HOME") .. "/.local/share/chezmoi"
end,
},
{
"xvzc/chezmoi.nvim",
cmd = { "ChezmoiEdit" },
keys = {
{
"<leader>sz",
pick_chezmoi,
desc = "Chezmoi",
},
},
opts = {
edit = {
watch = false,
force = false,
},
notification = {
on_open = true,
on_apply = true,
on_watch = false,
},
telescope = {
select = { "<CR>" },
},
},
init = function()
-- run chezmoi edit on file enter
vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
pattern = { os.getenv("HOME") .. "/.local/share/chezmoi/*" },
callback = function()
vim.schedule(require("chezmoi.commands.__edit").watch)
end,
})
end,
},
{
"nvimdev/dashboard-nvim",
optional = true,
opts = function(_, opts)
local projects = {
action = pick_chezmoi,
desc = " Config",
icon = "",
key = "c",
}
projects.desc = projects.desc .. string.rep(" ", 43 - #projects.desc)
projects.key_format = " %s"
-- remove lazyvim config property
for i = #opts.config.center, 1, -1 do
if opts.config.center[i].key == "c" then
table.remove(opts.config.center, i)
end
end
table.insert(opts.config.center, 5, projects)
end,
},
{
"folke/snacks.nvim",
optional = true,
opts = function(_, opts)
local chezmoi_entry = {
icon = "",
key = "c",
desc = "Config",
action = pick_chezmoi,
}
local config_index
for i = #opts.dashboard.preset.keys, 1, -1 do
if opts.dashboard.preset.keys[i].key == "c" then
table.remove(opts.dashboard.preset.keys, i)
config_index = i
break
end
end
table.insert(opts.dashboard.preset.keys, config_index, chezmoi_entry)
end,
},
-- Filetype icons
{
"echasnovski/mini.icons",
opts = {
file = {
[".chezmoiignore"] = { glyph = "", hl = "MiniIconsGrey" },
[".chezmoiremove"] = { glyph = "", hl = "MiniIconsGrey" },
[".chezmoiroot"] = { glyph = "", hl = "MiniIconsGrey" },
[".chezmoiversion"] = { glyph = "", hl = "MiniIconsGrey" },
["bash.tmpl"] = { glyph = "", hl = "MiniIconsGrey" },
["json.tmpl"] = { glyph = "", hl = "MiniIconsGrey" },
["ps1.tmpl"] = { glyph = "󰨊", hl = "MiniIconsGrey" },
["sh.tmpl"] = { glyph = "", hl = "MiniIconsGrey" },
["toml.tmpl"] = { glyph = "", hl = "MiniIconsGrey" },
["yaml.tmpl"] = { glyph = "", hl = "MiniIconsGrey" },
["zsh.tmpl"] = { glyph = "", hl = "MiniIconsGrey" },
},
},
},
}