dot

vizie's NixOS desktop: Hyprland, CachyOS kernel and gaming tweaks

36 lines1472 bytesBourne Again Shell (bash)executable
1#!/usr/bin/env bash
2# Network state as one line of JSON for the Eww control center. Wired wins over
3# Wi-Fi when both are up.
4ICON_WIRED=$'\U000F0200' # nf-md-ethernet
5ICON_WIFI=$'\U000F05A9' # nf-md-wifi
6ICON_OFF=$'\U000F0202' # nf-md-ethernet_off
7
8state() { # available on connected icon label
9 printf '{"available":%s,"on":%s,"connected":%s,"icon":"%s","label":"%s"}\n' "$@"
10}
11
12command -v nmcli >/dev/null 2>&1 || { state false false false "$ICON_OFF" "Unavailable"; exit 0; }
13networking=$(timeout 2 nmcli networking 2>/dev/null) || { state false false false "$ICON_OFF" "Unavailable"; exit 0; }
14[ "$networking" = enabled ] || { state true false false "$ICON_OFF" "Off"; exit 0; }
15
16# DEVICE:TYPE:STATE:CONNECTION
17devices=$(timeout 2 nmcli -t -f DEVICE,TYPE,STATE,CONNECTION device 2>/dev/null)
18wired=$(grep -m1 -E '^[^:]+:ethernet:connected:' <<<"$devices")
19wifi=$(grep -m1 -E '^[^:]+:wifi:connected:' <<<"$devices")
20
21if [ -n "$wired" ]; then
22 dev=${wired%%:*}
23 speed=$(cat "/sys/class/net/$dev/speed" 2>/dev/null)
24 label="Wired"
25 if [ "${speed:-0}" -ge 1000 ] 2>/dev/null; then
26 label+=" · $(awk "BEGIN { print $speed / 1000 }") Gb/s"
27 elif [ "${speed:-0}" -gt 0 ] 2>/dev/null; then
28 label+=" · $speed Mb/s"
29 fi
30 state true true true "$ICON_WIRED" "$label"
31elif [ -n "$wifi" ]; then
32 name=$(cut -d: -f4- <<<"$wifi" | tr -d '"\\')
33 state true true true "$ICON_WIFI" "${name:-Wi-Fi}"
34else
35 state true true false "$ICON_OFF" "Disconnected"
36fi