-
Notifications
You must be signed in to change notification settings - Fork 0
/
wmenu
executable file
·118 lines (96 loc) · 2.64 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
#!/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
}
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))
W=$(($(mattr w "$PRI") - LGAP - RGAP - BW*2))
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 "$((X + 40))" "$(($(mattr y "$PRI") + Y + 20))"
}
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 ;;
-h|help) usage 0 ;;
*) usage 1 ;;
esac
focus -w "$(selection)"
}
main "$@"