double idx as number and table

This commit is contained in:
Jonathan Feinberg 2021-05-27 13:37:44 +02:00
parent 6dfcc07aa3
commit f1a61debf2
No known key found for this signature in database
GPG Key ID: 3673F898D24A40A7

View File

@ -4,14 +4,14 @@ local log = require("harpoon.dev").log
local M = {} local M = {}
local terminals = {} local terminals = {}
local function create_terminal(create_cmd) local function create_terminal(create_with)
if not create_cmd then if not create_with then
create_cmd = ":terminal" create_with = ":terminal"
end end
log.trace("_create_terminal(): Init:", create_cmd) log.trace("_create_terminal(): Init:", create_with)
local current_id = vim.fn.bufnr() local current_id = vim.fn.bufnr()
vim.cmd(create_cmd) vim.cmd(create_with)
local buf_id = vim.fn.bufnr() local buf_id = vim.fn.bufnr()
local term_id = vim.b.terminal_job_id local term_id = vim.b.terminal_job_id
@ -30,11 +30,14 @@ local function create_terminal(create_cmd)
return buf_id, term_id return buf_id, term_id
end end
local function find_terminal(idx, create_cmd) local function find_terminal(args)
log.trace("_find_terminal(): Terminal:", idx) log.trace("_find_terminal(): Terminal:", args)
local term_handle = terminals[idx] if type(args) == "number" then
args = { idx=args }
end
local term_handle = terminals[args.idx]
if not term_handle or not vim.api.nvim_buf_is_valid(term_handle.buf_id) then if not term_handle or not vim.api.nvim_buf_is_valid(term_handle.buf_id) then
local buf_id, term_id = create_terminal(create_cmd) local buf_id, term_id = create_terminal(args.create_with)
if buf_id == nil then if buf_id == nil then
return return
end end
@ -43,32 +46,29 @@ local function find_terminal(idx, create_cmd)
buf_id = buf_id, buf_id = buf_id,
term_id = term_id, term_id = term_id,
} }
terminals[idx] = term_handle terminals[args.idx] = term_handle
end end
return term_handle return term_handle
end end
M.gotoTerminal = function(idx, create_cmd) M.gotoTerminal = function(idx)
log.trace("gotoTerminal(): Terminal:", idx) log.trace("gotoTerminal(): Terminal:", idx)
local term_handle = find_terminal(idx, create_cmd) local term_handle = find_terminal(idx)
vim.api.nvim_set_current_buf(term_handle.buf_id) vim.api.nvim_set_current_buf(term_handle.buf_id)
end end
M.sendCommand = function(idx, cmd, create_cmd) M.sendCommand = function(idx, cmd, ...)
log.trace("sendCommand(): Terminal:", idx) log.trace("sendCommand(): Terminal:", idx)
local term_handle = find_terminal(idx, create_cmd) local term_handle = find_terminal(idx)
if type(cmd) == "number" then if type(cmd) == "number" then
cmd = harpoon.get_term_config().cmds[cmd] cmd = harpoon.get_term_config().cmds[cmd]
end end
if type(cmd) == "string" then
cmd = {cmd}
end
if cmd then if cmd then
log.debug("sendCommand:", cmd[1]) log.debug("sendCommand:", cmd)
vim.fn.chansend(term_handle.term_id, string.format(unpack(cmd))) vim.fn.chansend(term_handle.term_id, string.format(cmd, ...))
end end
end end