HOWTO: add battery status to bash prompt

Forum rules
Share your brain ;)
User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

HOWTO: add battery status to bash prompt

Unread post by dkeg » Thu Oct 24, 2013 4:29 am

If you use a laptop with a battery and you tend to forget to plug in, this may be for you.
Add battery status to your prompt. there are more than one way of course, but this is how I'm doing it for now.

create script. I have a directory called scripts, but could also place in ~/bin

Code: Select all

#! /bin/bash
cat /sys/class/power_supply/BAT0/capacity
of course make chmod +x it. and in .bashrc w/in prompt code

Code: Select all

\$(/path/to/script.sh)
oblig scrot
Attachments
scrot.png
scrot.png (702 Bytes) Viewed 5421 times

Work hard; Complain less

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: bash prompts

Unread post by dkeg » Fri Oct 25, 2013 2:15 am

update to the battery meter script to add to your prompt. Took something I found online and did a re-write. Name it and call it from your .bashrc as described above, or write as a function directly in your .bashrc.

Code: Select all

#! /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"
   ;;
   '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

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

# end
EDIT - fixed a couple of errors. Fixed the CHARGED variable and 'Charged' to 'Full' in the case statement.
EDIT 2 - more clean up, less bloat :)

place in .bashrc prompt code

Code: Select all

PS1="\[\033[0;37m\]\][\$(/path/to/script/battstat2.sh)\033[0;37m\]]-[\[\033[0;31m\]\[\A\[\033[0;37m\]]-[\[\033[0;32m\]\w\[\033[0;37m\]]\n\[\033[0;37m\]\342\224\224\342\224\200\342\224\200\342\224\200\342\224\200\342\225\274 \[\033[0m\]"
charging

discharging
Attachments
scrot3.png
scrot2.png

Work hard; Complain less

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: bash prompts

Unread post by machinebacon » Fri Oct 25, 2013 5:14 am

We could add [used RAM] and [CPU] to it and make conky turn yellow :)
..gnutella..

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: bash prompts

Unread post by dkeg » Fri Oct 25, 2013 1:10 pm

* fixed a couple mistakes up there ^*

Come on guys, now you're just getting ridiculous :D

But I always space out and forget how low my battery is, that is why I did this little project.

Work hard; Complain less

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: HOWTO: add battery status to bash prompt

Unread post by dkeg » Sat Oct 26, 2013 1:54 pm

Taken from the bash prompt thread

http://www.linuxbbq.org/bbs/viewtopic.php?f=4&t=648

Work hard; Complain less

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: HOWTO: add battery status to bash prompt

Unread post by machinebacon » Sat Oct 26, 2013 2:02 pm

bonus: memstat.sh - updates for every newly opened term, else can be piped to dzen2 or whatever where.

Code: Select all

#!/bin/bash
hw_mem=0
free_mem=0
human=1024

