From 7cf2e20a411ea106d7367fab4f10bf0243e4f2c2 Mon Sep 17 00:00:00 2001 From: Cam Graff Date: Sat, 22 Jan 2022 19:27:11 -0600 Subject: [PATCH] Add ability to pass any tmux pane identifier --- README.md | 6 ++++++ lua/harpoon/tmux.lua | 29 +++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d8ec620..a686b74 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,12 @@ lua require("harpoon.tmux").sendCommand(1, "ls -La") -- sends ls -La to tmux lua require("harpoon.tmux").sendCommand(1, 1) -- sends command 1 to tmux window 1 ``` +`sendCommand` and `goToTerminal` also accept any valid [tmux pane identifier](https://man7.org/linux/man-pages/man1/tmux.1.html#COMMANDS). +```lua +lua require("harpoon.tmux").gotoTerminal("{down-of}") -- focus the pane directly below +lua require("harpoon.tmux").sendCommand("%3", "ls") -- send a command to the pane with id '%3' +``` + ### Telescope Support 1st register harpoon as a telescope extension ```lua diff --git a/lua/harpoon/tmux.lua b/lua/harpoon/tmux.lua index b1f6a67..4e58821 100644 --- a/lua/harpoon/tmux.lua +++ b/lua/harpoon/tmux.lua @@ -67,6 +67,12 @@ end local function find_terminal(args) log.trace("tmux: _find_terminal(): Window:", args) + if type(args) == "string" then + -- assume args is a valid tmux target identifier + -- if invalid, the error returned by tmux will be thrown + return { window_id = args } + end + if type(args) == "number" then args = { idx = args } end @@ -87,7 +93,7 @@ local function find_terminal(args) end window_handle = { - window_id = window_id, + window_id = "%" .. window_id, } tmux_windows[args.idx] = window_handle @@ -110,12 +116,16 @@ function M.gotoTerminal(idx) log.trace("tmux: gotoTerminal(): Window:", idx) local window_handle = find_terminal(idx) - utils.get_os_command_output({ + local _, ret, stderr = utils.get_os_command_output({ "tmux", - "select-window", + "select-pane", "-t", - "%" .. window_handle.window_id, + window_handle.window_id, }, vim.loop.cwd()) + + if ret ~= 0 then + error("Failed to go to terminal." .. stderr[1]) + end end function M.sendCommand(idx, cmd, ...) @@ -133,14 +143,17 @@ function M.sendCommand(idx, cmd, ...) if cmd then log.debug("sendCommand:", cmd) - -- Send the command to the given tmux window (creates it if it doesn't exist) - local _, _, _ = utils.get_os_command_output({ + local _, ret, stderr = utils.get_os_command_output({ "tmux", "send-keys", "-t", - "%" .. window_handle.window_id, + window_handle.window_id, string.format(cmd, ...), }, vim.loop.cwd()) + + if ret ~= 0 then + error("Failed to send command. " .. stderr[1]) + end end end @@ -153,7 +166,7 @@ function M.clear_all() "tmux", "kill-window", "-t", - "%" .. window.window_id, + window.window_id, }, vim.loop.cwd()) end