tmux-ip: portable local-IP fallback for macOS

`hostname -I` is GNU-only and errors on BSD/macOS, so when the public-IP
curl couldn't reach api.ipify.org the script printed nothing and the
tmux status bar showed an empty IP. Try `ipconfig getifaddr` for common
interfaces first, fall back to `hostname -I` on Linux, then `ifconfig`
as a last resort. Also guard against empty-but-zero-exit curl output.
This commit is contained in:
2026-05-17 03:12:11 -07:00
parent 474fdd028c
commit 467b967ced

View File

@@ -29,5 +29,21 @@ if is_digitalocean && do_anchor_ip; then
exit 0 exit 0
fi fi
curl -s --connect-timeout 3 https://api.ipify.org 2>/dev/null \ print_local_ip() {
|| hostname -I | awk '{print $1}' local iface ip
if command -v ipconfig >/dev/null 2>&1; then
for iface in en0 en1 en2; do
ip=$(ipconfig getifaddr "$iface" 2>/dev/null) || true
[ -n "${ip:-}" ] && { printf '%s\n' "$ip"; return 0; }
done
fi
if hostname -I >/dev/null 2>&1; then
hostname -I | awk '{print $1}'
return 0
fi
ifconfig 2>/dev/null | awk '/inet /{ if ($2 != "127.0.0.1") { print $2; exit } }'
}
ip=$(curl -s --connect-timeout 3 https://api.ipify.org 2>/dev/null || true)
[ -z "${ip:-}" ] && ip=$(print_local_ip)
printf '%s\n' "${ip:-}"