From 28b7dab48307af3f976a0db778ccbd5ad830633a Mon Sep 17 00:00:00 2001 From: Luca Bilke Date: Wed, 24 Jul 2024 12:00:45 +0200 Subject: [PATCH] sb-internet: rewrite with nmcli --- .local/libexec/statusbar/sb-internet | 101 ++++++++++++--------------- 1 file changed, 45 insertions(+), 56 deletions(-) diff --git a/.local/libexec/statusbar/sb-internet b/.local/libexec/statusbar/sb-internet index 1c1e42b66..a73be1591 100755 --- a/.local/libexec/statusbar/sb-internet +++ b/.local/libexec/statusbar/sb-internet @@ -1,73 +1,62 @@ #!/bin/sh -# TODO: rewrite this with nmcli - shift="" -# Ethernet -for if in /sys/class/net/*; do - [ "$(cat "$if/type")" = 1 ] || continue - - ifname=$(basename "$if") - - echo "$ifname" | grep -q \ - -e "^br" \ - -e "^docker" && - continue - - case "$(cat "$if/operstate")" in - up) eth_icon="󰱓" ;; - down) eth_icon="󰅛" ;; - *) eth_icon="󰛵" ;; - esac - printf "%b" "$shift$eth_icon $ifname" - shift=" " -done - -# WLAN -for if in /sys/class/net/*; do - eval "$(cat "$if/uevent")" - - [ "$DEVTYPE" = "wlan" ] || continue - unset DEVTYPE - - ifname=$(basename "$if") - - if grep -vxq '0x\(1\|9\)003' "$if/flags"; then - wifi_icon="󰀞" +wifi() { + if grep -vxq '0x\(1\|9\)003' "$info/flags"; then + icon="󰀞" else - case "$(cat "$if/operstate")" in - up) - percent=$(awk -v interface="$INTERFACE" '$1==interface {gsub(/[^0-9]/,"",$3); print $3}' /proc/net/wireless) + case "$state" in + connected) + percent=$(awk -v interface="$device:" '$1==interface {gsub(/[^0-9]/,"",$3); print $3}' /proc/net/wireless) # shellcheck disable=SC2194 case 1 in - $((percent >= 0))) wifi_icon="󰤯" ;; - $((percent >= 20))) wifi_icon="󰤟" ;; - $((percent >= 40))) wifi_icon="󰤢" ;; - $((percent >= 60))) wifi_icon="󰤥" ;; - $((percent >= 80))) wifi_icon="󰤨" ;; + $((percent >= 80))) icon="󰤨" ;; + $((percent >= 60))) icon="󰤥" ;; + $((percent >= 40))) icon="󰤢" ;; + $((percent >= 20))) icon="󰤟" ;; + $((percent >= 0))) icon="󰤯" ;; esac ;; - down) wifi_icon="󰤮" ;; - *) wifi_icon="󰤫" ;; + disconnected) icon="󰤮" ;; + *) icon="󰤫" ;; esac + printf "%b" "$shift$icon $connection" + shift=" " fi +} - printf "%b" "$shift$wifi_icon $ifname" +wireguard() { + printf "%b" "$shift󰖂 $connection" shift=" " -done +} -# Wireguard -for if in /sys/class/net/*; do - eval "$(cat "$if/uevent")" +ethernet() { + case "$state" in + connected) icon="󰱓" ;; + unavailable) icon="󰅛" ;; + disconnected) icon="󰲛" ;; + *) icon="󰛵" ;; + esac - [ "$DEVTYPE" = "wireguard" ] || continue - unset DEVTYPE - - ifname=$(basename "$if") - - printf "%b" "$shift󰖂 $ifname" + printf "%b" "$shift$icon" shift=" " -done +} -printf "\n" +fallback() { + : +} + +for x in $(nmcli --terse device status); do + device="$(echo "$x" | cut -d: -f1)" + type="$(echo "$x" | cut -d: -f2)" + state="$(echo "$x" | cut -d: -f3)" + connection="$(echo "$x" | cut -d: -f4)" + info="/sys/class/net/${device}" + + case "$type" in + wifi | wireguard | ethernet) $type ;; + *) fallback ;; + esac + unset device type state connection info icon +done