Files
shawnpana__smux/scripts/smart-layout
Shawn Pana 5dd81430a9 Option+O picks the layout that best matches the window shape; cycling moves to Option+P
Option+O used to cycle tmux's built-in layouts. It now runs a smart-layout
script that scores even-horizontal, even-vertical, and tiled by how close
each one puts the per-pane aspect to a 2:1 cell target (roughly square in
pixels) and applies the winner. A wide window with four panes becomes a
single horizontal row instead of a squished 2x2; a tall narrow window
becomes a vertical stack. The old layout-cycling behavior is preserved on
Option+P for anyone who wants to hand-pick.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 13:01:07 -07:00

36 lines
1.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Pick the tmux layout that makes panes closest to a "nice" shape (target
# aspect = 2 cells wide : 1 cell tall, which renders roughly square in
# pixels). Considers even-horizontal, even-vertical, and tiled; applies the
# one with the lowest aspect error. Usage: smart-layout [<pane_id>]
set -u
TGT="${1:-}"
read -r N W H <<<"$(
tmux display-message -p ${TGT:+-t "$TGT"} '#{window_panes} #{window_width} #{window_height}' 2>/dev/null
)"
[ -z "${N:-}" ] && exit 0
[ "$N" -le 1 ] && exit 0
LAYOUT=$(awk -v n="$N" -v w="$W" -v h="$H" 'BEGIN {
target = 2.0
tc = int(sqrt(n)); if (tc*tc < n) tc++
tr = int(n/tc); if (tc*tr < n) tr++
split("even-horizontal even-vertical tiled", names, " ")
cols[1] = n; rows[1] = 1
cols[2] = 1; rows[2] = n
cols[3] = tc; rows[3] = tr
best_name = "tiled"; best_err = 1e9
for (i = 1; i <= 3; i++) {
a = (w/cols[i]) / (h/rows[i])
err = (a > target) ? a/target : target/a
if (err < best_err) { best_err = err; best_name = names[i] }
}
print best_name
}')
tmux select-layout ${TGT:+-t "$TGT"} "$LAYOUT"