forked from wymiller/nix-config-v2
mod: show public ip in waybar
on click to show
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
"layer": "top",
|
||||
"modules-left": ["sway/workspaces"],
|
||||
"modules-center": ["clock"],
|
||||
"modules-right": ["custom/playerctl", "custom/weather", "pulseaudio", "network", "tray"],
|
||||
"modules-right": ["custom/playerctl", "custom/weather", "pulseaudio", "custom/network", "tray"],
|
||||
"sway/workspaces": {
|
||||
"disable-scroll": true,
|
||||
"all-outputs": true,
|
||||
@@ -34,11 +34,11 @@
|
||||
"exec": "wttrbar --fahrenheit --main-indicator 'temp_F' --location 'Grand Rapids'",
|
||||
"return-type": "json"
|
||||
},
|
||||
"network": {
|
||||
"format-wifi": " {essid} ({signalStrength}%)",
|
||||
"format-ethernet": " {ipaddr}",
|
||||
"format-disconnected": "⚠ Disconnected",
|
||||
"family": "ipv4",
|
||||
"custom/network": {
|
||||
"format": "{text}",
|
||||
"return-type": "json",
|
||||
"exec": "bash $HOME/.config/waybar/network.sh",
|
||||
"on-click": "bash $HOME/.config/waybar/network.sh toggle",
|
||||
"interval": 5,
|
||||
"tooltip": false
|
||||
},
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
#!/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"}'
|
||||
@@ -31,7 +31,7 @@ window#waybar {
|
||||
#cpu,
|
||||
#memory,
|
||||
#pulseaudio,
|
||||
#network,
|
||||
#custom-network,
|
||||
#battery,
|
||||
#clock,
|
||||
#custom-playerctl {
|
||||
@@ -65,7 +65,7 @@ window#waybar {
|
||||
animation-direction: alternate;
|
||||
}
|
||||
|
||||
#network.disconnected {
|
||||
#custom-network.disconnected {
|
||||
color: #cc241d;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user