Skip to content

Instantly share code, notes, and snippets.

@davidosomething
Last active July 20, 2023 15:46
Show Gist options
  • Save davidosomething/6c2615710003bec328719c0c50a0ad0f to your computer and use it in GitHub Desktop.
Save davidosomething/6c2615710003bec328719c0c50a0ad0f to your computer and use it in GitHub Desktop.
balance sibling pane sizes in wezterm (not working)
--- Get panes that are on the same axis as the tab's active pane
---@param axis 'y'|'x'
---@param tab table
---@return table siblings
local function get_axis_siblings(axis, tab)
local initial = tab:active_pane()
local siblings = { initial }
local prev_dir = axis == "x" and "Left" or "Up"
local next_dir = axis == "x" and "Right" or "Down"
local prev = tab:get_pane_direction(prev_dir)
while prev do
table.insert(siblings, 1, prev)
prev:activate()
prev = tab:get_pane_direction(prev_dir)
end
initial:activate() -- annoying
local next = tab:get_pane_direction(next_dir)
while next do
table.insert(siblings, next)
next:activate()
next = tab:get_pane_direction(next_dir)
end
initial:activate() -- restore
return siblings
end
---@param axis 'y'|'x'
local function balance_panes(axis)
---@param win table
return function(win)
local tab = win:active_tab()
local initial = tab:active_pane()
local prev_dir = axis == "x" and "Left" or "Up"
local next_dir = axis == "x" and "Right" or "Down"
local siblings = get_axis_siblings(axis, tab)
local tab_size = tab:get_size()[axis == "x" and "cols" or "rows"]
local balanced_size = math.floor(tab_size / #siblings)
local pane_size_key = axis == "x" and "cols" or "viewport_rows"
wezterm.log_info(
string.format(
"resizing %s panes on %s axis to %s cells",
#siblings,
axis,
balanced_size
)
)
for i, p in ipairs(siblings) do
local pane_size = p:get_dimensions()[pane_size_key]
local adj_amount = pane_size - balanced_size
local adj_dir = adj_amount < 0 and next_dir or prev_dir
adj_amount = math.abs(adj_amount)
wezterm.log_info(
string.format(
"adjusting pane %s from %s by %s cells %s",
tostring(i),
tostring(pane_size),
tostring(adj_amount),
adj_dir
)
)
p:activate()
win:perform_action(
-- AdjustPaneSize only acts on active pane
wezterm.action.AdjustPaneSize({ adj_dir, adj_amount }),
p -- this does not affect anything
)
end
initial:activate()
end
end
wezterm.on("augment-command-palette", function()
return {
{
brief = "Balance panes horizontally",
action = wezterm.action_callback(balance_panes("x")),
},
}
end)
@davidosomething
Copy link
Author

davidosomething commented Jul 20, 2023

related

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment