neodotfiles/user/.config/suckless/dwmblocks/scripts/block_wifi

51 lines
1.5 KiB
Text
Raw Normal View History

2023-09-04 17:36:18 +00:00
#!/usr/bin/env bash
2023-09-04 17:36:18 +00:00
# ***This script was made by Clay Gomera (Drake)***
# - Description: A dwmblocks script to print the wifi status
# - Dependencies: dwm, dwmblocks, nmcli
#####
## This function gets the wifi
## signal strength from nmcli and
## then converts it from dBm to
## values from 1 to 4
#####
## param: none
## return: string
#####
get_wifi_strength() {
# Retrieves the wifi signal strength in dBm
strength=$(nmcli -t -f active,ssid,signal dev wifi | grep yes | cut -d ":" -f3)
# Converts dBm to percentage (0% to 100%)
if [ "$strength" -le 30 ]; then
echo "1"
elif [ "$strength" -le 60 ]; then
echo "2"
elif [ "$strength" -le 90 ]; then
echo "3"
else
echo "4"
fi
}
2023-09-04 17:36:18 +00:00
# this variable will store the current state of the connection (connected or disconnected)
2022-09-14 00:44:31 +00:00
constate=$(nmcli dev | grep wifi | sed 's/ \{2,\}/|/g' | cut -d '|' -f3 | head -1)
2023-09-04 17:36:18 +00:00
# this variable will store the name of the wifi network that the computer is currently connected to
2022-09-14 00:44:31 +00:00
currentwfi=$(nmcli dev | grep wifi | sed 's/ \{2,\}/|/g' | cut -d '|' -f4 | head -1)
2023-09-04 17:36:18 +00:00
if [ "$constate" = "disconnected" ]; then # if the computer is disconnected
2023-05-26 06:32:03 +00:00
echo " 󰤮 "
2023-09-04 17:36:18 +00:00
elif [ "$constate" = "connected" ]; then # if it's connected
strength=$(get_wifi_strength)
case "$strength" in
"1") icon=" 󰤟 " ;;
"2") icon=" 󰤢 " ;;
"3") icon=" 󰤥 " ;;
"4") icon=" 󰤨 " ;;
esac
echo "$icon$currentwfi"
2022-09-14 00:44:31 +00:00
else
2023-09-04 17:36:18 +00:00
echo " 󰤮 " # just in case if nmcli isn't available or something weird is happening
2022-09-14 00:44:31 +00:00
fi