-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmux-layout-dwindle
executable file
·290 lines (282 loc) · 11.5 KB
/
tmux-layout-dwindle
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/bin/sh -eu
#
# Usage: tmux-layout-dwindle [tblrhvsc]...
#
# Arranges panes in the current window into Binary Space Partitions of
# dwindling size towards a corner of the screen (tl, tr, bl, br) along
# a straight line (c) or spiral trajectory (s) while laying out branch
# panes either vertically (v) or horizontally (h) to produce one of 16
# possible orientations, illustrated below, of which "brvc" is default.
#
# Inspired by the dwindle and spiral layout concepts by Niki Yoshiuchi
# in dwm's fibonacci patch <http://dwm.suckless.org/patches/fibonacci>.
#
#
#
#
# oooo ooo .ooooo.
# `88. .8' d88' `"Y8
# `88..8' 888
# `888' 888 .o8
# `8' `Y8bod8P'
#
#
#
# vertical corner
#
#
#
# tlvc trvc
#
# +--+--+-----+-----------+ +-----------+-----+--+--+
# |\\|5 | | | | | | 5|//|
# +--+--+ 3 | | | | 3 +--+--+
# | 4 | | | | | | 4 |
# +-----+-----+ 1 | | 1 +-----+-----+
# | | | | | |
# | 2 | | | | 2 |
# | | | | | |
# +-----------+-----------+ +-----------+-----------+
#
# +-----------+-----------+ +-----------+-----------+
# | | | | | |
# | 2 | | | | 2 |
# | | | | | |
# +-----+-----+ 1 | | 1 +-----+-----+
# | 4 | | | | | | 4 |
# +--+--+ 3 | | | | 3 +--+--+
# |//|5 | | | | | | 5|\\|
# +--+--+-----+-----------+ +-----------+-----+--+--+
#
# blvc brvc
#
#
#
#
#
# oooo ooo .oooo.o
# `88. .8' d88( "8
# `88..8' `"Y88b.
# `888' o. )88b
# `8' 8""888P'
#
#
#
# vertical spiral
#
#
#
# tlvs trvs
#
# +-----+-----+-----------+ +-----------+-----+-----+
# | | 4 | | | | 4 | |
# | 3 +--+--+ | | +--+--+ 3 |
# | |\\|5 | | | | 5|//| |
# +-----+--+--+ 1 | | 1 +--+--+-----+
# | | | | | |
# | 2 | | | | 2 |
# | | | | | |
# +-----------+-----------+ +-----------+-----------+
#
# +-----------+-----------+ +-----------+-----------+
# | | | | | |
# | 2 | | | | 2 |
# | | | | | |
# +-----+--+--+ 1 | | 1 +--+--+-----+
# | |//|5 | | | | 5|\\| |
# | 3 +--+--+ | | +--+--+ 3 |
# | | 4 | | | | 4 | |
# +-----+-----+-----------+ +-----------+-----+-----+
#
# blvs brvs
#
#
#
# oooo
# `888
# 888 .oo. .ooooo.
# 888P"Y88b d88' `"Y8
# 888 888 888
# 888 888 888 .o8
# o888o o888o `Y8bod8P'
#
#
#
# horizontal corner
#
#
#
# tlhc trhc
#
# +-----+-----+-----------+ +-----------+-----+-----+
# | \\\ | | | | | | /// |
# +-----+ 4 | | | | 4 +-----+
# | 5 | | | | | | 5 |
# +-----+-----+ 2 | | 2 +-----+-----+
# | | | | | |
# | 3 | | | | 3 |
# | | | | | |
# +-----------+-----------+ +-----------+-----------+
# | | | |
# | | | |
# | | | |
# | 1 | | 1 |
# | | | |
# | | | |
# | | | |
# +-----------------------+ +-----------------------+
#
# +-----------------------+ +-----------------------+
# | | | |
# | | | |
# | | | |
# | 1 | | 1 |
# | | | |
# | | | |
# | | | |
# +-----------+-----------+ +-----------+-----------+
# | | | | | |
# | 3 | | | | 3 |
# | | | | | |
# +-----+-----+ 2 | | 2 +-----+-----+
# | 5 | | | | | | 5 |
# +-----+ 4 | | | | 4 +-----+
# | /// | | | | | | \\\ |
# +-----+-----+-----------+ +-----------+-----+-----+
#
# blhc brhc
#
#
#
# oooo
# `888
# 888 .oo. .oooo.o
# 888P"Y88b d88( "8
# 888 888 `"Y88b.
# 888 888 o. )88b
# o888o o888o 8""888P'
#
#
#
# horizontal spiral
#
#
#
# tlhs trhs
#
# +-----------+-----------+ +-----------+-----------+
# | | | | | |
# | 3 | | | | 3 |
# | | | | | |
# +-----+-----+ 2 | | 2 +-----+-----+
# | | \\\ | | | | /// | |
# | 4 +-----+ | | +-----+ 4 |
# | | 5 | | | | 5 | |
# +-----+-----+-----------+ +-----------+-----+-----+
# | | | |
# | | | |
# | | | |
# | 1 | | 1 |
# | | | |
# | | | |
# | | | |
# +-----------------------+ +-----------------------+
#
# +-----------------------+ +-----------------------+
# | | | |
# | | | |
# | | | |
# | 1 | | 1 |
# | | | |
# | | | |
# | | | |
# +-----+-----+-----------+ +-----------+-----+-----+
# | | 5 | | | | 5 | |
# | 4 +-----+ | | +-----+ 4 |
# | | /// | | | | \\\ | |
# +-----+-----+ 2 | | 2 +-----+-----+
# | | | | | |
# | 3 | | | | 3 |
# | | | | | |
# +-----------+-----------+ +-----------+-----------+
#
# blhs brhs
#
#
#
#
# Written in 2016 by Suraj N. Kurapati <https://github.com/sunaku>
# Documented at <https://sunaku.github.io/tmux-layout-dwindle.html>
# parse any orientation flags specified among the command-line arguments
case "$*" in
(*t*) corner_tb=+ spiral_tb= ;; # top
(*b*|*) corner_tb= spiral_tb=+ ;; # bottom
esac
case "$*" in
(*l*) corner_lr=+ spiral_lr= ;; # left
(*r*|*) corner_lr= spiral_lr=+ ;; # right
esac
case "$*" in
(*h*) modulo_hv=0 is_vertical=false ;; # horizontal
(*v*|*) modulo_hv=1 is_vertical=true ;; # vertical
esac
case "$*" in
(*s*) is_spiral=true ;; # spiral
(*c*|*) is_spiral=false ;; # corner
esac
# gather information about the current state of the window and its panes
set -- $(tmux list-panes -F '#{pane_id}')
selected_pane=$(tmux display-message -p '#{pane_id}')
historic_pane=$(tmux last-pane 2>/dev/null \;\
display-message -p '#{pane_id}' \;\
last-pane) ||: # exit 1 - no last pane
window_height=$(tmux display-message -p '#{window_height}')
# execute all tmux commands in one shot to avoid flickering and slowness
exec tmux $({
# flatten current layout, stacking all panes vertically like pancakes
echo select-layout even-vertical
# transform pane stack into binary space partitions of dwindling size
count=1
for pane_id; do
if [ $count -eq $# ]; then
break # skip last pane because .+1 wraps around to the first pane
elif [ $(( count % 2 )) -eq $modulo_hv ]; then
move_h=+
if $is_spiral && [ $(( count % 5 )) -gt 2 ]
then move_b=$spiral_lr
else move_b=$corner_lr
fi
else
move_h=
if $is_spiral && [ $(( count % 5 )) -gt 2 ]
then move_b=$spiral_tb
else move_b=$corner_tb
fi
fi
echo resize-pane -t $pane_id -y $window_height # make room for move
echo select-pane -t $pane_id # for relative pane addressing in move
echo move-pane -d -s .+1 -t . ${move_h:+-h} ${move_b:+-b} # move it
count=$(( count + 1 ))
done
# divide available space evenly among panes (binary space partitions)
branch_height=$window_height
count=1
for pane_id; do
if [ $count -eq $# ] && ! $is_vertical; then
break # skip last pane because it will already be sized correctly
elif [ $(( count % 2 )) -eq 1 ]; then
# every other pane is a branch in the binary space partition tree
parent_height=$branch_height
branch_height=$(( branch_height / 2 ))
if $is_vertical
then resize_y=$parent_height
else resize_y=$branch_height
fi
echo resize-pane -t $pane_id -y $resize_y
fi
count=$(( count + 1 ))
done
# restore pane selection back to how it was before we did any of this
test -n "$historic_pane" && echo select-pane -t $historic_pane
echo select-pane -t $selected_pane
} | sed 's/$/ ;/')