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).
This commit is contained in:
Marty Oehme 2024-07-26 10:56:36 +02:00
parent 12d186d871
commit 017668792c
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A
2 changed files with 26 additions and 9 deletions

View file

@ -145,7 +145,7 @@
"format": "{} ", "format": "{} ",
}, },
"river/window": { "river/window": {
"format": " {}", "format": " {}",
"max-length": 70 "max-length": 70
}, },
"temperature": { "temperature": {
@ -166,8 +166,11 @@
"interval": 3600 "interval": 3600
}, },
"custom/wireguard": { "custom/wireguard": {
"format": "{icon}",
"format-icons": { "format-icons": {
"default": "󰖂" "default": "",
"pia": "󰖂",
"netbird": "󱗆"
}, },
"exec": "~/.config/waybar/modules/wireguard json", "exec": "~/.config/waybar/modules/wireguard json",
"exec-if": "command -v nmcli >/dev/null 2>&1", "exec-if": "command -v nmcli >/dev/null 2>&1",

View file

@ -41,9 +41,8 @@ connected=()
available=() available=()
function print_as_json() { function print_as_json() {
text="󰖂" # only prints a single icon when connected text="${1%% |*}" # prints out name of first vpn it finds
# text="${1}" # use this line to show all output in text alt="${1%%:*}"
alt="${1}"
tooltip="${1}" tooltip="${1}"
[ -n "$1" ] && class="connected" || class="disconnected" [ -n "$1" ] && class="connected" || class="disconnected"
printf "{\"text\": \"%s\", \"alt\": \"%s\", \"tooltip\": \"%s\", \"class\": \"%s\"}" \ printf "{\"text\": \"%s\", \"alt\": \"%s\", \"tooltip\": \"%s\", \"class\": \"%s\"}" \
@ -79,7 +78,19 @@ function get_pia {
status=$(piactl get connectionstate) status=$(piactl get connectionstate)
if [[ $status = "Connected" ]]; then 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 fi
} }
@ -122,19 +133,22 @@ function array_contains {
} }
if [[ $nargs == 0 ]] || [[ $printjson = "yes" ]]; then if [[ $nargs == 0 ]] || [[ $printjson = "yes" ]]; then
get_conns "$wgactive"
get_pia get_pia
get_netbird
get_conns "$wgactive"
print_conns connected print_conns connected
elif [[ $showmenu == "yes" ]]; then elif [[ $showmenu == "yes" ]]; then
get_conns "$wgconns"
get_pia get_pia
get_netbird
get_conns "$wgconns"
print_conns connected "list" print_conns connected "list"
print_conns available "list" print_conns available "list"
elif [[ $dotoggle == "yes" ]]; then elif [[ $dotoggle == "yes" ]]; then
get_conns "$wgconns"
get_pia get_pia
get_netbird
get_conns "$wgconns"
if [[ "$(array_contains connected "$conn")" == "yes" ]]; then if [[ "$(array_contains connected "$conn")" == "yes" ]]; then
$nmclicmd down "$conn" $nmclicmd down "$conn"