MacBook battery status and screen

While I'm writing my beginning Perl book, I've developed several tools to make my life easier. However, I've encountered a problem. I've often found that I am using iTerm2 in full-screen and this obscures my battery indicator. If I'm not plugged in, my battery can get dangerously low. I've fixed that.

I have the following little shell script which I've saved as "battery".

#!/bin/sh                                                                                                                                                 
ioreg -l |                            \
    grep -E '(Max|Current)Capacity' | \
    cut -d= -f2 |                     \
    perl -e '@b=<STDIN>;printf"%.1f%%\n",100*$b[1]/$b[0]'

In my .screenrd.d/book file (detailed in the "tools" post), I've modified my caption using a backtick (I've cut out some stuff here to make it easier to see):

backtick 1 1 1 battery                                                                                                                                        
caption always "%H%{-} [Battery %1`]"

That [Battery %1`] line now shows my battery life percentage in my screen caption.

Enjoy!

3 Comments

thanks!

Been meaning to add a battery indicator to my shell prompt for a long time now. Thanks for solving the hard bit for me :) The other bit was caching since it's a tiny bit sluggish: https://github.com/sartak/shell-prompt/commit/5809d48b2

I am usually alerted by the pop up box (on full screen also with iTerm2) which says 'your battery is very low'.

Leave a comment

About Ovid

user-pic Freelance Perl/Testing/Agile consultant and trainer. See http://www.allaroundtheworld.fr/ for our services. If you have a problem with Perl, we will solve it for you. And don't forget to buy my book! http://www.amazon.com/Beginning-Perl-Curtis-Poe/dp/1118013840/