From 017668792ccb105c7d1416b9e5061144fb02e12d Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 26 Jul 2024 10:56:36 +0200 Subject: [PATCH] waybar: Improve vpn block icon rendering This is a change which is very much hardcoded for my setup, but the vpn block will now give preference to displaying individual VPN types from top to bottom: If privateinternetaccess is connected, it will display its icon. If proton is connected, it will display its icon. If netbird is connected it will display its icon. If nothing is connected it will display nothing. It is still quite a hacky solution and should also be replaced by a signal-driven system instead of the recurrent polling it does currently (it only polls once a minute atm, to keep system load/battery drain low but since it invokes a lot of external commands, e.g. piactl and netbird, it should really only be invoked on vpn changes). --- desktop/.config/waybar/config | 7 ++++-- desktop/.config/waybar/modules/wireguard | 28 ++++++++++++++++++------ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/desktop/.config/waybar/config b/desktop/.config/waybar/config index e0d7ca1..6946a07 100644 --- a/desktop/.config/waybar/config +++ b/desktop/.config/waybar/config @@ -145,7 +145,7 @@ "format": "{} ", }, "river/window": { - "format": " {}", + "format": " {}", "max-length": 70 }, "temperature": { @@ -166,8 +166,11 @@ "interval": 3600 }, "custom/wireguard": { + "format": "{icon}", "format-icons": { - "default": "󰖂" + "default": "", + "pia": "󰖂", + "netbird": "󱗆" }, "exec": "~/.config/waybar/modules/wireguard json", "exec-if": "command -v nmcli >/dev/null 2>&1", diff --git a/desktop/.config/waybar/modules/wireguard b/desktop/.config/waybar/modules/wireguard index fcdca46..db27acf 100755 --- a/desktop/.config/waybar/modules/wireguard +++ b/desktop/.config/waybar/modules/wireguard @@ -41,9 +41,8 @@ connected=() available=() function print_as_json() { - text="󰖂" # only prints a single icon when connected - # text="${1}" # use this line to show all output in text - alt="${1}" + text="${1%% |*}" # prints out name of first vpn it finds + alt="${1%%:*}" tooltip="${1}" [ -n "$1" ] && class="connected" || class="disconnected" printf "{\"text\": \"%s\", \"alt\": \"%s\", \"tooltip\": \"%s\", \"class\": \"%s\"}" \ @@ -79,7 +78,19 @@ function get_pia { status=$(piactl get connectionstate) if [[ $status = "Connected" ]]; then - connected+=("$(piactl get region): $(piactl get vpnip)") + connected+=("pia: $(piactl get vpnip)") + fi +} + +function get_netbird { + if ! command -v netbird >/dev/null 2>&1 || [ "$(systemctl is-active netbird)" == "inactive" ]; then + return 1 + fi + + status="$(netbird status)" + + if echo "$status" | grep -q "^Signal: Connected"; then + connected+=("netbird:$(echo "$status" | grep "^NetBird IP:" | cut -d':' -f2)") fi } @@ -122,19 +133,22 @@ function array_contains { } if [[ $nargs == 0 ]] || [[ $printjson = "yes" ]]; then - get_conns "$wgactive" get_pia + get_netbird + get_conns "$wgactive" print_conns connected elif [[ $showmenu == "yes" ]]; then - get_conns "$wgconns" get_pia + get_netbird + get_conns "$wgconns" print_conns connected "list" print_conns available "list" elif [[ $dotoggle == "yes" ]]; then - get_conns "$wgconns" get_pia + get_netbird + get_conns "$wgconns" if [[ "$(array_contains connected "$conn")" == "yes" ]]; then $nmclicmd down "$conn"