#!/usr/bin/env bash set -u state_dir="${XDG_RUNTIME_DIR:-/tmp}/waybar-${UID}" state_file="$state_dir/public-ip" if [[ ${1:-} == toggle ]]; then mkdir -p -m 700 "$state_dir" if [[ -s $state_file ]]; then : > "$state_file" else public_ip=$(curl -4fsS --max-time 5 https://wtfismyip.com/json 2>/dev/null | jq -er '.YourFuckingIPAddress | select(type == "string") | split(".") | select(length == 4 and all(.[]; test("^[0-9]{1,3}$") and (tonumber <= 255))) | join(".")' 2>/dev/null) || exit 0 printf '%s\n' "$public_ip" > "$state_file" fi exit 0 fi route=$(ip -4 route get 1.1.1.1 2>/dev/null) || route= if [[ ! $route =~ [[:space:]]dev[[:space:]]([^[:space:]]+) ]]; then jq -cn '{text: "⚠ Disconnected", class: "disconnected"}' exit 0 fi interface=${BASH_REMATCH[1]} if [[ ! $route =~ [[:space:]]src[[:space:]]([0-9.]+) ]]; then jq -cn '{text: "⚠ Disconnected", class: "disconnected"}' exit 0 fi local_ip=${BASH_REMATCH[1]} if [[ -s $state_file ]]; then jq -cn --rawfile ip "$state_file" '{text: (" " + ($ip | rtrimstr("\n"))), class: "public"}' exit 0 fi if [[ $(nmcli -g GENERAL.TYPE device show "$interface" 2>/dev/null) == wifi ]]; then essid=$(nmcli -g GENERAL.CONNECTION device show "$interface" 2>/dev/null | head -n 1) signal=$(nmcli -t -f IN-USE,SIGNAL device wifi list ifname "$interface" --rescan no 2>/dev/null | awk -F: '$1 == "*" { print $2; exit }') if [[ -n $essid && $essid != -- ]]; then text=" $essid" [[ -n $signal ]] && text="${text} (${signal}%)" else text=" $local_ip" fi else text=" $local_ip" fi jq -cn --arg text "$text" '{text: $text, class: "connected"}'