Browse Source

wifi: use color and nmcli -a for input

pull/6/head
DanielFGray 3 years ago
parent
commit
391ba67140
1 changed files with 8 additions and 37 deletions
  1. 8
    37
      wifi

+ 8
- 37
wifi View File

@@ -1,25 +1,5 @@
#!/usr/bin/env bash

ask() {
local opts=()
if [[ "$1" == '-1' ]]; then
opts+=( '-n1' )
one=1
shift
fi
if [[ "$1" == '-s' ]]; then
opts+=( '-s' )
shift
fi
read -e -r "${opts[@]}" -p "$* " ans
if [[ $one == 1 ]]; then
echo
[[ ${ans,} == y ]]
else
echo "$ans"
fi
}

has() {
local verbose=false
if [[ $1 == '-v' ]]; then
@@ -35,30 +15,21 @@ has() {
}

err() {
echo -e "\e[31m$1\e[0m" >&2
}

info() {
echo -e "\e[34m$1\e[0m" >&2
printf '\e[31m%s\e[0m\n' "$*" >&2
}

die() {
[[ -n "$1" ]] && err "$1"
(( $# > 0 )) && err "$*"
exit 1
}

fzf() {
command fzf --ansi --inline-info "$@"
}

has -v nmcli fzf || die

network=$(nmcli d wifi | fzf --header-lines=1 | sed -r 's/^\s*\*?\s*//; s/\s*Infra.*//')
network=$(nmcli --color yes device wifi | fzf --nth=2 --header-lines=1 | sed -r 's/^\s*\*?\s*//; s/\s*(Ad-Hoc|Infra).*//')
[[ -z "$network" ]] && exit

password=$(ask -s 'Password:')
echo

connect=$(nmcli device wifi connect "$network" password "$password")
if [[ "$connect" == *'successfully activated'* ]]; then
info "successfully connected to $network"
else
err "failed to connect to $network"
err "$connect"
fi
nmcli -a device wifi connect "$network"

Loading…
Cancel
Save