Skip to content

Commit 7f63d4f

Browse files
committed
feat(terminal): added Snacks.terminal.get(). Closes #122
1 parent c770ebe commit 7f63d4f

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

doc/snacks-terminal.txt

+14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Table of Contents *snacks-terminal-table-of-contents*
1212
5. Module |snacks-terminal-module|
1313
- Snacks.terminal() |snacks-terminal-module-snacks.terminal()|
1414
- Snacks.terminal.colorize()|snacks-terminal-module-snacks.terminal.colorize()|
15+
- Snacks.terminal.get() |snacks-terminal-module-snacks.terminal.get()|
1516
- Snacks.terminal.open() |snacks-terminal-module-snacks.terminal.open()|
1617
- Snacks.terminal.toggle() |snacks-terminal-module-snacks.terminal.toggle()|
1718
6. Links |snacks-terminal-links|
@@ -158,6 +159,19 @@ Example:
158159
<
159160

160161

162+
`Snacks.terminal.get()` *Snacks.terminal.get()*
163+
164+
Get or create a terminal window. The terminal id is based on the `cmd`, `cwd`,
165+
`env` and `vim.v.count1` options. `opts.create` defaults to `true`.
166+
167+
>lua
168+
---@param cmd? string | string[]
169+
---@param opts? snacks.terminal.Opts| {create?: boolean}
170+
---@return snacks.win? terminal, boolean? created
171+
Snacks.terminal.get(cmd, opts)
172+
<
173+
174+
161175
`Snacks.terminal.open()` *Snacks.terminal.open()*
162176

163177
Open a new terminal window.

docs/terminal.md

+13
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ ls -la --color=always | nvim - -c "lua Snacks.terminal.colorize()"
133133
Snacks.terminal.colorize()
134134
```
135135

136+
### `Snacks.terminal.get()`
137+
138+
Get or create a terminal window.
139+
The terminal id is based on the `cmd`, `cwd`, `env` and `vim.v.count1` options.
140+
`opts.create` defaults to `true`.
141+
142+
```lua
143+
---@param cmd? string | string[]
144+
---@param opts? snacks.terminal.Opts| {create?: boolean}
145+
---@return snacks.win? terminal, boolean? created
146+
Snacks.terminal.get(cmd, opts)
147+
```
148+
136149
### `Snacks.terminal.open()`
137150

138151
Open a new terminal window.

lua/snacks/terminal.lua

+18-9
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,30 @@ function M.open(cmd, opts)
120120
return terminal
121121
end
122122

123-
--- Toggle a terminal window.
123+
--- Get or create a terminal window.
124124
--- The terminal id is based on the `cmd`, `cwd`, `env` and `vim.v.count1` options.
125+
--- `opts.create` defaults to `true`.
125126
---@param cmd? string | string[]
126-
---@param opts? snacks.terminal.Opts
127-
function M.toggle(cmd, opts)
127+
---@param opts? snacks.terminal.Opts| {create?: boolean}
128+
---@return snacks.win? terminal, boolean? created
129+
function M.get(cmd, opts)
128130
opts = opts or {}
129-
130131
local id = vim.inspect({ cmd = cmd, cwd = opts.cwd, env = opts.env, count = vim.v.count1 })
131-
132-
if terminals[id] and terminals[id]:buf_valid() then
133-
terminals[id]:toggle()
134-
else
132+
local created = false
133+
if not (terminals[id] and terminals[id]:buf_valid()) and (opts.create ~= false) then
135134
terminals[id] = M.open(cmd, opts)
135+
created = true
136136
end
137-
return terminals[id]
137+
return terminals[id], created
138+
end
139+
140+
--- Toggle a terminal window.
141+
--- The terminal id is based on the `cmd`, `cwd`, `env` and `vim.v.count1` options.
142+
---@param cmd? string | string[]
143+
---@param opts? snacks.terminal.Opts
144+
function M.toggle(cmd, opts)
145+
local terminal, created = M.get(cmd, opts)
146+
return created and terminal or assert(terminal):toggle()
138147
end
139148

140149
--- Parses a shell command into a table of arguments.

lua/snacks/win.lua

+2
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ end
225225

226226
function M:hide()
227227
self:close({ buf = false })
228+
return self
228229
end
229230

230231
function M:toggle()
@@ -233,6 +234,7 @@ function M:toggle()
233234
else
234235
self:show()
235236
end
237+
return self
236238
end
237239

238240
---@private

0 commit comments

Comments
 (0)