Battery Status within Command Line

Not sure if this has been mentioned before, but I was referencing the Mobian WIKI and came across some code that allowed current battery status being showed within the command line. Thought to see if it worked within Manjaro Phosh and to my surprise it does. Looks like it colorized the bash session as well.

Not sure if I can post the Mobian WIKI on here but here are the step to recreate.

image

You will want to edit the following file

sudo nano ~/.bashrc
– Add the following to the end of this page –

# -= start battery prompt =-
battery_status(){
BATTERY=/sys/class/power_supply/axp20x-battery #this is for pinephone and pinetab
BATSTATE=$(cat ${BATTERY}/status)
CHARGE=$(cat ${BATTERY}/capacity)
NON='\001\e[0m\002'
BLD='\001\e[1m\002'
RED='\001\e[1;31m\002'
GRN='\001\e[1;32m\002'
YEL='\001\e[1;33m\002'
COLOUR="$RED"
case "${BATSTATE}" in
  'Charged')
    BATSTT="="
  ;;
  'Charging')
    BATSTT="+"
  ;;
  'Not charging'|'Discharging')
    BATSTT="-"
  ;;
  *)
    BATSTT="*"
  ;;
esac
# prevent a charge of more than 100% displaying
if [ "$CHARGE" -gt "99" ]
then
  CHARGE=100
fi
if [ "$CHARGE" -gt "24" ]
then
  COLOUR="$YEL"
fi
if [ "$CHARGE" -gt "34" ]
then
  COLOUR="$GRN"
fi
printf "[${BATSTT}${COLOUR}${CHARGE}%%${NON}]"
} #end of battery_status()
if [ $(id -u) -eq 0 ];
then # you are root, set red colour prompt
  PS1='$(battery_status)\[\033[1;31m\]\u\[\033[0;32m\]@\h\[\033[1;34m\] \w\[\033[0m\]\$ '
else # normal user
  PS1='$(battery_status)\[\033[1;36m\]\u\[\033[0;32m\]@\h\[\033[1;34m\] \w\[\033[0m\]\$ '
fi
# -=end battery prompt=-```
1 Like

Please, when you share code or commands, use the proper preformatted text
Here is a hint

1 Like