aboutsummaryrefslogtreecommitdiff
path: root/tmuxrdr/plugin/bin/rotate-window
blob: 2457fa3bcf3b4dce6d05686aa422232e63368359 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/sh
# tmux prefix+C-o (rotate-window, upward) and prefix+M-o (rotate-window -D):
# every pane moves one place in pane-index order, wrapping. Measured on tmux
# 3.7c: %0 %1 %2 becomes %1 %2 %0 upward, %2 %0 %1 downward, and focus stays on
# the *position* -- the pane now there becomes current -- so this refocuses it
# by id over the socket (pane.focus, which the CLI does not wrap).
. "$(dirname "$0")/../lib.sh"

p="$(active_pane)"
panes="$(tab_panes "$p")"
n="$(printf '%s\n' "$panes" | wc -l | tr -d ' ')"
[ "$n" -gt 1 ] || exit 0
pos="$(printf '%s\n' "$panes" | grep -n -x -F "$p" | cut -d: -f1)"

# Swaps move panes, ids and all: swapping the first pane with each of the
# others in turn rotates upward; the last with each, in reverse, downward.
case "${1:-up}" in
up)
    first="$(printf '%s\n' "$panes" | sed -n 1p)"
    printf '%s\n' "$panes" | sed 1d | while IFS= read -r t; do
        swap "$first" "$t"
    done
    ;;
down)
    last="$(printf '%s\n' "$panes" | sed -n "${n}p")"
    printf '%s\n' "$panes" | sed '$d' | sed -n '1!G;h;$p' | while IFS= read -r t; do
        swap "$last" "$t"
    done
    ;;
*)
    fail "usage: rotate-window up|down"
    ;;
esac
focus_pane "$(tab_panes "$p" | sed -n "${pos}p")"