-
Notifications
You must be signed in to change notification settings - Fork 1
/
rofi_iwd.sh
executable file
·196 lines (171 loc) · 4.56 KB
/
rofi_iwd.sh
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
#!/usr/bin/env zsh
set -ex
script=`basename "$0"`
pathname=`dirname "$0"`
help="$script [-h/--help] -- script to connect to wlan with iwd
Usage:
depending on how the script is named,
it will be executed either with dmenu, with rofi
or wofi.
Examples:
dmenu_iwd.sh
rofi_iwd.sh
fuzzel_iwd.sh
wofi_iwd.sh"
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
printf "%s\n" "$help"
exit 0
fi
function wofi() {
command wofi -d -I "$@"
}
function rofi() {
command rofi -m -1 -dmenu -i "$@"
}
function dmenu() {
command dmenu "$@"
}
function fuzzel() {
command fuzzel -w 75 -d "$@"
}
function ssid-scan(){
eval "$pathname/iwd-scan.py $@"
}
function percentage() {
if [[ -z "$strength" ]]
then
return
fi
offset=$(( $strength + 110 ))
progress=$(( $offset * 100 / 180 ))
progress=""
k=$(( $offset / 10))
echo $offset $k
progress="["
for ((ii = 0 ; ii <= k; ii++)); do progress="$progress█"; done
for ((j = ii ; j <= 10 ; j++)); do progress="$progress "; done
progress="$progress] "
}
case $script in
dmenu_*)
label_interface="interface »"
menu_interface="selection=`dmenu -l 3 -c -bw 2 -r -i -p $label_interface` print ${selection%% *}"
label_ssid="ssid »"
menu_ssid="dmenu -l 10 -c -bw 2 -r -i -p $label_ssid"
label_psk="passphrase »"
menu_psk="dmenu -l 1 -c -bw 2 -i -p $label_psk"
;;
rofi_*)
label_interface=""
menu_interface="rofi -l 3 -p $label_interface"
label_ssid=""
menu_ssid="rofi -p $label_ssid"
label_psk="🔒"
menu_psk="rofi -I -p $label_psk"
;;
wofi_*)
label_interface=""
menu_interface="wofi -l 3 -p $label_interface"
label_ssid=""
menu_ssid="wofi -p $label_ssid"
label_psk="🔒"
menu_psk="wofi -I -p $label_psk"
;;
fuzzel_*)
label_interface=""
menu_interface="fuzzel -p $label_interface"
label_ssid=""
menu_ssid="fuzzel -p $label_ssid"
label_psk="🔒"
menu_psk="fuzzel -I -p $label_psk"
;;
*)
printf "%s\n" "$help"
exit 1
;;
esac
remove_escape_sequences() {
tail -n +5 \
| sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g;/^\s*$/d"
}
get_interface() {
interface=$(iwctl device list \
| remove_escape_sequences \
| awk '{printf("%-12s %-9s %s\n", $1, $2, $3)}' \
| eval $menu_interface \
| awk '{print $1}'
)
[ -n "$interface" ] \
|| exit 1
}
scan_ssid() {
scan_result=("${(@f)$(ssid-scan)}")
scan_result_formatted=""
for (( i=1; i<$#scan_result; i+=4 ))
do
ssid=${scan_result[$i]}
strength=${scan_result[$i+1]}
security=${scan_result[$i+2]}
connected=${scan_result[$i+3]}
if [[ $connected = "1" ]]
then
connected="✅"
else
connected=" "
fi
percentage
echo $progress
printf -v scan_result_formatted "$scan_result_formatted%s%-42ls %s dBm %-6s %s\n" $connected $ssid $strength $security $progress
done
}
get_ssid() {
select=`echo "🔄[RESCAN]\n"$scan_result_formatted | eval $menu_ssid`
if [[ -z "$select" ]]
then
exit
elif [[ "$select" = "🔄[RESCAN]" ]]
then
# Rescan is always thu first option
scan_ssid
get_ssid
return
fi
# we have to convert the selected item to an index because not all the menu
# script tools support outputting the selected index
to_array=(${(f)scan_result_formatted})
selectedIndex=${to_array[(Ie)$select]}
print $selectedIndex
selectedIndex=$(( $(( $selectedIndex - 1 )) * 4 ))
selectedIndex=$(( $selectedIndex + 1 ))
i_ssid=${scan_result[$selectedIndex]}
i_strength=${scan_result[$selectedIndex +1]}
i_security=${scan_result[$selectedIndex+2]}
i_connected=${scan_result[$selectedIndex+3]}
if [[ $i_connected = "1" ]]
then
i_connected="✅"
else
i_connected=" "
fi
print "found a match $i_ssid"
ssid=$i_ssid
}
get_psk() {
psk=$(printf 'press esc or enter if you had already insert a passphrase before!\n' \
| eval $menu_psk \
)
}
connect_iwd() {
if [[ "$open" = 1 ]]
then
iwctl station "$interface" connect ''$ssid''
else
get_psk
iwctl station "$interface" connect "$ssid" -P "$psk"
fi
notify-send "iNet wireless daemon" "connected to \"$ssid\""
}
get_interface \
&& scan_ssid \
&& get_ssid \
&& connect_iwd