Fix tmux status bar for cross-platform (macOS + Linux)

This commit is contained in:
Viktor Barzin 2026-01-18 16:27:32 +00:00 committed by Viktor Barzin
parent b2800e3156
commit 73b2f6bf60

View file

@ -138,7 +138,7 @@ tmux_conf_theme_status_left=" ❐ #S | ↑#{?uptime_y, #{uptime_y}y,}#{?uptime_d
# Status right: network info + mem/cpu + battery + time + user@host # Status right: network info + mem/cpu + battery + time + user@host
# Battery vars use #{?...} conditionals so they're safe on non-laptop systems # Battery vars use #{?...} conditionals so they're safe on non-laptop systems
tmux_conf_theme_status_right=" #{local_ip}#{gateway} #{prefix}#{mouse}#{pairing}#{synchronized} #{mem_cpu}#{battery_fallback} , %R , %d %b | #{username}#{root} | #{hostname} " tmux_conf_theme_status_right=" #(awk '/^# EOF$/,0' ~/.tmux.conf.local | cut -c3- | sh -s _local_ip)#(awk '/^# EOF$/,0' ~/.tmux.conf.local | cut -c3- | sh -s _gateway) #{prefix}#{mouse}#{pairing}#{synchronized} #(awk '/^# EOF$/,0' ~/.tmux.conf.local | cut -c3- | sh -s _mem_cpu)#(awk '/^# EOF$/,0' ~/.tmux.conf.local | cut -c3- | sh -s _battery_fallback) , %R , %d %b | #{username}#{root} | #{hostname} "
# Status left colors: yellow, pink, green # Status left colors: yellow, pink, green
tmux_conf_theme_status_left_fg="$tmux_conf_theme_colour_6,$tmux_conf_theme_colour_7,$tmux_conf_theme_colour_8" tmux_conf_theme_status_left_fg="$tmux_conf_theme_colour_6,$tmux_conf_theme_colour_7,$tmux_conf_theme_colour_8"
@ -300,106 +300,86 @@ set -g @plugin 'thewtex/tmux-mem-cpu-load'
# /!\ do not remove the following line # /!\ do not remove the following line
# EOF # EOF
#
# /!\ do not "uncomment" the functions: the leading "# " characters are needed # # usage: #{local_ip}
# # Cross-platform: Linux (ip/hostname) and macOS (ipconfig/route)
# usage: #{local_ip} # _local_ip() {
# Cross-platform: Linux (ip/hostname) and macOS (ipconfig/route) # if command -v ip >/dev/null 2>&1; then
local_ip() { # result=$(ip route get 8.8.8.8 2>/dev/null | grep -oP 'src \K[0-9.]+' | head -1)
# Try Linux first (ip command) # [ -n "$result" ] && printf 'IP:%s' "$result" && return
if command -v ip >/dev/null 2>&1; then # fi
result=$(ip route get 8.8.8.8 2>/dev/null | grep -oP 'src \K[0-9.]+' | head -1) # if [ "$(uname)" = "Darwin" ]; then
[ -n "$result" ] && printf 'IP:%s' "$result" && return # iface=$(route get 8.8.8.8 2>/dev/null | awk '/interface:/ {print $2}')
fi # if [ -n "$iface" ]; then
# macOS fallback (route + ifconfig) # result=$(ipconfig getifaddr "$iface" 2>/dev/null)
if [ "$(uname)" = "Darwin" ]; then # [ -n "$result" ] && printf 'IP:%s' "$result" && return
iface=$(route get 8.8.8.8 2>/dev/null | awk '/interface:/ {print $2}') # fi
if [ -n "$iface" ]; then # fi
result=$(ipconfig getifaddr "$iface" 2>/dev/null) # result=$(hostname -I 2>/dev/null | awk '{print $1}')
[ -n "$result" ] && printf 'IP:%s' "$result" && return # [ -n "$result" ] && printf 'IP:%s' "$result" && return
fi # printf 'IP:offline'
fi # }
# Final fallback: hostname -I (Linux) #
result=$(hostname -I 2>/dev/null | awk '{print $1}') # # usage: #{gateway}
[ -n "$result" ] && printf 'IP:%s' "$result" && return # # Cross-platform: Linux and macOS
# No IP found # _gateway() {
printf 'IP:offline' # if command -v ip >/dev/null 2>&1; then
} # gw=$(ip route 2>/dev/null | awk '/default/ {print $3; exit}')
# [ -n "$gw" ] && printf ' GW:%s' "$gw" && return
# usage: #{gateway} # fi
# Cross-platform: Linux and macOS # if [ "$(uname)" = "Darwin" ]; then
gateway() { # gw=$(route -n get default 2>/dev/null | awk '/gateway:/ {print $2}')
# Try Linux first (ip command) # [ -n "$gw" ] && printf ' GW:%s' "$gw" && return
if command -v ip >/dev/null 2>&1; then # fi
gw=$(ip route 2>/dev/null | awk '/default/ {print $3; exit}') # printf ''
[ -n "$gw" ] && printf ' GW:%s' "$gw" && return # }
fi #
# macOS fallback # # usage: #{mem_cpu}
if [ "$(uname)" = "Darwin" ]; then # # Shows memory/CPU if tmux-mem-cpu-load plugin is installed
gw=$(route -n get default 2>/dev/null | awk '/gateway:/ {print $2}') # _mem_cpu() {
[ -n "$gw" ] && printf ' GW:%s' "$gw" && return # for path in "$HOME/.tmux/plugins/tmux-mem-cpu-load/tmux-mem-cpu-load" "/usr/local/bin/tmux-mem-cpu-load" "/usr/bin/tmux-mem-cpu-load"; do
fi # if [ -x "$path" ]; then
# No gateway - common on VPN or special network configs, don't show error # "$path" --colors --powerline-right --interval 2 2>/dev/null
printf '' # return
} # fi
# done
# usage: #{mem_cpu} # if [ -f /proc/loadavg ]; then
# Shows memory/CPU if tmux-mem-cpu-load plugin is installed # load=$(cut -d' ' -f1-3 /proc/loadavg 2>/dev/null)
mem_cpu() { # [ -n "$load" ] && printf 'Load:%s' "$load" && return
# Check common installation paths # fi
for path in \ # if [ "$(uname)" = "Darwin" ]; then
"${TMUX_PLUGIN_MANAGER_PATH:-$HOME/.tmux/plugins}/tmux-mem-cpu-load/tmux-mem-cpu-load" \ # load=$(sysctl -n vm.loadavg 2>/dev/null | awk '{print $2, $3, $4}')
"$HOME/.tmux/plugins/tmux-mem-cpu-load/tmux-mem-cpu-load" \ # [ -n "$load" ] && printf 'Load:%s' "$load" && return
"/usr/local/bin/tmux-mem-cpu-load" \ # fi
"/usr/bin/tmux-mem-cpu-load" # printf ''
do # }
if [ -x "$path" ]; then #
"$path" --colors --powerline-right --interval 2 2>/dev/null # # usage: #{battery_fallback}
return # # Shows battery status or nothing on desktops/servers
fi # _battery_fallback() {
done # if [ -d /sys/class/power_supply/BAT0 ]; then
# Plugin not installed - show basic load average as fallback # cap=$(cat /sys/class/power_supply/BAT0/capacity 2>/dev/null)
if [ -f /proc/loadavg ]; then # status=$(cat /sys/class/power_supply/BAT0/status 2>/dev/null)
load=$(cut -d' ' -f1-3 /proc/loadavg 2>/dev/null) # if [ -n "$cap" ]; then
[ -n "$load" ] && printf 'Load:%s' "$load" && return # icon="🔋"
fi # [ "$status" = "Charging" ] && icon="🔌"
# macOS fallback # printf '%s%s%%' "$icon" "$cap"
if [ "$(uname)" = "Darwin" ]; then # return
load=$(sysctl -n vm.loadavg 2>/dev/null | awk '{print $2, $3, $4}') # fi
[ -n "$load" ] && printf 'Load:%s' "$load" && return # fi
fi # if [ "$(uname)" = "Darwin" ]; then
printf '' # battery=$(pmset -g batt 2>/dev/null | grep -o '[0-9]*%' | head -1)
} # if [ -n "$battery" ]; then
# charging=$(pmset -g batt 2>/dev/null | grep -q 'AC Power' && echo 1)
# usage: #{battery_fallback} # icon="🔋"
# Shows battery status or nothing on desktops/servers # [ -n "$charging" ] && icon="🔌"
battery_fallback() { # printf '%s%s' "$icon" "$battery"
# Linux: check /sys/class/power_supply # return
if [ -d /sys/class/power_supply/BAT0 ]; then # fi
cap=$(cat /sys/class/power_supply/BAT0/capacity 2>/dev/null) # fi
status=$(cat /sys/class/power_supply/BAT0/status 2>/dev/null) # printf ''
if [ -n "$cap" ]; then # }
icon="🔋" #
[ "$status" = "Charging" ] && icon="🔌" # "$@"
printf '%s%s%%' "$icon" "$cap" # # /!\ do not remove the previous line
return # # do not write below this line
fi
fi
# macOS: use pmset
if [ "$(uname)" = "Darwin" ]; then
battery=$(pmset -g batt 2>/dev/null | grep -o '[0-9]*%' | head -1)
if [ -n "$battery" ]; then
charging=$(pmset -g batt 2>/dev/null | grep -q 'AC Power' && echo 1)
icon="🔋"
[ -n "$charging" ] && icon="🔌"
printf '%s%s' "$icon" "$battery"
return
fi
fi
# No battery (desktop/server) - show nothing
printf ''
}
"$@"
# /!\ do not remove the previous line
# do not write below this line