feat(terminal): added Snacks.terminal.get(). Closes #122

This commit is contained in:
Folke Lemaitre 2024-11-20 20:58:00 +01:00
parent c770ebeaf7
commit 7f63d4fefb
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
4 changed files with 48 additions and 10 deletions

View file

@ -120,21 +120,30 @@ function M.open(cmd, opts)
return terminal
end
--- Get or create a terminal window.
--- The terminal id is based on the `cmd`, `cwd`, `env` and `vim.v.count1` options.
--- `opts.create` defaults to `true`.
---@param cmd? string | string[]
---@param opts? snacks.terminal.Opts| {create?: boolean}
---@return snacks.win? terminal, boolean? created
function M.get(cmd, opts)
opts = opts or {}
local id = vim.inspect({ cmd = cmd, cwd = opts.cwd, env = opts.env, count = vim.v.count1 })
local created = false
if not (terminals[id] and terminals[id]:buf_valid()) and (opts.create ~= false) then
terminals[id] = M.open(cmd, opts)
created = true
end
return terminals[id], created
end
--- Toggle a terminal window.
--- The terminal id is based on the `cmd`, `cwd`, `env` and `vim.v.count1` options.
---@param cmd? string | string[]
---@param opts? snacks.terminal.Opts
function M.toggle(cmd, opts)
opts = opts or {}
local id = vim.inspect({ cmd = cmd, cwd = opts.cwd, env = opts.env, count = vim.v.count1 })
if terminals[id] and terminals[id]:buf_valid() then
terminals[id]:toggle()
else
terminals[id] = M.open(cmd, opts)
end
return terminals[id]
local terminal, created = M.get(cmd, opts)
return created and terminal or assert(terminal):toggle()
end
--- Parses a shell command into a table of arguments.

View file

@ -225,6 +225,7 @@ end
function M:hide()
self:close({ buf = false })
return self
end
function M:toggle()
@ -233,6 +234,7 @@ function M:toggle()
else
self:show()
end
return self
end
---@private