lua-api-crates/mux: expose swap_active_with_index
This is related to this discussion of swapping panes . For an usage example:
local function swap_panes(dir)
return wezterm.action_callback(function(win, pane)
local tab = win:active_tab()
local target_pane = tab:get_pane_direction(dir)
if target_pane then
local panes = tab:panes()
local target_index = nil
for i, p in ipairs(panes) do
if p.pane_id == target_pane.pane_id then
target_index = i - 1
break
end
end
if target_index then
tab:swap_active_with_index(target_index, true)
end
end
end)
end
config = {
keys = {
{ key = 'h', mods = 'CTRL|ALT', action = swap_panes("Left") },
{ key = 'j', mods = 'CTRL|ALT', action = swap_panes("Down") },
{ key = 'k', mods = 'CTRL|ALT', action = swap_panes("Up") },
{ key = 'l', mods = 'CTRL|ALT', action = swap_panes("Right") },
}
}
This originally confused me because I thought such a method already exists. It does but is limited to adjacent panes, which your example simply duplicates albeit with a different API call.
What method is that?
I was aware of this action and I even referenced it as I was figuring out swap_active_with_index. It didn't fit my needs however. I can see it overlapping with pane marking as I wrote in the documentation, but I feel like it still stands on its own.
Hey there, I was tinkering around on my own PR for this when I realized this one already existed, and had a couple thoughts on this:
- I think the Lua function would be cleaner if it took Pane ID instead of Pane index. Indices seem to be somewhat ephemeral, but Pane ID consistently identifies a given pane. Additionally, for the purposes of both the marking example and getting the directional swapping of
tmux, the relevant functions (i.e.pane:pane_idandtab:get_pane_directionrespectively) return Pane ID. -
TabandTabInner(inmux/src/tab.rs) should have aidx_from_idfunction analogous to the one inWindow(inmux/src/window.rs).
When combined, these two recommendations (imo) greatly simplify the Lua code and give us more options on the Rust side, both for error handling on this feature and in the future.
Thanks for the feedback. My goal here was to expose the method for my own use in my config and so my changes already provide what I need. I do think pane ID would be more straightforward but I feel like that requires Rust work and falls a bit out of scope of this PR. I'd love to get this merged
I have a commit I was using for my local build for a while that I think does what you'd need if you want to attempt it.
I'm not a maintainer, so I'm not blocking here, just another user coming from tmux who wants this feature. Either way (if this gets merged as is or tweaked to use pane ID) I'll get to use this feature :)
I've implemented your code as a separate commit and changed the documentation to reflect that.
Awesome! Looking forward to seeing this feature merged