Files
dotfiles/config/waybar/scripts/network-cycle.sh
T
owen 5c57ee20f7 Add Arch Wayland dotfiles: sway/waybar/kitty/nvim/zsh, idempotent installer
Auto-adapts sway workspace routing to however many monitors are
connected (laptop vs. desktop) instead of hardcoding output names.
2026-08-21 22:59:26 -05:00

54 lines
1.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
state_file="${XDG_RUNTIME_DIR:-/tmp}/waybar-network-state-${UID}"
current_ip_file="${XDG_RUNTIME_DIR:-/tmp}/waybar-network-current-ip-${UID}"
if [[ "${1:-}" == copy ]]; then
if [[ -s "$current_ip_file" ]]; then
current_ip=$(<"$current_ip_file")
printf '%s' "$current_ip" | wl-copy
notify-send -a Waybar -u low -r 9101 "IP address copied" "$current_ip"
fi
exit 0
fi
if [[ "${1:-}" == next ]]; then
state=0
[[ -r "$state_file" ]] && read -r state <"$state_file"
printf '%s\n' "$((state + 1))" >"$state_file"
pkill -RTMIN+8 waybar 2>/dev/null || true
exit 0
fi
entries=()
while read -r interface addresses; do
[[ -n "$interface" && -n "$addresses" ]] || continue
entries+=("$interface $addresses")
done < <(
ip -o -4 addr show scope global \
| awk '{sub(/\/.*/, "", $4); addresses[$2] = addresses[$2] (addresses[$2] ? "," : "") $4} END {for (interface in addresses) print interface, addresses[interface]}' \
| sort
)
public_ipv4=$(curl -4 -fsS --max-time 3 https://ifconfig.me/ip 2>/dev/null || true)
[[ -n "$public_ipv4" ]] && entries+=("public $public_ipv4")
if ((${#entries[@]} == 0)); then
entries+=("network unavailable")
fi
state=0
[[ -r "$state_file" ]] && read -r state <"$state_file"
index=$((state % ${#entries[@]}))
tooltip=$(printf '%s\n' "${entries[@]}")
current_ip="${entries[$index]#* }"
if [[ "$current_ip" == unavailable ]]; then
: >"$current_ip_file"
else
printf '%s' "$current_ip" >"$current_ip_file"
fi
jq -cn --arg text "󰈀 ${entries[$index]}" --arg tooltip "$tooltip" \
'{text: $text, tooltip: $tooltip}'