-
Notifications
You must be signed in to change notification settings - Fork 0
/
wmenu
executable file
·165 lines (135 loc) · 3.78 KB
/
wmenu
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
#!/bin/sh
#
# wmenu
usage() {
base="$(basename "$0")"
cat >&2 << EOF
Usage:
$ $base -b | bar <-au> : Show as a bar on primary screen.
$ $base -m | menu <-au> : Show as a window.
$ $base -c | center <-au> : Show as a window centered above focused window.
$ $base -h | help : Show this help.
EOF
[ $# -eq 0 ] || exit "$1"
}
width() {
ps=$(txtw -f "$FONT" "$PROMPT")
char="$(output | wc -L)"
W=$(echo "($ps + $char * 4.9 + 8) / 1" | bc)
}
output() {
for wid in $($LSWCMD); do
printf '%s\n' "$wid $(atomx WM_CLASS "$wid") | $(wname "$wid")"
done | grep -v "$PFW"
}
selection() {
output | sort -rk 2 | \
dmenu -name "$(basename "$0")" -f -l "$nWindows" -i -fn "$FONT" -p "$PROMPT" \
-nf "#$NF" -sf "#$SF" -nb "#$NB" -sb "#$SB" -bc "#$BC" \
-s "$(mattr d "$PFM")" -x "$X" -y "$Y" -w "$W" -h "$FH" -bw "$BW" | \
cut -d\ -f 1
}
groupoutput() {
for wid in $(printf '%s\n' "$grplist" | grep "GROUP_$grp" | cut -d' ' -f 2); do
printf '%s\n' "$wid $(atomx WM_CLASS "$wid") | $(wname "$wid")"
done
}
groupselection() {
groupoutput | sort -rk 2 | \
dmenu -name "$(basename "$0")" -f -l "$nWindows" -i -fn "$FONT" -p "$PROMPT" \
-nf "#$NF" -sf "#$SF" -nb "#$NB" -sb "#$SB" -bc "#$BC" \
-s "$(mattr d "$PFM")" -x "$X" -y "$Y" -w "$W" -h "$FH" -bw "$BW" | \
cut -d\ -f 1
}
menu() {
width
# menu geometry
X0="$(mattr x "$PFM")"
Y0="$(mattr y "$PFM")"
mouse="$(wmp)"
X=$((${mouse% *} - X0 - 20))
Y=$((${mouse#* } - Y0 - 20))
}
center() {
width
H=$((nWindows * FH))
# finding x y cords for centering
if [ "$PFW" = "0x00000000" ]; then
X=$(($(mattr w "$PFM")/2 - W/2 - 10))
Y=$(($(mattr h "$PFM")/2 - H/2 - 10))
else
X0="$(mattr x "$PFM")"
Y0="$(mattr y "$PFM")"
X=$(($(wattr x "$PFW") + $(wattr w "$PFW")/2 - W/2 - X0))
Y=$(($(wattr y "$PFW") + $(wattr h "$PFW")/2 - H/2 - Y0))
# move mouse to optimal selection position
wmp -a $(($(wattr x "$PFW") + $(wattr w "$PFW") / 2 - W / 2 + 20)) \
$(($(wattr y "$PFW") + $(wattr h "$PFW") / 2 - H / 2 + 20))
fi
}
bar() {
X=$((LGAP))
width
if [ "$BAR" = "top" ]; then
H=$((TGAP/2 + BW*2))
Y=$((H - TGAP/4))
else
H=$((BGAP/2 + BW*2))
Y=$(($(mattr h "$PRI") - FH * (nWindows + 1) - BGAP))
fi
# position mouse
wmp -a "$(($(mattr x "$PRI") + 40))" "$(($(mattr y "$PRI") + Y + 20))"
}
groupbar() {
grp="$1"
grplist="$(group -l)"
case "$(printf '%s\n' "$grplist" | grep -v "$PFW" | grep -c "GROUP_$grp")" in
0)
exit 1
;;
*)
LSWCMD="printf '%s\n' $grplist | grep GROUP_$grp | cut -d' ' -f 2"
output
exit 0
;;
esac
}
main() {
. fwmrc
wmenv
wmgaps
dcolours
PFM="$(pfm)"
FONT=$(awk '/font/ {print $3}' < ~/.Xresources | cut -d',' -f 1)
FH=15
PROMPT=" Window >"
case "$2" in
-a|all) LSWCMD="lsw -a" ;;
-u|hid) LSWCMD="lsw -u" ;;
*)
LSWCMD="lsw"
;;
esac
nWindows=$($LSWCMD | grep -cv "$PFW")
[ "$nWindows" -eq 0 ] && exit 1
case "$1" in
-b|bar) bar ;;
-m|menu) menu ;;
-c|center) center ;;
-g|group) groupbar "$2" ;;
-h|help) usage 0 ;;
*) usage 1 ;;
esac
# unfocus from current window to add contrast
focus -u
# grab window id from user input
wid="$(selection)"
# if dmenu returns no output refocus previous window
# else focus selected window
if [ -n "$wid" ]; then
focus -w "$wid"
else
focus -w "$PFW"
fi
}
main "$@"