Skip to content

Instantly share code, notes, and snippets.

@artman41
Created July 17, 2025 23:08
Show Gist options
  • Save artman41/93ed59d317672b4db1332d7dad574dd6 to your computer and use it in GitHub Desktop.
Save artman41/93ed59d317672b4db1332d7dad574dd6 to your computer and use it in GitHub Desktop.
show the battery pct & whether charging in ps1
function get_bat_msg() {
BAT_PCT=$(cat /sys/class/power_supply/BAT1/capacity);
if [ $BAT_PCT -le 40 ]; then
BAT_PCT_COLOUR=31
elif [ $BAT_PCT -le 80 ]; then
BAT_PCT_COLOUR=93
else
BAT_PCT_COLOUR=32;
fi;
if [[ "$(cat /sys/class/power_supply/BAT1/status)" == "Discharging" ]]; then
BAT_STATE="Battery"
BAT_STATE_COLOUR=37
else
BAT_STATE="Powered"
BAT_STATE_COLOUR=35
fi;
BAT_PCT_MSG="\e[38;5;${BAT_PCT_COLOUR}m${BAT_PCT}\e[0m%"
BAT_STATE_MSG="\e[38;5;${BAT_STATE_COLOUR}m$BAT_STATE\e[0m"
echo -e "($BAT_PCT_MSG [$BAT_STATE_MSG])"
}
PS1="\$(get_bat_msg) $PS1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment