Yet Another Battery Widget (Awesome 3.5.1)

Yet another battery widget for Awesome. This one actually works (shock! horror!) on Awesome 3.5.1 on my Thinkpad x230. Your mileage may vary. Colours used are from the excellent Solarized colour scheme. Behold the mighty widget, in all its unobtrusive glory!

battery

The implementation is in two parts: a simple shell script to output the battery status, and a bit of rc.lua tweaks to display the widget. This is mostly the result of a bit of copy/pasting from different sources I forgot to bookmark. Oh well.

~/bin/battery.sh:

#!/bin/bash
 
healthy='#859900'
low='#b58900'
discharge='#dc322f'
 
capacity=`cat /sys/class/power_supply/BAT0/capacity`
if (($capacity <= 25));
then
        capacityColour=$low
else
        capacityColour=$healthy
fi
 
status=`cat /sys/class/power_supply/BAT0/status`
 
if [[ "$status" = "Discharging" ]]
then
        statusColour=$discharge
        status="▼"
else
        statusColour=$healthy
        status="▲"
fi
 
echo "<span color=\"$capacityColour\">$capacity%</span> <span color=\"$statusColour\">$status</span>"

Add the following snippets to /path/to/awesome/rc.lua. I’ll attempt to indicate the approximate location at the top of each snippet.

Create the widget..and don’t forget to adjust the path to the battery.sh script.

-- This goes below the line containing mytextclock = awful.widget.textclock()
 
-- Create a battery widget
battery = wibox.widget.textbox()
function getBatteryStatus()
   local fd= io.popen("/path/to/battery.sh")
   local status = fd:read()
   fd:close()
   return status
end

Add the widget..

-- This goes above the line containing right_layout:add(mytextclock)
    right_layout:add(battery)

Get the widget to refresh every 30 seconds. Put this somewhere near the end of the config file.

-- Battery status timer
batteryTimer = timer({timeout = 30})
batteryTimer:connect_signal("timeout", function()
  battery:set_markup(getBatteryStatus())
end)
batteryTimer:start()
battery:set_markup(getBatteryStatus())

That’s all! Restart awesome and you’ll see a relatively purdy yet unobstrusive battery status display.

8 Replies to “Yet Another Battery Widget (Awesome 3.5.1)”

  1. You’re right, that’s odd. They’re in my script, but somehow they didn’t make it into the post:

    echo "<span color=\"$capacityColour\">$capacity%</span> <span color=\"$statusColour\">$status</span>"

  2. Extended it to my two batteries setup by just changing little to the battery.sh.
    Nice and easy. Thanks.

  3. …and since my laptop is not shutting down if one battery is reaching 0% i inverted the status if-clause to:

    if [[ “$status1” = “Charging” ]]
    then
    statusColour1=$healthy
    status1=”▲”
    else
    statusColour1=$discharge
    status1=”▼”
    fi
    So it no longer shows ▲ when empty. 😉
    Thanks again

  4. That’s probably more sensible than what I’m doing. Didn’t think to consider multiple batteries. Thanks for the suggestion!

Leave a Reply

Your email address will not be published. Required fields are marked *