Batt Stat in Bash Prompt

January 5, 2014

How many times has your battery died while you were right in the middle of something? No, No matter the warnings, I always am thinking, eh, I still got some time, let me just finish this last thing up. Then BAM. And it's funny. At first, you're ocnfused, wondering waht happened. And then you remember. When using conky, have it turn red, orange, whatever when below say 15%. Still doesn't work. Sure, I notice it, but again, I just have to finish that one item. So I thought, I'm always inside a terminal, can I add a battery status to my prompt. With that I put together a super, ridiculously simple script.

#! /bin/bash
cat /sys/class/power_supply_BAT0/capacity

Then in my conky I called upon that super fancy piece of bash. Befory you squak at my script, I'm on a laptop, I definitely have a battery, but my good friends at the grill quickly improved upon this, adding some fancy login

#! /bin/bash
if test -e /sys/class/power_supply/BAT0/capacity; then
  cat /sys/class/power_supply/BAT0/capacitycat /sys/class/power_supply/BAT0/capacity >&2
else
  exit 1
fi

Nice, huh? But, when you're hanging with the guys, drinking, chomping on some nice juicy steak, what do you do? You make it bigger, and better. No rest for the weary. So get crackin. Can we colorize it? I found some other similar stuff on the webs, and quickly hacked it up to work my way

#! /bin/bash

#battery status script

BATTERY=/sys/class/power_supply/BAT0

CAPACITY=`cat $BATTERY/capacity`
STATE=`cat $BATTERY/status`

NON='\033[00m'
BLD='\033[01m'
RED='\033[01;31m'
GRN='\033[01;32m'
YEL='\033[01;33m'

COLOR="$RED"

case "${STATE}" in
'Full')
STATE="$BLD*$NON"
#STATE="$BLD*$NON"
;;
'Charging')
STATE="$BLD+$NON"
;;
'Discharging')
STATE="$BLD-$NON"
;;
esac

# color code capacity
if [ "$CAPACITY" -gt "99" ] then
   CHARGE=100
fi
if [ "$CAPACITY" -gt "15" ] then
   COLOR="$YEL"
fi
if [ "$CAPACITY" -gt "30" ] then
   COLOR="$GRN"
fi

OUTPUT="${COLOR}${CAPACITY}${STATE}${NON}"
echo -e "$OUTPUT"

You can pretty much see the output in any scrot of mine you can find. I'm not going to show one here, you get the idea.

And in your .bashrc, of course after saved as 'whatever', and chmod +x, link to your executable. Or I guess, you could write a function within .bashrc, but I'm not doing that.

PS1="\$($HOME/scripts/battstat2.sh) ... rest of prompt ...."

Note, the '\' before '$' is required so your battery status updates automatically.

So there you have it. Battery metered in conky, osd_cat, bash prompt; wherever I can find a way to fit it in. Of oourse, believe it or not, I have still pushed it to the edge and let my battery die.


LINUX

LINKS