#!/usr/bin/env bash # Network state as one line of JSON for the Eww control center. Wired wins over # Wi-Fi when both are up. ICON_WIRED=$'\U000F0200' # nf-md-ethernet ICON_WIFI=$'\U000F05A9' # nf-md-wifi ICON_OFF=$'\U000F0202' # nf-md-ethernet_off state() { # available on connected icon label printf '{"available":%s,"on":%s,"connected":%s,"icon":"%s","label":"%s"}\n' "$@" } command -v nmcli >/dev/null 2>&1 || { state false false false "$ICON_OFF" "Unavailable"; exit 0; } networking=$(timeout 2 nmcli networking 2>/dev/null) || { state false false false "$ICON_OFF" "Unavailable"; exit 0; } [ "$networking" = enabled ] || { state true false false "$ICON_OFF" "Off"; exit 0; } # DEVICE:TYPE:STATE:CONNECTION devices=$(timeout 2 nmcli -t -f DEVICE,TYPE,STATE,CONNECTION device 2>/dev/null) wired=$(grep -m1 -E '^[^:]+:ethernet:connected:' <<<"$devices") wifi=$(grep -m1 -E '^[^:]+:wifi:connected:' <<<"$devices") if [ -n "$wired" ]; then dev=${wired%%:*} speed=$(cat "/sys/class/net/$dev/speed" 2>/dev/null) label="Wired" if [ "${speed:-0}" -ge 1000 ] 2>/dev/null; then label+=" · $(awk "BEGIN { print $speed / 1000 }") Gb/s" elif [ "${speed:-0}" -gt 0 ] 2>/dev/null; then label+=" · $speed Mb/s" fi state true true true "$ICON_WIRED" "$label" elif [ -n "$wifi" ]; then name=$(cut -d: -f4- <<<"$wifi" | tr -d '"\\') state true true true "$ICON_WIFI" "${name:-Wi-Fi}" else state true true false "$ICON_OFF" "Disconnected" fi