Bash · 1381 bytes Raw Blame History
1 #!/bin/bash
2
3 # Get wifi interface name
4 WIFI_DEVICE=$(networksetup -listallhardwareports | awk '/Wi-Fi/{getline; print $2}')
5
6 # Get wifi info using networksetup
7 WIFI_STATE=$(networksetup -getairportpower "$WIFI_DEVICE" | awk '{print $NF}')
8 CURRENT_NETWORK=$(networksetup -getairportnetwork "$WIFI_DEVICE" | awk -F": " '{print $2}')
9
10 # Use system_profiler for signal strength (returns RSSI in dBm)
11 SIGNAL_INFO=$(system_profiler SPAirPortDataType 2>/dev/null | awk '/Signal \/ Noise/ {print $4; exit}')
12 RSSI=$SIGNAL_INFO
13 [[ -z "$RSSI" ]] && RSSI=0
14
15 # Function to get appropriate icon based on signal strength
16 get_wifi_icon() {
17 local strength=$1
18
19 if [[ -z "$strength" ]] || [[ "$WIFI_STATE" == "Off" ]]; then
20 echo "󰤮" # Disconnected
21 return
22 fi
23
24 # RSSI is negative, so we compare directly
25 if (( strength >= -50 )); then
26 echo "󰤨" # Full (greater than -50 dBm)
27 elif (( strength >= -60 )); then
28 echo "󰤥" # Good (between -60 and -50 dBm)
29 elif (( strength >= -70 )); then
30 echo "󰤢" # Fair (between -70 and -60 dBm)
31 else
32 echo "󰤟" # Poor (less than -70 dBm)
33 fi
34 }
35
36 # Get icon
37 ICON=$(get_wifi_icon "$RSSI")
38
39 # Update sketchybar
40 if [[ "$WIFI_STATE" == "On" ]]; then
41 sketchybar --set wifi icon="$ICON" label="${RSSI}dBm"
42 else
43 sketchybar --set wifi icon="󰤮" label="Not Connected"
44 fi