mem_info=$(</proc/meminfo)
                mem_info=$(echo $(echo $(mem_info=${mem_info// /}; echo ${mem_info//kB/})))
                for m in $mem_info; do
                        if [[ ${m//:*} = MemTotal ]]; then
                                memtotal=${m//*:}
                        fi

                        if [[ ${m//:*} = MemFree ]]; then
                                memfree=${m//*:}
                        fi

                        if [[ ${m//:*} = Buffers ]]; then
                                membuffer=${m//*:}
                        fi

                        if [[ ${m//:*} = Cached ]]; then
                                memcached=${m//*:}
                        fi
                done

usedmem="$(((($memtotal - $memfree) - $membuffer - $memcached) / $human))"
totalmem="$(($memtotal / $human))"

echo "$usedmem"
..gnutella..

User avatar
xaos52
The Good Doctor
Posts: 190
Joined: Thu Aug 15, 2013 11:59 am
Location: Eernegem, Belgium

Re: HOWTO: add battery status to bash prompt

Unread post by xaos52 » Sat Oct 26, 2013 3:19 pm

My version of 'memstat':- using associative array - the script begs for an associative array

Code: Select all

#!/bin/bash
human=1024
declare -A meminfo

# split the lines from /proc/meminfo in 2 fields using the : as delimeter
meminfo_split () {
    cat /proc/meminfo | awk '
BEGIN { FS=":" }
{ label=$1; value=$2; print label,value }
' 
}

# read into an associative array after 'massaging' the values
while read -r _tag _value; do
    _value=${_value//kB/}
    _value=${_value// /}
    meminfo["$_tag"]="$_value"
done < <(meminfo_split)

# do your calculations
printf '%d Mb\n' $(( ( ${meminfo['MemTotal']} - ${meminfo['MemFree']} - ${meminfo['Buffers']} - ${meminfo['Cached']} ) / $human ))

exit 0

It is slightly faster than yours on my system, and you have all the /proc/meminfo variables available in the script to do all the calcultaions you want.
Better readable too, in my opinion.

Added to my bash_scripts repo on github
Last edited by xaos52 on Sat Oct 26, 2013 3:26 pm, edited 1 time in total.
Connected. Take this REPL, brother, and may it serve you well.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: HOWTO: add battery status to bash prompt

Unread post by machinebacon » Sat Oct 26, 2013 3:21 pm

^ Mine is not mine, it is stolen from screenfetch :)
..gnutella..

User avatar
xaos52
The Good Doctor
Posts: 190
Joined: Thu Aug 15, 2013 11:59 am
Location: Eernegem, Belgium

Re: HOWTO: add battery status to bash prompt

Unread post by xaos52 » Sat Oct 26, 2013 3:28 pm

Mine will make a better impression on bash script writers, I am sure :)
Connected. Take this REPL, brother, and may it serve you well.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: HOWTO: add battery status to bash prompt

Unread post by machinebacon » Sat Oct 26, 2013 3:35 pm

Code: Select all

└────╼ time memstat
565 Mb

real	0m0.031s
user	0m0.006s
sys	0m0.006s
[100]─[22:33]─[~]
└────╼ time memstat.sh
565

real	0m0.048s
user	0m0.010s
sys	0m0.005s
[100]─[23:40]─[~]
└────╼ time free -h | awk 'NR==3 { print $3 }'
602M

real	0m0.010s
user	0m0.002s
sys	0m0.001s
"slightly faster" - Doctor, you exaggerate:))

I'll replace it, thanks for the addition.
Edit: Pidsley wins the decruftification award.
..gnutella..

pidsley
Hermit
Posts: 2539
Joined: Wed Oct 17, 2012 12:31 pm

Re: HOWTO: add battery status to bash prompt

Unread post by pidsley » Sat Oct 26, 2013 4:08 pm

Code: Select all

free -h | awk 'NR==3 { print $3 }'

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: HOWTO: add battery status to bash prompt

Unread post by machinebacon » Sat Oct 26, 2013 4:40 pm

^ :tongue-in-cheek: but Bash-lovers won't find it fancy ;)

Of course that's the sexiest one.
..gnutella..

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: HOWTO: add battery status to bash prompt

Unread post by dkeg » Sun Oct 27, 2013 1:49 pm

^^Simplistically beautiful. And definitely bloat free.

Work hard; Complain less

User avatar
wuxmedia
Grasshopper
Posts: 6454
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: HOWTO: add battery status to bash prompt

Unread post by wuxmedia » Sun Oct 27, 2013 8:48 pm

I bloated the original script, as found on pony muncher;
If there's no battery present stops the spam.
no quite sure how it would fit into the updated version, over 10 lines and I get dizzy 8)

Code: Select all

#!/bin/bash
# test if /sys/class/power_supply/BAT0/capacity exists, avoiding console spam on desktops

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
haven't tested it on a lappy yet.
"Seek, and Ye shall find"
"Github | Chooons | Site"

pidsley
Hermit
Posts: 2539
Joined: Wed Oct 17, 2012 12:31 pm

Re: HOWTO: add battery status to bash prompt

Unread post by pidsley » Sun Oct 27, 2013 8:58 pm

wuxmedia wrote: If there's no battery present stops the spam.
Thank you from those of us with no batteries.

User avatar
wuxmedia
Grasshopper
Posts: 6454
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: HOWTO: add battery status to bash prompt

Unread post by wuxmedia » Sun Oct 27, 2013 10:19 pm

You're welcome.
AND I learnt a tiny bit of bash, baby steps wux... 8)
still not sure if it will work with a battery.
"Seek, and Ye shall find"
"Github | Chooons | Site"

Post Reply