From 467b967cedaaebc3eb02c52c64bce160887115de Mon Sep 17 00:00:00 2001 From: dissimulo Date: Sun, 17 May 2026 03:12:11 -0700 Subject: [PATCH] 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. --- .local/bin/tmux-ip | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/.local/bin/tmux-ip b/.local/bin/tmux-ip index 03381491..4af64dd0 100755 --- a/.local/bin/tmux-ip +++ b/.local/bin/tmux-ip @@ -29,5 +29,21 @@ if is_digitalocean && do_anchor_ip; then exit 0 fi -curl -s --connect-timeout 3 https://api.ipify.org 2>/dev/null \ - || hostname -I | awk '{print $1}' +print_local_ip() { + 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:-}"