From 6dfcc07aa30cb37532843dcdae561b77866562ec Mon Sep 17 00:00:00 2001 From: Jonathan Feinberg Date: Sat, 22 May 2021 21:39:15 +0200 Subject: [PATCH 1/3] custom terminal creation command --- lua/harpoon/term.lua | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/lua/harpoon/term.lua b/lua/harpoon/term.lua index 90c4268..69d765e 100644 --- a/lua/harpoon/term.lua +++ b/lua/harpoon/term.lua @@ -4,11 +4,14 @@ local log = require("harpoon.dev").log local M = {} local terminals = {} -local function create_terminal() - log.trace("_create_terminal()") +local function create_terminal(create_cmd) + if not create_cmd then + create_cmd = ":terminal" + end + log.trace("_create_terminal(): Init:", create_cmd) local current_id = vim.fn.bufnr() - vim.cmd(":terminal") + vim.cmd(create_cmd) local buf_id = vim.fn.bufnr() local term_id = vim.b.terminal_job_id @@ -27,11 +30,11 @@ local function create_terminal() return buf_id, term_id end -local function find_terminal(idx) +local function find_terminal(idx, create_cmd) log.trace("_find_terminal(): Terminal:", idx) local term_handle = terminals[idx] if not term_handle or not vim.api.nvim_buf_is_valid(term_handle.buf_id) then - local buf_id, term_id = create_terminal() + local buf_id, term_id = create_terminal(create_cmd) if buf_id == nil then return end @@ -45,24 +48,27 @@ local function find_terminal(idx) return term_handle end -M.gotoTerminal = function(idx) +M.gotoTerminal = function(idx, create_cmd) log.trace("gotoTerminal(): Terminal:", idx) - local term_handle = find_terminal(idx) + local term_handle = find_terminal(idx, create_cmd) vim.api.nvim_set_current_buf(term_handle.buf_id) end -M.sendCommand = function(idx, cmd, ...) +M.sendCommand = function(idx, cmd, create_cmd) log.trace("sendCommand(): Terminal:", idx) - local term_handle = find_terminal(idx) + local term_handle = find_terminal(idx, create_cmd) if type(cmd) == "number" then cmd = harpoon.get_term_config().cmds[cmd] end + if type(cmd) == "string" then + cmd = {cmd} + end if cmd then - log.debug("sendCommand:", cmd) - vim.fn.chansend(term_handle.term_id, string.format(cmd, ...)) + log.debug("sendCommand:", cmd[1]) + vim.fn.chansend(term_handle.term_id, string.format(unpack(cmd))) end end From f1a61debf204c8e17ea60f52671ea563a137f938 Mon Sep 17 00:00:00 2001 From: Jonathan Feinberg Date: Thu, 27 May 2021 13:37:44 +0200 Subject: [PATCH 2/3] double idx as number and table --- lua/harpoon/term.lua | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/lua/harpoon/term.lua b/lua/harpoon/term.lua index 69d765e..0fa9076 100644 --- a/lua/harpoon/term.lua +++ b/lua/harpoon/term.lua @@ -4,14 +4,14 @@ local log = require("harpoon.dev").log local M = {} local terminals = {} -local function create_terminal(create_cmd) - if not create_cmd then - create_cmd = ":terminal" +local function create_terminal(create_with) + if not create_with then + create_with = ":terminal" end - log.trace("_create_terminal(): Init:", create_cmd) + log.trace("_create_terminal(): Init:", create_with) local current_id = vim.fn.bufnr() - vim.cmd(create_cmd) + vim.cmd(create_with) local buf_id = vim.fn.bufnr() local term_id = vim.b.terminal_job_id @@ -30,11 +30,14 @@ local function create_terminal(create_cmd) return buf_id, term_id end -local function find_terminal(idx, create_cmd) - log.trace("_find_terminal(): Terminal:", idx) - local term_handle = terminals[idx] +local function find_terminal(args) + log.trace("_find_terminal(): Terminal:", args) + 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 - local buf_id, term_id = create_terminal(create_cmd) + local buf_id, term_id = create_terminal(args.create_with) if buf_id == nil then return end @@ -43,32 +46,29 @@ local function find_terminal(idx, create_cmd) buf_id = buf_id, term_id = term_id, } - terminals[idx] = term_handle + terminals[args.idx] = term_handle end return term_handle end -M.gotoTerminal = function(idx, create_cmd) +M.gotoTerminal = function(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) end -M.sendCommand = function(idx, cmd, create_cmd) +M.sendCommand = function(idx, cmd, ...) log.trace("sendCommand(): Terminal:", idx) - local term_handle = find_terminal(idx, create_cmd) + local term_handle = find_terminal(idx) if type(cmd) == "number" then cmd = harpoon.get_term_config().cmds[cmd] end - if type(cmd) == "string" then - cmd = {cmd} - end if cmd then - log.debug("sendCommand:", cmd[1]) - vim.fn.chansend(term_handle.term_id, string.format(unpack(cmd))) + log.debug("sendCommand:", cmd) + vim.fn.chansend(term_handle.term_id, string.format(cmd, ...)) end end From c3f6755344c94ab8eb872d0871c43f6d79635339 Mon Sep 17 00:00:00 2001 From: Jonathan Feinberg Date: Fri, 4 Jun 2021 20:20:05 +0200 Subject: [PATCH 3/3] fixed formatting --- lua/harpoon/term.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/harpoon/term.lua b/lua/harpoon/term.lua index 0fa9076..4ab640b 100644 --- a/lua/harpoon/term.lua +++ b/lua/harpoon/term.lua @@ -33,7 +33,7 @@ end local function find_terminal(args) log.trace("_find_terminal(): Terminal:", args) if type(args) == "number" then - args = { idx=args } + 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