-
Notifications
You must be signed in to change notification settings - Fork 0
/
tile
executable file
·219 lines (183 loc) · 5.24 KB
/
tile
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
#!/bin/sh
#
# tile
usage() {
base="$(basename "$0")"
cat >&2 << EOF
Usage:
$ $base -g | --grid : Tile windows in a grid.
$ $base -s | --split : Tile windows horizontal.
$ $base -v | --vert : Tile windows vertically.
$ $base -l | --left : Place pfw on left and tile windows vertically right.
$ $base -r | --right : Place pfw on right and tile windows on horizontally left.
$ $base -h | --help : Show this help
EOF
[ $# -eq 0 ] || exit "$1"
}
# stolen logic from switch_grid from contrib
# thanks z3bra & greduan (you do deserve credit bro)
grid() {
case "$nWindows" in
1) COLS=1 ;;
2|4|5|6|7) COLS=2 ;;
3|8|9|10|11) COLS=3 ;;
12|13|14) COLS=4 ;;
15|16|17) COLS=5 ;;
*)
COLS="$(echo "sqrt($nWindows)" | bc)"
;;
esac
ROWS="$((nWindows / COLS))"
row=1; while test "$row" -le "$ROWS"; do
test "$row" -eq "$ROWS" && {
test $((nWindows % COLS)) -gt 0 && COLS="$((nWindows - COLS))"
}
col=1; while test "$col" -le "$COLS"; do
W=$(((SW + IGAP)/COLS - IGAP - BW))
H=$(((SH + IGAP)/ROWS - IGAP - BW))
X=$((SX - IGAP + W * (col - 1) + col * (IGAP + BW)))
Y=$((SY - IGAP + H * (row - 1) + row * (IGAP + BW)))
wtp "$X" "$Y" "$W" "$H" \
"$(sed "$(((row - 1) * row + col))p;d" "$winlist" | cut -d\ -f 5)"
col=$((col + 1))
done
row=$((row + 1))
done
}
split() {
X=$SX
W=$(((SW + IGAP) / nWindows - IGAP))
while read -r line; do
wid="${line##* }"
wtp "$X" "$SY" "$W" "$SH" "$wid"
X=$((X + W + IGAP))
done < "$winlist"
}
vertical() {
Y=$SY
H=$(((SH + IGAP) / nWindows - IGAP))
while read -r line; do
wid="${line##* }"
wtp "$SX" "$Y" "$SW" "$H" "$wid"
Y=$((Y + H + IGAP))
done < "$winlist"
}
# tile smaller windows on the right
left() {
Y=$SY
W=$(echo "$SW / 100 * 33" | bc -l | cut -d. -f 1)
[ "$W" -lt 445 ] && W=445
X=$((SX + SW - W))
H=$(((SH + IGAP) / (nWindows - 1) - IGAP))
while read -r line; do
wid="${line##* }"
wtp "$X" "$Y" "$W" "$H" "$wid"
Y=$((Y + H + IGAP))
done < "$winlist"
W=$((SW - W - IGAP))
wtp "$SX" "$SY" "$W" "$SH" "$wid"
}
# tile smaller windows on the left
right() {
Y=$SY
H=$(((SH + IGAP) / (nWindows - 1) - IGAP))
W=$(echo "$SW / 100 * 33" | bc -l | cut -d. -f 1)
[ "$W" -lt 445 ] && W=445
while read -r line; do
wid="${line##* }"
wtp "$SX" "$Y" "$W" "$H" "$wid"
Y=$((Y + H + IGAP))
done < "$winlist"
X=$((SX + W + IGAP))
W=$((SW - W - RGAP))
wtp "$X" "$SY" "$W" "$SH" "$wid"
}
browse() {
X=$SX
Y=$SY
W=$(((SW + IGAP) / 2 - IGAP))
H=$(echo "$SH / 100 * 60" | bc -l | cut -d. -f 1)
toplist="$(mktemp)"
botlist="$(mktemp)"
head -n 2 "$winlist" > "$toplist"
tail -n +3 "$winlist" > "$botlist"
while read -r line; do
wid="${line##* }"
wtp "$X" "$Y" "$W" "$H" "$wid"
X=$((X + W + IGAP))
done < "$toplist"
X=$SX
Y=$((SY + H + IGAP))
W=$(((SW + IGAP) / $(wc -l < "$botlist") - IGAP))
H=$((SH - H - IGAP))
while read -r line; do
wid="${line##* }"
wtp "$X" "$Y" "$W" "$H" "$wid"
X=$((X + W + IGAP))
done < "$botlist"
}
main() {
. fwmrc
wmenv
wmgaps
case $# in
0)
usage 1
;;
1)
SCR="$(mattr i "$PFW" || pfm)"
;;
2)
case "$2" in
"SEC")
SCR="$(printf '%s\n' "$SEC" | head -n 1)"
;;
*)
mattr "$2" && SCR="$2" || {
printf '%s\n\n' "$2 is not a connected screen."
usage 1
}
;;
esac
;;
esac
# save windows to tmp in window stack order
winlist="$(mktemp)"
wattr xywhi $(listwindows "$SCR") > "$winlist"
nWindows="$(wc -l < "$winlist")"
# test if possible to tile
[ "$nWindows" -eq 0 ] && exit 2
# grab screen variables
case "$SCR" in
"$PRI")
SX=$(($(mattr x "$SCR") + LGAP))
SY=$(($(mattr y "$SCR") + TGAP))
SW=$(($(mattr w "$SCR") - LGAP - RGAP))
SH=$(($(mattr h "$SCR") - TGAP - BGAP))
;;
"$SEC")
SX=$(($(mattr x "$SCR") + LSEC))
SY=$(($(mattr y "$SCR") + TSEC))
SW=$(($(mattr w "$SCR") - LSEC - RSEC))
SH=$(($(mattr h "$SCR") - TSEC - BSEC))
;;
esac
case "$1" in
-g|--grid|grid) grid ;;
-s|--split|split) split ;;
-v|--vert|vert) vertical ;;
-l|--left|left) left ;;
-r|--right|right) right ;;
-b|--browse|browse) browse ;;
-h|--help|help) usage 0 ;;
*) usage 1 ;;
esac
# move mouse to middle of window
widCheck "$PFW" && {
wmp -a $(($(wattr x "$PFW") + $(wattr w "$PFW") / 2 + BW)) \
$(($(wattr y "$PFW") + $(wattr h "$PFW") / 2 + BW))
}
# clean temp file
rm "$winlist"
}
main "$@"