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

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 Have Perl; Will Travel. Freelance Perl/Testing/Agile consultant. Photo by http://www.circle23.com/. Warning: that site is not safe for work. The photographer is a good friend of mine, though, and it's appropriate to credit his work.