#!/usr/bin/env bash if ! command -v nmcli &>/dev/null; then echo "{\"text\": \"󰤫\", \"tooltip\": \"nmcli utility is missing\"}" exit 1 fi # Check if Wi-Fi is enabled wifi_status=$(nmcli radio wifi 2>/dev/null) if [ "$wifi_status" = "disabled" ]; then echo "{\"text\": \"󰤮\", \"tooltip\": \"Wi-Fi Disabled\"}" exit 0 fi # Initialize variables with defaults essid="No Connection" signal=0 tooltip="No Wi-Fi Connection" ip_address="Not Connected" security="None" chan="N/A" wifi_info=$(nmcli -t -f active,ssid,signal,security dev wifi 2>/dev/null | grep "^yes" | head -1) if [ -n "$wifi_info" ]; then # Parse the wifi info with error handling essid=$(echo "$wifi_info" | awk -F: '{print $2}') signal=$(echo "$wifi_info" | awk -F: '{print $3}') security=$(echo "$wifi_info" | awk -F: '{print $4}') # Set defaults if parsing failed [ -z "$essid" ] && essid="Unknown" [ -z "$signal" ] && signal=0 [ -z "$security" ] && security="None" # Get active WiFi device, ignoring WireGuard interfaces active_device=$(nmcli -t -f DEVICE,TYPE,STATE device status 2>/dev/null | grep -E 'wifi:connected$' | grep -v -E '^(wg0|wg1|wg2):' | awk -F: '{print $1}' | head -1) if [ -n "$active_device" ]; then # Get IP address with error handling ip_output=$(nmcli -e no -g ip4.address device show "$active_device" 2>/dev/null | head -1) [ -n "$ip_output" ] && ip_address="$ip_output" # Get channel and frequency info wifi_details=$(nmcli -e no -t -f active,bssid,chan,freq device wifi 2>/dev/null | grep "^yes" | head -1) if [ -n "$wifi_details" ]; then chan_temp=$(echo "$wifi_details" | awk -F: '{print $8}') freq_temp=$(echo "$wifi_details" | awk -F: '{print $9}') [ -n "$chan_temp" ] && [ -n "$freq_temp" ] && chan="$chan_temp ($freq_temp)" fi # Build tooltip with proper formatting tooltip="SSID: ${essid}" tooltip+="\\nIP Address: ${ip_address}" tooltip+="\\nSecurity: ${security}" tooltip+="\\nChannel: ${chan}" tooltip+="\\nStrength: ${signal}/100" else tooltip="Connected to: ${essid}\\nStrength: ${signal}/100\\nSecurity: ${security}" fi fi # Ensure signal is a number for comparison signal=$((signal + 0)) # Determine Wi-Fi icon based on signal strength if [ "$signal" -ge 80 ]; then icon="󰤨" # Strong signal elif [ "$signal" -ge 60 ]; then icon="󰤥" # Good signal elif [ "$signal" -ge 40 ]; then icon="󰤢" # Weak signal elif [ "$signal" -ge 20 ]; then icon="󰤟" # Very weak signal else icon="󰤯" # No signal fi # Output JSON with proper escaping echo "{\"text\": \"${icon}\", \"tooltip\": \"${tooltip}\"}"