Skip to content

Commit

Permalink
feat(util): LazyVim terminals are now persistent by default (toggleterm)
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 3, 2023
1 parent 15f5a22 commit 5a47492
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions lua/lazyvim/util/init.lua
Expand Up @@ -123,17 +123,39 @@ function M.telescope(builtin, opts)
end
end

---@type table<string,LazyFloat>
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", "<esc>", "<esc>", { 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", "<esc>", "<esc>", { 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?
Expand Down

0 comments on commit 5a47492

Please sign in to comment.