Skip to content

Commit

Permalink
feat: Add TermNew command
Browse files Browse the repository at this point in the history
Humans are bad at remembering numbers, so this command allows you to
open a new terminal at the next available count. It's helpful in
combination with `:TermSelect` to work with terminals without needing to
remember numbers. It supports the regular arguments as `:ToggleTerm`.
  • Loading branch information
waiting-for-dev committed Dec 24, 2024
1 parent 87b2d6a commit f57953a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,13 @@ You can send commands to a terminal without opening its window by using the `ope

see `:h expand()` for more details

### TermNew

This command allows you to open a new terminal at the next available count.
It's helpful in combination with `TermSelect` (see below) to work with
terminals without needing to remember numbers. The `size`, `dir`, `direction`
and `name` arguments work the same as in `ToggleTerm`.

### TermSelect

This command uses `vim.ui.select` to allow a user to select a terminal to open
Expand Down
6 changes: 6 additions & 0 deletions doc/toggleterm.txt
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ You can send commands to a terminal without opening its window by using the

see |expand()| for more details

TERMNEW ~

This command allows you to open a new terminal at the next available count.
It's helpful in combination with `TermSelect` (see below) to work with
terminals without needing to remember numbers. The `size`, `dir`, `direction`
and `name` arguments work the same as in `ToggleTerm`.

TERMSELECT ~

Expand Down
27 changes: 27 additions & 0 deletions lua/toggleterm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,18 @@ function M.send_lines_to_terminal(selection_type, trim_spaces, cmd_data)
api.nvim_win_set_cursor(current_window, { start_line, start_col - 1 })
end

function M.new_command(args)
local parsed = commandline.parse(args)
vim.validate({
size = { parsed.size, "number", true },
dir = { parsed.dir, "string", true },
direction = { parsed.direction, "string", true },
name = { parsed.name, "string", true },
})
if parsed.size then parsed.size = tonumber(parsed.size) end
M.new(parsed.size, parsed.dir, parsed.direction, parsed.name)
end

function M.toggle_command(args, count)
local parsed = commandline.parse(args)
vim.validate({
Expand All @@ -278,6 +290,15 @@ function _G.___toggleterm_winbar_click(id)
end
end

--- Creates new terminal at the first available id
--- @param size number?
--- @param dir string?
--- @param direction string?
--- @param name string?
function M.new(size, dir, direction, name)
toggle_nth_term(terms.next_id(), size, dir, direction, name)
end

--- If a count is provided we operate on the specific terminal buffer
--- i.e. 2ToggleTerm => open or close Term 2
--- if the count is 1 we use a heuristic which is as follows
Expand Down Expand Up @@ -420,6 +441,12 @@ local function setup_commands()
{ count = true, complete = commandline.term_exec_complete, nargs = "*" }
)

command(
"TermNew",
function(opts) M.new_command(opts.args) end,
{ count = true, complete = commandline.toggle_term_complete, nargs = "*" }
)

command(
"ToggleTerm",
function(opts) M.toggle_command(opts.args, opts.count) end,
Expand Down
2 changes: 1 addition & 1 deletion lua/toggleterm/terminal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ local Terminal = {}
--- hasn't already been allocated e.g. in a list of {1,2,5,6} the next id should
--- be 3 then 4 then 7
---@return integer
local function next_id()
function M.next_id()
local all = M.get_all(true)
for index, term in pairs(all) do
if index ~= term.id then return index end
Expand Down

0 comments on commit f57953a

Please sign in to comment.