diff --git a/lua/lazyvim/util/init.lua b/lua/lazyvim/util/init.lua index 7625d8a9..c42e0ab2 100644 --- a/lua/lazyvim/util/init.lua +++ b/lua/lazyvim/util/init.lua @@ -123,17 +123,39 @@ function M.telescope(builtin, opts) end end +---@type table +local terminals = {} + -- Opens a floating terminal (interactive by default) ---@param cmd? string[]|string ---@param opts? LazyCmdOptions|{interactive?:boolean, esc_esc?:false} function M.float_term(cmd, opts) opts = vim.tbl_deep_extend("force", { + ft = "lazyterm", size = { width = 0.9, height = 0.9 }, - }, opts or {}) - local float = require("lazy.util").float_term(cmd, opts) - if opts.esc_esc == false then - vim.keymap.set("t", "", "", { buffer = float.buf, nowait = true }) + }, opts or {}, { persistent = true }) + ---@cast opts LazyCmdOptions|{interactive?:boolean, esc_esc?:false} + + local termkey = vim.inspect({ cmd = cmd or "shell", cwd = opts.cwd, env = opts.env }) + + 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", "", "", { buffer = buf, nowait = true }) + end + vim.api.nvim_create_autocmd("BufEnter", { + buffer = buf, + callback = function() + vim.cmd.startinsert() + end, + }) end + + return terminals[termkey] end ---@param silent boolean?