Add ability to pass any tmux pane identifier

This commit is contained in:
Cam Graff 2022-01-22 19:27:11 -06:00
parent 6948a402c2
commit 7cf2e20a41
2 changed files with 27 additions and 8 deletions

View File

@ -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 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 ### Telescope Support
1st register harpoon as a telescope extension 1st register harpoon as a telescope extension
```lua ```lua

View File

@ -67,6 +67,12 @@ end
local function find_terminal(args) local function find_terminal(args)
log.trace("tmux: _find_terminal(): Window:", 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 if type(args) == "number" then
args = { idx = args } args = { idx = args }
end end
@ -87,7 +93,7 @@ local function find_terminal(args)
end end
window_handle = { window_handle = {
window_id = window_id, window_id = "%" .. window_id,
} }
tmux_windows[args.idx] = window_handle tmux_windows[args.idx] = window_handle
@ -110,12 +116,16 @@ function M.gotoTerminal(idx)
log.trace("tmux: gotoTerminal(): Window:", idx) log.trace("tmux: gotoTerminal(): Window:", idx)
local window_handle = find_terminal(idx) local window_handle = find_terminal(idx)
utils.get_os_command_output({ local _, ret, stderr = utils.get_os_command_output({
"tmux", "tmux",
"select-window", "select-pane",
"-t", "-t",
"%" .. window_handle.window_id, window_handle.window_id,
}, vim.loop.cwd()) }, vim.loop.cwd())
if ret ~= 0 then
error("Failed to go to terminal." .. stderr[1])
end
end end
function M.sendCommand(idx, cmd, ...) function M.sendCommand(idx, cmd, ...)
@ -133,14 +143,17 @@ function M.sendCommand(idx, cmd, ...)
if cmd then if cmd then
log.debug("sendCommand:", cmd) log.debug("sendCommand:", cmd)
-- Send the command to the given tmux window (creates it if it doesn't exist) local _, ret, stderr = utils.get_os_command_output({
local _, _, _ = utils.get_os_command_output({
"tmux", "tmux",
"send-keys", "send-keys",
"-t", "-t",
"%" .. window_handle.window_id, window_handle.window_id,
string.format(cmd, ...), string.format(cmd, ...),
}, vim.loop.cwd()) }, vim.loop.cwd())
if ret ~= 0 then
error("Failed to send command. " .. stderr[1])
end
end end
end end
@ -153,7 +166,7 @@ function M.clear_all()
"tmux", "tmux",
"kill-window", "kill-window",
"-t", "-t",
"%" .. window.window_id, window.window_id,
}, vim.loop.cwd()) }, vim.loop.cwd())
end end