-
Notifications
You must be signed in to change notification settings - Fork 2
/
triplebinpack
executable file
·99 lines (86 loc) · 2.28 KB
/
triplebinpack
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
#!/bin/sh
# If initial binpack fails, remove second largest window into seperate grouping, and try again.
width=$1; width=$((width / 3))
offset=$1; offset=$((offset / 3))
doubleoffset=$(($((offset * 2)) + $((gap * 3))))
offset=$((offset + $((gap * 3))))
heigh=$2
gap=$3
# We center around second monitor.
first=""
second=""
third=""
# stash our data
while read -r input; do
second="$input $second"
done
# Check if we have more than one window
windows=$(echo "$second" | wc -w); windows=$((windows / 5))
if [ $windows -eq 1 ]; then
echo "$second" | binpack -x "$width" -y "$heigh" -g "$gap" | awk '{print $1 + "'"$offset"'", $2, $3, $4, $5}'
exit
fi
matchy() {
awk '{
for(i=1;i<=NF;i++) {
if($i == "'"$1"'") {
print $(i-4) OFS $(i-3) OFS $(i-2) OFS $(i-1) OFS $i
}
}
}'
}
grok() {
first=0
second=0
firstid=""
secondid=""
while read -r minw minh maxw maxh id; do
max=$((maxw * maxh))
if test "$max" -gt "$first"; then
# New top window
second="$first"
first="$max"
# Stash the ID
secondid="$firstid"
firstid="$id"
continue
fi
if test "$max" -gt "$second"; then
second="$max"
secondid=$id
fi
done
echo "$secondid"
}
split() {
while read -r minw minh maxw maxh id; do
printf "%s %s %s %s %s\n" $minw $minh $maxw $maxh $id
done
}
flag=0
while :; do
# Capture remaining amount of windows
windows=$(echo "$second" | wc -w); windows=$((windows / 5 ))
# Exit when we're on the last window
if [ $windows -eq 1 ]; then
break
fi
# simplepack fails or succeeds
if echo "$second" | split | simplepack -x "$width" -y "$heigh" -g "$gap"; then
break
fi
# Grab the second biggest window, push it to one of the flanking windows
ID=$(echo "$second" | split | grok )
toflank="$(echo "$second" | matchy $ID)"
second="$(echo "$second" | sed 's/'"$toflank"'//')"
# Use ID to match input string
case $flag in
0) flag=1
first="$toflank $first" ;;
1) flag=0
third="$toflank $third" ;;
esac
done
test "$first" != "" && echo "$first" | split | binpack -x "$width" -y "$heigh" -g "$gap"
echo "$second" | split | binpack -x "$width" -y "$heigh" -g "$gap" | awk '{print $1 + "'"$offset"'", $2, $3, $4, $5}'
test "$third" != "" && echo "$third" | split | binpack -x "$width" -y "$heigh" -g "$gap" | awk '{print $1 + "'"$doubleoffset"'", $2, $3, $4, $5}'