From fe6a4af7e6f25efa8da6a4ab346c12ecf8b90c01 Mon Sep 17 00:00:00 2001 From: ulve Date: Sat, 13 Jun 2026 18:20:48 +0200 Subject: [PATCH] waybar: add network widget, margins, border and rounded corners Co-Authored-By: Claude Sonnet 4.6 --- config/waybar/config | 10 +++++++++- config/waybar/network.sh | 35 +++++++++++++++++++++++++++++++++++ config/waybar/style.css | 9 ++++++++- home.nix | 4 ++++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 config/waybar/network.sh diff --git a/config/waybar/config b/config/waybar/config index 4d04955..8bed39a 100644 --- a/config/waybar/config +++ b/config/waybar/config @@ -2,6 +2,8 @@ "layer": "top", "position": "top", "height": 24, + "margin-left": 250, + "margin-right": 250, "modules-left": ["custom/nurgle", "sway/workspaces"], "custom/nurgle": { @@ -9,7 +11,7 @@ "tooltip": false }, "modules-center": ["clock"], - "modules-right": ["cpu", "battery"], + "modules-right": ["custom/network", "cpu", "battery"], "sway/workspaces": { "disable-scroll": true @@ -20,6 +22,12 @@ "interval": 60 }, + "custom/network": { + "exec": "~/.local/bin/waybar-network.sh", + "interval": 2, + "format": "{}" + }, + "cpu": { "format": "CPU {usage}%", "interval": 2 diff --git a/config/waybar/network.sh b/config/waybar/network.sh new file mode 100644 index 0000000..30fdeef --- /dev/null +++ b/config/waybar/network.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +CACHE="/tmp/waybar-net" +iface=$(ip route get 8.8.8.8 2>/dev/null | awk '{for(i=1;i<=NF;i++) if($i=="dev") print $(i+1)}' | head -1) + +if [ -z "$iface" ]; then + echo "↑0B ↓0B" + exit 0 +fi + +rx=$(cat /sys/class/net/$iface/statistics/rx_bytes 2>/dev/null || echo 0) +tx=$(cat /sys/class/net/$iface/statistics/tx_bytes 2>/dev/null || echo 0) +now=$(date +%s%3N) + +if [ -f "$CACHE" ]; then + read -r prev_rx prev_tx prev_time < "$CACHE" + dt=$(( now - prev_time )) + [ "$dt" -le 0 ] && dt=1000 + up=$(( (tx - prev_tx) * 1000 / dt )) + down=$(( (rx - prev_rx) * 1000 / dt )) +else + up=0; down=0 +fi + +echo "$rx $tx $now" > "$CACHE" + +fmt() { + local b=$1 + if [ "$b" -ge 1073741824 ]; then printf "%4s" "$(( b / 1073741824 ))G" + elif [ "$b" -ge 1048576 ]; then printf "%4s" "$(( b / 1048576 ))M" + elif [ "$b" -ge 1024 ]; then printf "%4s" "$(( b / 1024 ))K" + else printf "%4s" "${b}B" + fi +} + +echo "↑$(fmt "$up")/s ↓$(fmt "$down")/s" diff --git a/config/waybar/style.css b/config/waybar/style.css index 8e5901c..d9085cb 100644 --- a/config/waybar/style.css +++ b/config/waybar/style.css @@ -1,6 +1,6 @@ * { font-family: JetBrainsMono Nerd Font; - font-size: 13px; + font-size: 18px; border: none; border-radius: 0; min-height: 0; @@ -9,6 +9,8 @@ window#waybar { background: #0a0a0a; color: #f3eeea; + border: 1px solid #560500; + border-radius: 8px; } #custom-nurgle { @@ -39,6 +41,11 @@ window#waybar { color: #f3eeea; } +#custom-network { + color: #f3eeea; + padding: 0 8px; +} + #cpu { color: #f3eeea; padding: 0 8px; diff --git a/home.nix b/home.nix index c2299f4..1aa3648 100644 --- a/home.nix +++ b/home.nix @@ -243,6 +243,10 @@ source = ./config/sway/wallpaper.sh; executable = true; }; + home.file.".local/bin/waybar-network.sh" = { + source = ./config/waybar/network.sh; + executable = true; + }; xdg.configFile."waybar".source = ./config/waybar; xdg.configFile."foot".source = ./config/foot; xdg.dataFile."applications/switch-wallpaper.desktop".source = ./config/applications/switch-wallpaper.desktop;