From cc10c92af6403e5e7bec22beb069e668230d570e Mon Sep 17 00:00:00 2001 From: Uyanide Date: Sun, 3 Aug 2025 17:26:49 +0200 Subject: [PATCH] waybar: publicip module --- waybar/config.jsonc | 37 ++++++++++++++++++------------ waybar/modules/.gitignore | 3 +++ waybar/modules/publicip.sh | 47 ++++++++++++++++++++++++++++++++++++++ waybar/style.css | 19 ++++++++++----- waybar/style.css.template | 19 ++++++++++----- 5 files changed, 99 insertions(+), 26 deletions(-) create mode 100644 waybar/modules/.gitignore create mode 100755 waybar/modules/publicip.sh diff --git a/waybar/config.jsonc b/waybar/config.jsonc index 2430a7d..92f8bf6 100644 --- a/waybar/config.jsonc +++ b/waybar/config.jsonc @@ -119,7 +119,7 @@ }, // System monitors "group/monitors": { - "modules": ["network#speed", "temperature", "memory", "cpu", "battery", "backlight", "wireplumber"], + "modules": ["network#speed", "custom/publicip", "temperature", "memory", "cpu", "battery", "backlight", "wireplumber"], "orientation": "inherit" }, "network#speed": { @@ -133,9 +133,18 @@ "tooltip-format-wifi": "{essid} {signalStrength}%", "tooltip-format-ethernet": "{ifname} 󰌘", "tooltip-format-disconnected": "󰌙 Disconnected", - "max-length": 24, "min-length": 20 }, + "custom/publicip": { + "interval": 60, + "return-type": "json", + "format": " {text}", + "tooltip-format": "{alt}", + "max-length": 6, + "min-length": 6, + "exec": "$HOME/.config/waybar/modules/publicip.sh", + "on-click": "rm -f $HOME/.config/waybar/modules/publicip.cache && sleep 0.1" + }, "temperature": { "interval": 5, "thermal-zone": 6, @@ -145,23 +154,23 @@ "format-critical": " {temperatureC}°C", "format": "{icon} {temperatureC}°C", "format-icons": ["", "", ""], - "max-length": 7, - "min-length": 7 + "max-length": 6, + "min-length": 6 }, "memory": { "interval": 11, // "format": " {used:0.2f} / {total:0.0f} GB", "format": "󰍛 {percentage}%", "on-click": "killall btop || ghostty -e btop", - "min-length": 7, - "max-length": 7 + "max-length": 6, + "min-length": 6 }, "cpu": { "interval": 3, //"format": " {}%", // Icon: microchip "format": "󰘚 {usage}%", - "max-length": 7, - "min-length": 7, + "max-length": 6, + "min-length": 6, "on-click": "killall btop || ghostty -e btop" }, "battery": { @@ -175,8 +184,8 @@ "format-charging": " {capacity}%", "format-plugged": " {capacity}%", "format-icons": ["", "", "", "", ""], - "max-length": 7, - "min-length": 7 + "max-length": 6, + "min-length": 6 }, "backlight": { "device": "$DISPLAY_DEVICE", @@ -187,8 +196,8 @@ "format-icons": [""], "on-scroll-down": "brightnessctl -d $DISPLAY_DEVICE set 5%-", "on-scroll-up": "brightnessctl -d $DISPLAY_DEVICE set +5%", - "max-length": 7, - "min-length": 7 + "max-length": 6, + "min-length": 6 }, "wireplumber": { "on-click": "pavucontrol", @@ -208,8 +217,8 @@ "car": "", "default": ["", "", "", "", "", ""] }, - "max-length": 7, - "min-length": 7 + "max-length": 6, + "min-length": 6 }, // Hyprland "group/workspaceactions": { diff --git a/waybar/modules/.gitignore b/waybar/modules/.gitignore new file mode 100644 index 0000000..3a64743 --- /dev/null +++ b/waybar/modules/.gitignore @@ -0,0 +1,3 @@ +publicip.conf +publicip.cache +publicip.log \ No newline at end of file diff --git a/waybar/modules/publicip.sh b/waybar/modules/publicip.sh new file mode 100755 index 0000000..21ac3fe --- /dev/null +++ b/waybar/modules/publicip.sh @@ -0,0 +1,47 @@ +#!/bin/env bash +# shellcheck disable=SC1091 + +# Entries in publicip.conf: +# IP_QUERY_URL: URL to query public IP of the system. +# param: none +# return: JSON object with "ip" field. +# note: This URL will be queried with short intervals (60s for example), +# therefore it may not be a good idea to use a public API with +# a limited number of calls. +# IP_INFO_URL: URL to query IP location. +# param: +# return: JSON object with "country_code" field. +# note: This URL will only be quetried when public IP changes or when "force" is given as parameter. + +path="$(dirname "$(readlink -f "$0")")" +cache_file="$path/publicip.cache" +config_file="$path/publicip.conf" +time_log="$path/publicip.log" + +[ -f "$config_file" ] || exit 1 +. "$config_file" +[ -z "$IP_QUERY_URL" ] && exit 1 +[ -z "$IP_INFO_URL" ] && exit 1 +[ "$1" == "force" ] && rm -f "$cache_file" +[ -f "$cache_file" ] && . "$cache_file" + + +ip_current=$(curl -s -L "$IP_QUERY_URL" | jq -r '.ip') +[ -z "$ip_current" ] && exit 1 + +if [ "$ip_current" != "$CACHED_IP" ]; then + echo "$(date +%Y-%m-%dT%H:%M:%S) - IP changed: $CACHED_IP -> $ip_current" >> "$time_log" + CACHED_IP="$ip_current" + + ip_info_url=${IP_INFO_URL///$ip_current} + CACHED_CODE=$(curl -s -L "$ip_info_url" | jq -r '.country_code') + [ -z "$CACHED_CODE" ] && CACHED_CODE="N/A" + echo "CACHED_IP=$CACHED_IP" > "$cache_file" + echo "CACHED_CODE=$CACHED_CODE" >> "$cache_file" + notify-send "New Public IP detected" "New IP: $ip_current\nCountry: $CACHED_CODE" +fi + +jq -n --unbuffered --compact-output \ + --arg ip "$CACHED_IP" \ + --arg country "$CACHED_CODE" \ + '{alt: $ip, text: $country}' \ No newline at end of file diff --git a/waybar/style.css b/waybar/style.css index b39e9d4..724c938 100644 --- a/waybar/style.css +++ b/waybar/style.css @@ -116,6 +116,7 @@ tooltip { #custom-hyprPicker, #custom-power-menu, #custom-spotify, +#custom-publicip, #custom-weather, #custom-weather.severe, #custom-weather.sunnyDay, @@ -144,27 +145,33 @@ tooltip { padding: 0; } +#custom-publicip { + background: transparent; + color: @peach; + padding: 0; +} + #temperature { background: transparent; - color: @maroon; + color: @yellow; padding: 0px; } #memory { - color: @peach; + color: @green; background: transparent; padding: 0px; } #cpu { - color: @yellow; + color: @teal; background: transparent; padding: 0px; } #battery { background: transparent; - color: #c0caf5; + color: @sapphire; padding: 0; } @@ -176,13 +183,13 @@ tooltip { #backlight { background: transparent; - color: @teal; + color: @blue; padding: 0; } #wireplumber { background: transparent; - color: @sapphire; + color: @lavender; padding: 0; } diff --git a/waybar/style.css.template b/waybar/style.css.template index 8e1aa5f..8eb22ec 100644 --- a/waybar/style.css.template +++ b/waybar/style.css.template @@ -116,6 +116,7 @@ tooltip { #custom-hyprPicker, #custom-power-menu, #custom-spotify, +#custom-publicip, #custom-weather, #custom-weather.severe, #custom-weather.sunnyDay, @@ -144,27 +145,33 @@ tooltip { padding: 0; } +#custom-publicip { + background: transparent; + color: @peach; + padding: 0; +} + #temperature { background: transparent; - color: @maroon; + color: @yellow; padding: 0px; } #memory { - color: @peach; + color: @green; background: transparent; padding: 0px; } #cpu { - color: @yellow; + color: @teal; background: transparent; padding: 0px; } #battery { background: transparent; - color: #c0caf5; + color: @sapphire; padding: 0; } @@ -176,13 +183,13 @@ tooltip { #backlight { background: transparent; - color: @teal; + color: @blue; padding: 0; } #wireplumber { background: transparent; - color: @sapphire; + color: @lavender; padding: 0; }