Files
configs/waybar/scripts/network.sh
T
2026-04-19 18:05:23 +01:00

40 lines
1.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Fetch the active connection name and type
# -t: Terse output
# -f: Specific fields
ACTIVE_CONN=$(nmcli -t -f NAME,TYPE connection show --active | grep -E '802-3-ethernet|802-11-wireless' | head -n 1)
# If no connection is found, output disconnected icon
if [ -z "$ACTIVE_CONN" ]; then
echo "{\"text\": \"󰤭 \", \"class\": \"disconnected\"}"
exit 0
fi
# Extract the name and type using parameter expansion (faster than cut)
CONN_NAME="${ACTIVE_CONN%%:*}"
CONN_TYPE="${ACTIVE_CONN##*:}"
# Output JSON for Waybar custom module
if [[ "$CONN_TYPE" == *"802-11-wireless"* ]]; then
# It's Wi-Fi
SIGNAL=$(nmcli -t -f IN-USE,SIGNAL dev wifi | grep '^\*' | cut -d':' -f2)
if [ -z "$SIGNAL" ]; then
ICON="󰤟"
elif [ "$SIGNAL" -ge 80 ]; then
ICON="󰤨"
elif [ "$SIGNAL" -ge 60 ]; then
ICON="󰤥"
elif [ "$SIGNAL" -ge 40 ]; then
ICON="󰤢"
elif [ "$SIGNAL" -ge 20 ]; then
ICON="󰤟"
else
ICON="󰤯"
fi
echo "{\"text\": \"$CONN_NAME $ICON \", \"class\": \"wifi\"}"
else
# It's Ethernet
echo "{\"text\": \"$CONN_NAME 󰈀 \", \"class\": \"ethernet\"}"
fi