feat: add a terminal select command (#429)

This commit is contained in:
Akin 2023-04-24 19:34:48 +01:00 committed by GitHub
parent 72f5445442
commit c8574d7a7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 11 deletions

View file

@ -277,6 +277,12 @@ You can send commands to a terminal without opening its window by using the `ope
see `:h expand()` for more details
### TermSelect
This command uses `vim.ui.select` to allow a user to select a terminal to open
or to focus it if it's already open. This can be useful if you have a lot of
terminals and want to open a specific one.
### Sending lines to the terminal
You can "send lines" to the toggled terminals with the following commands:

View file

@ -120,13 +120,14 @@ end
local function on_term_open()
local id, term = terms.identify()
if not term then
local buf = api.nvim_get_current_buf()
terms.Terminal
:new({
id = id,
bufnr = api.nvim_get_current_buf(),
bufnr = buf,
window = api.nvim_get_current_win(),
highlights = config.highlights,
job_id = vim.b.terminal_job_id,
job_id = vim.b[buf].terminal_job_id,
direction = ui.guess_direction(),
})
:__resurrect()
@ -360,42 +361,59 @@ local function request_term_name(term)
end)
end
local function select_terminal(opts)
local terminals = terms.get_all(opts.bang)
if #terminals == 0 then return utils.notify("No toggleterms are open yet", "info") end
vim.ui.select(terminals, {
prompt = "Please select a terminal to open (or focus): ",
format_item = function(term) return term.id .. ": " .. term:_display_name() end,
}, function(term)
if not term then return end
if term:is_open() then
term:focus()
else
term:open()
end
end)
end
local function setup_commands()
local cmd = api.nvim_create_user_command
local command = api.nvim_create_user_command
command("TermSelect", select_terminal, { bang = true })
-- Count is 0 by default
cmd(
command(
"TermExec",
function(opts) M.exec_command(opts.args, opts.count) end,
{ count = true, complete = commandline.term_exec_complete, nargs = "*" }
)
cmd(
command(
"ToggleTerm",
function(opts) M.toggle_command(opts.args, opts.count) end,
{ count = true, complete = commandline.toggle_term_complete, nargs = "*" }
)
cmd("ToggleTermToggleAll", function(opts) M.toggle_all(opts.bang) end, { bang = true })
command("ToggleTermToggleAll", function(opts) M.toggle_all(opts.bang) end, { bang = true })
cmd(
command(
"ToggleTermSendVisualLines",
function(args) M.send_lines_to_terminal("visual_lines", true, args) end,
{ range = true, nargs = "?" }
)
cmd(
command(
"ToggleTermSendVisualSelection",
function(args) M.send_lines_to_terminal("visual_selection", true, args) end,
{ range = true, nargs = "?" }
)
cmd(
command(
"ToggleTermSendCurrentLine",
function(args) M.send_lines_to_terminal("single_line", true, args) end,
{ nargs = "?" }
)
cmd("ToggleTermSetName", function(opts)
command("ToggleTermSetName", function(opts)
local no_count = not opts.count or opts.count < 1
local no_name = opts.args == ""
if no_count and no_name then

View file

@ -321,7 +321,7 @@ describe("ToggleTerm tests:", function()
end)
it("should execute the same regardless whether shell is a string or a function", function()
toggleterm.setup { shell = function() return vim.o.shell end }
toggleterm.setup({ shell = function() return vim.o.shell end })
local test1 = Terminal:new():toggle()
local _ = match._
spy.on(test1, "send")