Skip to content

Instantly share code, notes, and snippets.

@rolurq
Created April 6, 2026 13:26
Show Gist options
  • Select an option

  • Save rolurq/3c8afdd441c793c26b6a8cf31dae556d to your computer and use it in GitHub Desktop.

Select an option

Save rolurq/3c8afdd441c793c26b6a8cf31dae556d to your computer and use it in GitHub Desktop.
Minimalistic zjstatus
#!/usr/bin/env sh
format="{t} {p} {i}"
print_help() {
echo "usage: battstat [options] format"
echo ""
echo "options:"
echo " -h, --help display help information"
echo " -f, --format formatted output of battstat"
echo " --percent-when-charged only display percent when charged"
echo ""
echo "format replacement tokens:"
echo " {i} display icon"
echo " {t} display time remaining"
echo " {p} display percent"
echo ""
echo "example:"
echo " battstat -f \"Cool Battery: {t} ({p})\""
}
exit_no_battery() {
echo "󰐥"
exit 1
}
get_darwin_details() {
battery_details=$(pmset -g batt)
# Exit if no battery exists.
if ! echo "$battery_details" | grep -q InternalBattery; then
exit_no_battery
fi
charged=$(echo "$battery_details" | grep -w 'charged')
charging=$(echo "$battery_details" | grep -w 'AC Power')
discharging=$(echo "$battery_details" | grep -w 'Battery Power')
time=$(echo "$battery_details" | grep -Eo '([0-9][0-9]|[0-9]):[0-5][0-9]')
percent=$(echo "$battery_details" | grep -o "[0-9]*"%)
}
get_linux_details() {
battery_details=$(LC_ALL=en_US.UTF-8 upower -i $(upower -e | grep 'BAT'))
# Exit if no batery exists.
if [ -z "$battery_details" ]; then
exit_no_battery
fi
charged=$(echo "$battery_details" | grep 'state' | grep -w 'fully-charged')
charging=$(echo "$battery_details" | grep 'state' | grep -w 'charging')
discharging=$(echo "$battery_details" | grep 'state' | grep -w 'discharging')
percent=$(echo "$battery_details" | grep 'percentage' | awk '{print $2}')
case $(echo "$battery_details" | grep 'time' | awk '{print $5}') in
"hours")
hours=$(echo "$battery_details" | grep 'time' | awk '{print $4}' | cut -d . -f1)
minutes=$(echo "$battery_details" | grep 'time' | awk '{print $4}' | cut -d . -f2)
minutes=$(echo .$minutes \* 60 | bc -l | cut -d. -f1)
;;
"minutes")
minutes=$(echo "$battery_details" | grep 'time' | awk '{print $4}' | cut -d . -f1)
;;
esac
# Diplay 0 in the hours spot when only minutes remain.
if [ -z "$hours" ]; then
hours="0"
fi
# Prefix 0 when minutes drop below 10.
if [ ${#minutes} -eq '1' ]; then
minutes="0$minutes"
fi
time=$hours:$minutes
}
get_openbsd_details() {
battery_details=$(apm)
# Exit if no battery exists.
if [ -z "$battery_details" ]; then
exit_no_battery
fi
charging=$(echo $battery_details | grep -w 'state: connected')
discharging=$(echo $battery_details | grep -w 'state: not connected')
percent=$(echo $battery_details | grep -o '[0-9]*%')
full_minutes=$(echo $battery_details | grep -o ' [0-9]* ')
# Battery is considered charged when AC is connected and 100%
if [ ! -z "$charging" ] && [ $percent = "100%" ]; then
charged="charged"
fi
# Only compute time when available
if [ ! -z "$full_minutes" ]; then
hours=$(($full_minutes / 60))
minutes=$(($full_minutes % 60))
# Prefix 0 when minutes drop below 10.
if [ ${#minutes} -eq '1' ]; then
minutes="0$minutes"
fi
time=$hours:$minutes
fi
}
hide_percent_until_charged() {
if [ -z "$charged" ]; then
percent=""
fi
}
set_icon() {
if [ ! -z "$charging" ] || [ ! -z "$charged" ]; then
icon=󰂄
elif [ ! -z "$discharging" ]; then
if [ $percent_num -gt 90 ]; then
icon=󰁹
elif [ $percent_num -gt 80 ]; then
icon=󰂂
elif [ $percent_num -gt 70 ]; then
icon=󰂁
elif [ $percent_num -gt 60 ]; then
icon=󰂀
elif [ $percent_num -gt 50 ]; then
icon=󰁿
elif [ $percent_num -gt 40 ]; then
icon=󰁾
elif [ $percent_num -gt 30 ]; then
icon=󰁽
elif [ $percent_num -gt 20 ]; then
icon=󰁼
else
icon=󰁻
fi
fi
}
set_time() {
# Display "calc..." when calculating time remaining.
if [ -z "$time" ] || [ $time = "0:00" ]; then
time="calc..."
fi
# Hide time when fully charged.
if [ ! -z "$charged" ]; then
time=""
fi
}
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
print_help
exit 0
fi
case $(uname) in
"Darwin")
get_darwin_details
;;
"Linux")
get_linux_details
;;
"OpenBSD")
get_openbsd_details
;;
*)
echo "battstat: operating system not supported"
exit 1
;;
esac
percent_num=${percent%\%}
while test $# -gt 0; do
case "$1" in
--percent-when-charged)
hide_percent_until_charged
shift
;;
-f | --format)
format="$2"
shift
shift
;;
*)
print_help
format=""
break
;;
esac
done
set_icon
set_time
if [ ! -z "$format" ]; then
echo $format | sed "s/{t}/$time/g;s/{p}/$percent/g;s/{i}/$icon/g"
fi
layout {
default_tab_template {
pane size=2 borderless=true {
plugin location="https://github.com/dj95/zjstatus/releases/latest/download/zjstatus.wasm" {
hide_frame_except_for_search "true"
border_enabled "true"
border_char "─"
border_format "#[fg=#6C7086]{char}"
border_position "bottom"
format_left " {mode} #[fg=#6c7086]│ #[fg=#eba0ac] {session}"
format_center "{tabs}"
format_right "{command_battery} #[fg=#6c7086]│ {datetime}"
format_space ""
format_hide_on_overlength "true"
format_precedence "crl"
tab_normal "#[fg=#f5e0dc] {index}: {name}{fullscreen_indicator}{sync_indicator}{floating_indicator} "
tab_active "#[fg=#1e1e2e,bg=#fab387] {index}: {name}{fullscreen_indicator}{sync_indicator}{floating_indicator} "
tab_fullscreen_indicator " □"
tab_sync_indicator " "
tab_floating_indicator " 󰉈"
tab_separator "#[fg=#6C7086] │ "
command_battery_command "sh -c \"~/.config/zellij/scripts/battstat -f '{i} {p}'\""
command_battery_format "#[fg=#f5c2e7]{stdout}"
command_battery_interval "60"
command_battery_rendermode "static"
// Date/time
datetime "#[fg=#89b4fa]󰭦 {format} "
datetime_format "%d.%m 󰅐 %H:%M"
datetime_timezone "Europe/Sofia"
mode_normal "#[fg=#a6da95]NORMAL"
mode_locked "#[fg=#6e738d]LOCKED "
mode_resize "#[fg=#f38ba8]RESIZE"
mode_pane "#[fg=#89b4fa]PANE"
mode_tab "#[fg=#b4befe]TAB"
mode_scroll "#[fg=#f9e2af]SCROLL"
mode_enter_search "#[fg=#8aadf4]ENT-SEARCH"
mode_search "#[fg=#8aadf4]SEARCHARCH"
mode_rename_tab "#[fg=#b4befe]RENAME-TAB"
mode_rename_pane "#[fg=#89b4fa]RENAME-PANE"
mode_session "#[fg=#74c7ec]SESSION"
mode_move "#[fg=#f5c2e7]MOVE"
mode_prompt "#[fg=#8aadf4]PROMPT"
mode_tmux "#[fg=#f5a97f]TMUX"
mode_default_to_mode "tmux"
}
}
children
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment