wezterm icon indicating copy to clipboard operation
wezterm copied to clipboard

lua-api-crates/mux: expose swap_active_with_index

Open SandWoodJones opened this issue 9 months ago • 10 comments

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") },
  }
}

SandWoodJones avatar Mar 24 '25 19:03 SandWoodJones

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?

SandWoodJones avatar Mar 25 '25 01:03 SandWoodJones

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.

SandWoodJones avatar Apr 13 '25 14:04 SandWoodJones

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_id and tab:get_pane_direction respectively) return Pane ID.
  • Tab and TabInner (in mux/src/tab.rs) should have a idx_from_id function analogous to the one in Window (in mux/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.

nonolai avatar May 09 '25 17:05 nonolai

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

SandWoodJones avatar Jun 06 '25 05:06 SandWoodJones

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 :)

nonolai avatar Jun 08 '25 02:06 nonolai

I've implemented your code as a separate commit and changed the documentation to reflect that.

SandWoodJones avatar Jun 08 '25 17:06 SandWoodJones

Awesome! Looking forward to seeing this feature merged

nonolai avatar Jun 08 '25 17:06 nonolai