feat(picker.preview): allow passing additional args to the git preview command

This commit is contained in:
Folke Lemaitre 2025-02-11 20:12:24 +01:00
parent 92312295a7
commit 910437f145
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
2 changed files with 15 additions and 22 deletions

View file

@ -163,6 +163,7 @@ local defaults = {
previewers = {
git = {
native = false, -- use native (terminal) or Neovim for previewing git diffs and commits
args = {}, -- additional arguments passed to the git command. Useful to set pager options usin `-c ...`
},
file = {
max_size = 1024 * 1024, -- 1MB

View file

@ -276,13 +276,19 @@ function M.git_show(ctx)
M.cmd(cmd, ctx, { ft = not native and "git" or nil })
end
---@param ctx snacks.picker.preview.ctx
local function git(ctx, ...)
local ret = { "git", "-c", "delta." .. vim.o.background .. "=true" }
vim.list_extend(ret, ctx.picker.opts.previewers.git.args or {})
vim.list_extend(ret, { ... })
return ret
end
---@param ctx snacks.picker.preview.ctx
function M.git_log(ctx)
local native = ctx.picker.opts.previewers.git.native
local cmd = {
"git",
"-c",
"delta." .. vim.o.background .. "=true",
local cmd = git(
ctx,
"log",
"--pretty=format:%h %s (%ch)",
"--abbrev-commit",
@ -291,8 +297,8 @@ function M.git_log(ctx)
"--color=never",
"--no-show-signature",
"--no-patch",
ctx.item.commit,
}
ctx.item.commit
)
if not native then
table.insert(cmd, 2, "--no-pager")
end
@ -321,13 +327,7 @@ end
---@param ctx snacks.picker.preview.ctx
function M.git_diff(ctx)
local native = ctx.picker.opts.previewers.git.native
local cmd = {
"git",
"-c",
"delta." .. vim.o.background .. "=true",
"diff",
"HEAD",
}
local cmd = git(ctx, "diff", "HEAD")
if ctx.item.file then
vim.list_extend(cmd, { "--", ctx.item.file })
end
@ -340,15 +340,7 @@ end
---@param ctx snacks.picker.preview.ctx
function M.git_stash(ctx)
local native = ctx.picker.opts.previewers.git.native
local cmd = {
"git",
"-c",
"delta." .. vim.o.background .. "=true",
"stash",
"show",
"--patch",
ctx.item.stash,
}
local cmd = git(ctx, "stash", "show", "--patch", ctx.item.stash)
if not native then
table.insert(cmd, 2, "--no-pager")
end