soku 461177d71c Module Eth +Mask IP
Transforms the [Module/Eth] into a module which can be used to show and hide your local machine's IP (if you're connected by ethernet)
2025-04-30 10:41:39 +00:00

38 lines
1.2 KiB
Bash

#!/usr/bin/env bash
# Recommended to store this file in the $HOME/.config/polybar/polybar-scripts/ folder
# you can use... mkdir $HOME/.config/polybar && mkdir $HOME/.config/polybar/polybar-scripts/
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Temp File to save a state, Polybar makes a variable loop impossible
# unless we pull the value from outside of our script in another file.
F=/tmp/polybar_mask_ip_toggle
# Our print values!
IP="$(hostname -I | awk '{print $1}')"
MASK="████████████"
# detect real interface name
INTERFACENAME=$(ip route get 1.1.1.1 2>/dev/null \
| awk '/dev/ { for(i=1;i<NF;i++) if($i=="dev") print $(i+1)}' \
| head -n1)
[[ -z "$IF" ]] && IF="eth0"
# Initialize state if missing
if [[ ! -f $F ]]; then
echo 1 > "$F"
fi
# If called to toggle, flip 1↔ 2, write and exit
if [[ $1 == "toggle" ]]; then
state=$(<"$F")
# compute next: 1→ 2, 2→ 1
next=$(( state % 2 + 1 ))
echo $next > "$F"
exit 0
fi
# Otherwise, just read the state and print
state=$(<"$F")
if (( state == 1 )); then
echo "%{F#F0C674}$INTERFACENAME%{F-} $MASK"
else
echo "%{F#F0C674}$INTERFACENAME%{F-} $IP"
fi