summary refs log tree commit diff
path: root/.config/waybar/module-tailscale.sh
diff options
context:
space:
mode:
Diffstat (limited to '.config/waybar/module-tailscale.sh')
-rwxr-xr-x.config/waybar/module-tailscale.sh33
1 files changed, 33 insertions, 0 deletions
diff --git a/.config/waybar/module-tailscale.sh b/.config/waybar/module-tailscale.sh
new file mode 100755
index 0000000..53b0409
--- /dev/null
+++ b/.config/waybar/module-tailscale.sh
@@ -0,0 +1,33 @@
+#!/bin/bash
+set -eu
+[ -n "${DEBUG:-}" ] && set -x
+
+update() {
+	local ts_acct ts_ip
+	local class text alt
+
+	if tailscale status | grep -q stopped; then
+		return 1
+	fi
+
+	ts_ip=$(tailscale ip -1 || return 1)
+	ts_acct=$(tailscale switch --list 2>/dev/null | awk '/\*$/{ print gensub(/^.* ([^ ]+)\*$/, "\\1", "1") }')
+
+	alt="$ts_acct"
+	class="$ts_acct"
+	# only display account name when not "main"
+	[ "$ts_acct" = "main" ] || text=$ts_acct
+
+	if tailscale exit-node list | grep -q selected; then
+		alt="exit-node"
+		class="exit-node"
+	fi
+
+	jq -nrc '{text:$text,tooltip:$tip,class:$class,alt:$alt}' \
+		--arg tip "${ts_ip:?}" \
+		--arg class "${class:?}" \
+		--arg alt "${alt:?}" \
+		--arg text "${text:-}"
+}
+
+update || echo '{}'