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

@ -158,18 +158,12 @@ map("n", "<leader>uI", "<cmd>InspectTree<cr>", { desc = "Inspect Tree" })
map("n", "<leader>L", function() LazyVim.news.changelog() end, { desc = "LazyVim Changelog" })
-- floating terminal
local lazyterm = function() LazyVim.terminal(nil, { cwd = LazyVim.root() }) end
map("n", "<leader>ft", lazyterm, { desc = "Terminal (Root Dir)" })
map("n", "<leader>fT", function() LazyVim.terminal() end, { desc = "Terminal (cwd)" })
map("n", "<c-/>", lazyterm, { desc = "Terminal (Root Dir)" })
map("n", "<c-_>", lazyterm, { desc = "which_key_ignore" })
map("n", "<leader>fT", function() Snacks.terminal() end, { desc = "Terminal (cwd)" })
map("n", "<leader>ft", function() Snacks.terminal(nil, { cwd = LazyVim.root() }) end, { desc = "Terminal (Root Dir)" })
map("n", "<c-/>", function() Snacks.terminal(nil, { cwd = LazyVim.root() }) end, { desc = "Terminal (Root Dir)" })
map("n", "<c-_>", function() Snacks.terminal(nil, { cwd = LazyVim.root() }) end, { desc = "which_key_ignore" })
-- Terminal Mappings
map("t", "<esc><esc>", "<c-\\><c-n>", { desc = "Enter Normal Mode" })
map("t", "<C-h>", "<cmd>wincmd h<cr>", { desc = "Go to Left Window" })
map("t", "<C-j>", "<cmd>wincmd j<cr>", { desc = "Go to Lower Window" })
map("t", "<C-k>", "<cmd>wincmd k<cr>", { desc = "Go to Upper Window" })
map("t", "<C-l>", "<cmd>wincmd l<cr>", { desc = "Go to Right Window" })
map("t", "<C-/>", "<cmd>close<cr>", { desc = "Hide Terminal" })
map("t", "<c-_>", "<cmd>close<cr>", { desc = "which_key_ignore" })

View file

@ -31,14 +31,6 @@ return {
return vim.api.nvim_win_get_config(win).relative == ""
end,
},
{
ft = "lazyterm",
title = "LazyTerm",
size = { height = 0.4 },
filter = function(buf)
return not vim.b[buf].lazyterm_cmd
end,
},
"Trouble",
{ ft = "qf", title = "QuickFix" },
{
@ -103,6 +95,7 @@ return {
end
end
-- trouble
for _, pos in ipairs({ "top", "bottom", "left", "right" }) do
opts[pos] = opts[pos] or {}
table.insert(opts[pos], {
@ -116,6 +109,22 @@ return {
end,
})
end
-- snacks float
for _, pos in ipairs({ "top", "bottom", "left", "right" }) do
opts[pos] = opts[pos] or {}
table.insert(opts[pos], {
ft = "snacks_terminal",
size = { height = 0.4 },
title = "%{b:snacks_terminal.id}: %{b:term_title}",
filter = function(_buf, win)
return vim.w[win].snacks_float
and vim.w[win].snacks_float.position == pos
and vim.w[win].snacks_float.relative == "editor"
and not vim.w[win].trouble_preview
end,
})
end
return opts
end,
},

View file

@ -19,7 +19,8 @@ return {
"fzf",
"help",
"lazy",
"lazyterm",
"snacks_terminal",
"snacks_float",
"mason",
"neo-tree",
"notify",

View file

@ -8,14 +8,14 @@ return {
{
"<leader>gG",
function()
LazyVim.terminal.open({ "gitui" }, { esc_esc = false, ctrl_hjkl = false })
Snacks.terminal({ "gitui" })
end,
desc = "GitUi (cwd)",
},
{
"<leader>gg",
function()
LazyVim.terminal.open({ "gitui" }, { cwd = LazyVim.root.get(), esc_esc = false, ctrl_hjkl = false })
Snacks.terminal({ "gitui" }, { cwd = LazyVim.root.get() })
end,
desc = "GitUi (Root Dir)",
},

View file

@ -10,8 +10,35 @@ end
require("lazyvim.config").init()
-- Terminal Mappings
local function term_nav(dir)
---@param self snacks.terminal
return function(self)
return self:is_floating() and "<c-" .. dir .. ">" or vim.schedule(function()
vim.cmd.wincmd(dir)
end)
end
end
return {
{ "folke/lazy.nvim", version = "*" },
{ "LazyVim/LazyVim", priority = 10000, lazy = false, opts = {}, cond = true, version = "*" },
{ "folke/snacks.nvim", lazy = false, opts = {} },
{
"folke/snacks.nvim",
priority = 1000,
lazy = false,
---@type snacks.Config
opts = {
terminal = {
float = {
keys = {
nav_h = { "<C-h>", term_nav("h"), desc = "Go to Left Window", expr = true, mode = "t" },
nav_j = { "<C-j>", term_nav("j"), desc = "Go to Lower Window", expr = true, mode = "t" },
nav_k = { "<C-k>", term_nav("k"), desc = "Go to Upper Window", expr = true, mode = "t" },
nav_l = { "<C-l>", term_nav("l"), desc = "Go to Right Window", expr = true, mode = "t" },
},
},
},
},
},
}

View file

@ -254,7 +254,8 @@ return {
"mason",
"notify",
"toggleterm",
"lazyterm",
"snacks_float",
"snacks_terminal",
},
},
}

View file

@ -148,19 +148,22 @@ end
function M.blame_line(opts)
opts = vim.tbl_deep_extend("force", {
count = 3,
filetype = "git",
size = {
width = 0.6,
height = 0.6,
interactive = false,
float = {
win = {
width = 0.6,
height = 0.6,
border = "rounded",
},
bo = { filetype = "git" },
},
border = "rounded",
}, opts or {})
local cursor = vim.api.nvim_win_get_cursor(0)
local line = cursor[1]
local file = vim.api.nvim_buf_get_name(0)
local root = LazyVim.root.detectors.pattern(0, { ".git" })[1] or "."
local cmd = { "git", "-C", root, "log", "-n", opts.count, "-u", "-L", line .. ",+1:" .. file }
return require("lazy.util").float_cmd(cmd, opts)
return Snacks.terminal(cmd, opts)
end
-- stylua: ignore

View file

@ -72,15 +72,17 @@ function M.open(file, opts)
end
end
local float = require("lazy.util").float({
local float = Snacks.float({
file = file,
size = { width = 0.6, height = 0.6 },
win = { width = 0.6, height = 0.6 },
wo = {
spell = false,
wrap = false,
signcolumn = "yes",
statuscolumn = " ",
conceallevel = 3,
},
})
vim.opt_local.spell = false
vim.opt_local.wrap = false
vim.opt_local.signcolumn = "yes"
vim.opt_local.statuscolumn = " "
vim.opt_local.conceallevel = 3
if vim.diagnostic.enable then
pcall(vim.diagnostic.enable, false, { bufnr = float.buf })
else

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