-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshmenu
executable file
·96 lines (79 loc) · 2.21 KB
/
sshmenu
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
#!/bin/sh
#
# sshmenu
usage() {
base=$(basename "$0")
cat >&2 << EOF
Usage:
$ $base
EOF
[ $# -eq 0 ] || exit "$1"
}
width() {
ps=$(txtw -f "$FONT" "$PROMPT")
char="$(printf '%s\n' "$hosts" | wc -L)"
W=$(echo "($ps + $char * 4.9 + 8) / 1" | bc)
}
menu() {
# menu geometry
X0="$(mattr x "$PFM")"
Y0="$(mattr y "$PFM")"
mouse="$(wmp)"
X=$((${mouse% *} - X0 - 20))
Y=$((${mouse#* } - Y0 - 20))
}
center() {
width
H=$((nHosts * FH))
# finding x y cords for centering
if [ "$PFW" = "$(lsw -r)" ]; then
X=$(($(mattr w "$PFM")/2 - W/2 - 10))
Y=$(($(mattr h "$PFM")/2 - H/2 - 10))
elif [ "$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
}
main() {
. fwmrc
wmenv
wmgaps
dcolours
PFM="$(pfm)"
FONT=$(awk '/font/ {print $3}' < ~/.Xresources | cut -d',' -f 1)
FH=15
W="150"
PROMPT=" Mosh >"
hosts="$(grep -w "host" ~/.ssh/config | cut -d\ -f 2)"
nHosts=$(printf '%s\n' "$hosts" | grep -cv "$PFW")
[ "$nHosts" -eq 0 ] && exit 1
case "$1" in
-m|menu) menu ;;
-c|center) center ;;
-h|--help|help) usage 0 ;;
*) center ;;
esac
# unfocus from current window to add contrast
focus -u
# grab hostname
host=$(printf '%s\n' "$hosts" | \
dmenu -name "$(basename "$0")" -f -l "$nHosts" -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)
# if dmenu returns no output refocus previous window
# else connect to hostname
if [ -n "$host" ]; then
urxvtc -g 85x22 -e sh -c "mosh $host || ssh $host"
else
focus -w "$PFW"
fi
}
main "$@"