feat(snacks): use terminal

This commit is contained in:
Folke Lemaitre 2024-11-03 23:03:19 +01:00
parent 45acfaacb5
commit ad9d3d19b1
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
9 changed files with 77 additions and 89 deletions

View file

@ -1,14 +1,11 @@
---@class lazyvim.util.terminal
---@overload fun(cmd: string|string[], opts: LazyTermOpts): LazyFloat
---@overload fun(cmd: string|string[], opts: snacks.terminal.Config): snacks.terminal
local M = setmetatable({}, {
__call = function(m, ...)
return m.open(...)
end,
})
---@type table<string,LazyFloat>
local terminals = {}
---@param shell? string
function M.setup(shell)
vim.o.shell = shell or vim.o.shell
@ -40,58 +37,12 @@ function M.setup(shell)
end
end
---@class LazyTermOpts: LazyCmdOptions
---@field interactive? boolean
---@field esc_esc? boolean
---@field ctrl_hjkl? boolean
-- Opens a floating terminal (interactive by default)
---@deprecated use Snacks.terminal instead
---@param cmd? string[]|string
---@param opts? LazyTermOpts
---@param opts? snacks.terminal.Config
function M.open(cmd, opts)
opts = vim.tbl_deep_extend("force", {
ft = "lazyterm",
size = { width = 0.9, height = 0.9 },
backdrop = LazyVim.has("edgy.nvim") and not cmd and 100 or nil,
}, opts or {}, { persistent = true }) --[[@as LazyTermOpts]]
local termkey = vim.inspect({ cmd = cmd or "shell", cwd = opts.cwd, env = opts.env, count = vim.v.count1 })
if terminals[termkey] and terminals[termkey]:buf_valid() then
terminals[termkey]:toggle()
else
terminals[termkey] = require("lazy.util").float_term(cmd, opts)
local buf = terminals[termkey].buf
vim.b[buf].lazyterm_cmd = cmd
if opts.esc_esc == false then
vim.keymap.set("t", "<esc>", "<esc>", { buffer = buf, nowait = true })
end
if opts.ctrl_hjkl == false then
vim.keymap.set("t", "<c-h>", "<c-h>", { buffer = buf, nowait = true })
vim.keymap.set("t", "<c-j>", "<c-j>", { buffer = buf, nowait = true })
vim.keymap.set("t", "<c-k>", "<c-k>", { buffer = buf, nowait = true })
vim.keymap.set("t", "<c-l>", "<c-l>", { buffer = buf, nowait = true })
end
vim.keymap.set("n", "gf", function()
local f = vim.fn.findfile(vim.fn.expand("<cfile>"))
if f ~= "" then
vim.cmd("close")
vim.cmd("e " .. f)
end
end, { buffer = buf })
vim.api.nvim_create_autocmd("BufEnter", {
buffer = buf,
callback = function()
vim.cmd.startinsert()
end,
})
vim.cmd("noh")
end
return terminals[termkey]
return Snacks.terminal(cmd, opts)
end
return M