mirror of
https://github.com/folke/snacks.nvim
synced 2025-08-04 02:38:46 +00:00
feat(picker.git): git diff/show can now use native or neovim for preview. defaults to neovim. Closes #500. Closes #494. Closes #491. Closes #478
This commit is contained in:
parent
e5dd9281ee
commit
e36e6af96c
2 changed files with 45 additions and 16 deletions
|
@ -38,12 +38,16 @@ local M = {}
|
|||
---@class snacks.picker.preview.Config
|
||||
---@field man_pager? string MANPAGER env to use for `man` preview
|
||||
---@field file snacks.picker.preview.file.Config
|
||||
---@field git snacks.picker.preview.git.Config
|
||||
|
||||
---@class snacks.picker.preview.file.Config
|
||||
---@field max_size? number default 1MB
|
||||
---@field max_line_length? number defaults to 500
|
||||
---@field ft? string defaults to auto-detect
|
||||
|
||||
---@class snacks.picker.preview.git.Config
|
||||
---@field native? boolean use terminal or Neovim for previewing git diffs and commits
|
||||
|
||||
---@class snacks.picker.layout.Config
|
||||
---@field layout snacks.layout.Box
|
||||
---@field reverse? boolean when true, the list will be reversed (bottom-up)
|
||||
|
@ -100,6 +104,9 @@ local defaults = {
|
|||
},
|
||||
ui_select = true, -- replace `vim.ui.select` with the snacks picker
|
||||
previewers = {
|
||||
git = {
|
||||
native = false, -- use native (terminal) or Neovim for previewing git diffs and commits
|
||||
},
|
||||
file = {
|
||||
max_size = 1024 * 1024, -- 1MB
|
||||
max_line_length = 500,
|
||||
|
|
|
@ -125,14 +125,33 @@ end
|
|||
function M.cmd(cmd, ctx, opts)
|
||||
opts = opts or {}
|
||||
local buf = ctx.preview:scratch()
|
||||
local pty = opts.pty ~= false and not opts.ft
|
||||
local killed = false
|
||||
local chan = vim.api.nvim_open_term(buf, {})
|
||||
local chan = pty and vim.api.nvim_open_term(buf, {}) or nil
|
||||
local output = {} ---@type string[]
|
||||
|
||||
---@param data string
|
||||
local function add(data)
|
||||
output[#output + 1] = data
|
||||
if chan then
|
||||
if pcall(vim.api.nvim_chan_send, chan, data) then
|
||||
vim.api.nvim_buf_call(buf, function()
|
||||
vim.cmd("norm! gg")
|
||||
end)
|
||||
end
|
||||
else
|
||||
vim.bo[buf].modifiable = true
|
||||
local lines = vim.split(table.concat(output, "\n"), "\n")
|
||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
|
||||
vim.bo[buf].modifiable = false
|
||||
end
|
||||
end
|
||||
|
||||
local jid = vim.fn.jobstart(cmd, {
|
||||
height = vim.api.nvim_win_get_height(ctx.win),
|
||||
width = vim.api.nvim_win_get_width(ctx.win),
|
||||
pty = opts.pty ~= false and not opts.ft,
|
||||
cwd = ctx.item.cwd,
|
||||
height = pty and vim.api.nvim_win_get_height(ctx.win) or nil,
|
||||
width = pty and vim.api.nvim_win_get_width(ctx.win) or nil,
|
||||
pty = pty,
|
||||
cwd = ctx.item.cwd or ctx.picker.opts.cwd,
|
||||
env = vim.tbl_extend("force", {
|
||||
PAGER = "cat",
|
||||
DELTA_PAGER = "cat",
|
||||
|
@ -141,14 +160,7 @@ function M.cmd(cmd, ctx, opts)
|
|||
if not vim.api.nvim_buf_is_valid(buf) then
|
||||
return
|
||||
end
|
||||
data = table.concat(data, "\n")
|
||||
local ok = pcall(vim.api.nvim_chan_send, chan, data)
|
||||
if ok then
|
||||
vim.api.nvim_buf_call(buf, function()
|
||||
vim.cmd("norm! gg")
|
||||
end)
|
||||
end
|
||||
table.insert(output, data)
|
||||
add(table.concat(data, "\n"))
|
||||
end,
|
||||
on_exit = function(_, code)
|
||||
if not killed and code ~= 0 then
|
||||
|
@ -171,7 +183,9 @@ function M.cmd(cmd, ctx, opts)
|
|||
callback = function()
|
||||
killed = true
|
||||
vim.fn.jobstop(jid)
|
||||
vim.fn.chanclose(chan)
|
||||
if chan then
|
||||
vim.fn.chanclose(chan)
|
||||
end
|
||||
end,
|
||||
})
|
||||
if jid <= 0 then
|
||||
|
@ -181,6 +195,7 @@ end
|
|||
|
||||
---@param ctx snacks.picker.preview.ctx
|
||||
function M.git_show(ctx)
|
||||
local native = ctx.picker.opts.previewers.git.native
|
||||
local cmd = {
|
||||
"git",
|
||||
"-c",
|
||||
|
@ -188,11 +203,15 @@ function M.git_show(ctx)
|
|||
"show",
|
||||
ctx.item.commit,
|
||||
}
|
||||
M.cmd(cmd, ctx)
|
||||
if not native then
|
||||
table.insert(cmd, 2, "--no-pager")
|
||||
end
|
||||
M.cmd(cmd, ctx, { ft = not native and "git" or nil })
|
||||
end
|
||||
|
||||
---@param ctx snacks.picker.preview.ctx
|
||||
function M.git_diff(ctx)
|
||||
local native = ctx.picker.opts.previewers.git.native
|
||||
local cmd = {
|
||||
"git",
|
||||
"-c",
|
||||
|
@ -201,7 +220,10 @@ function M.git_diff(ctx)
|
|||
"--",
|
||||
ctx.item.file,
|
||||
}
|
||||
M.cmd(cmd, ctx)
|
||||
if not native then
|
||||
table.insert(cmd, 2, "--no-pager")
|
||||
end
|
||||
M.cmd(cmd, ctx, { ft = not native and "diff" or nil })
|
||||
end
|
||||
|
||||
---@param ctx snacks.picker.preview.ctx
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue