Nowheretobefound

  • Increase font size
  • Default font size
  • Decrease font size

Battery status

E-mail Print

Introduction

Since I'm running Debian Linux on my laptop as well I was looking for a way to have the status of it's battery always visible from the console. I saw some solutions that use tput to print a banner at the top of the screen. But I found that to be a bit nervous because the banner would flicker with every command I entered.

Therefore I decided to go with changing the prompt. It had to change while working on the console but using PROMPT_COMMAND to retrieve the information with every command entered made the console rather slow... So here's how I got it to work the way I want. You may need to adjust things here and there.

 

What needs to be done

Collecting information

First we need to collect the battery information from ACPI.

mcedit /home/batt.sh

The script looks like this.

#!/bin/bash
 
# List of colors
NOCOLOR='\[\033[0m\]'
BLACK='\[\033[0;30m\]'
GRAY='\[\033[1;30m\]'
RED='\[\033[0;31m\]'
PINK='\[\033[1;31m\]'
GREEN='\[\033[0;32m\]'
LIGHT_GREEN='\[\033[1;32m\]'
BROWN='\[\033[0;33m\]'
YELLOW='\[\033[1;33m\]'
BLUE='\[\033[0;34m\]'
LIGHT_BLUE='\[\033[1;34m\]'
PURPLE='\[\033[0;35m\]'
LIGHT_PURPLE='\[\033[1;35m\]'
CYAN='\[\033[0;36m\]'
TEAL='\[\033[1;36m\]'
LIGHT_GRAY='\[\033[0;37m\]'
WHITE='\[\033[1;37m\]'
 
# Get battery info
BATTERY=/proc/acpi/battery/BAT0
REM_CAP=`grep "^remaining capacity" $BATTERY/state | awk '{ print $3 }'`
BATSTATE=`grep "^charging state" $BATTERY/state | awk '{ print $3 }'`
FULL_CAP=`grep "design capacity:" $BATTERY/info | tr -d A-z | tr -d " " | tr -d :`
CHARGE=$(((100*$REM_CAP)/$FULL_CAP))
 
# State
case "${BATSTATE}" in
   'charged')
      BSTATE="="
   ;;
   'charging')
      BSTATE="+"
   ;;
   'discharging')
      BSTATE="-"
   ;;
esac
 
# Prompt
if [ $CHARGE -le "10" ]
then
    echo $RED$BSTATE$CHARGE%$NOCOLOR >/home/batt.status
elif [ $CHARGE -le "20" ]
then
    echo $PINK$BSTATE$CHARGE%$NOCOLOR >/home/batt.status
elif [ $CHARGE -le "40" ]
then
   echo $LIGHT_PURPLE$BSTATE$CHARGE%$NOCOLOR >/home/batt.status
elif [ $CHARGE -le "60" ]
then
   echo $YELLOW$BSTATE$CHARGE%$NOCOLOR >/home/batt.status
elif [ $CHARGE -le "80" ]
then
   echo $CYAN$BSTATE$CHARGE%$NOCOLOR >/home/batt.status
else
   echo $GREEN$BSTATE$CHARGE%$NOCOLOR >/home/batt.status
fi

That will write the battery status to a file in the home directory. Make sure it can be executed:

chmod u+x /home/batt.sh

I used Webmin (System -> Scheduled Cron Jobs) to scheduled it to run every minute.

 

Changing the prompt

Now we need to change the prompt itself.

mcedit ~/.bashrc

I added these lines:

function prompt_command {
   export PS1='\h'`cat /home/batt.status`':\w\$ '
}
PROMPT_COMMAND=prompt_command
export PS1='\h'`cat /home/batt.status`':\w\$ '

Make sure that the default PS1 line is either removed or commented out with a # sign.
You will need to log off and on again to see the change.

 

Finally

There might be better ways to make this happen. But this works just fine and does exactly what I want without slowing down the console or being annoying.