Showing battery life in awesome with Lua

logo

On my Asus EEE PC 701, I don’t have a lot of screen real estate (800×480 on a 7″ screen). Also, using the touchpad is not the most efficient method of controlling a window manager, since it requires more precision than I care to use. These factors make a tiling window manager — like awesome — ideal.

I’ve already written about some of my experiences with awesome and Lua, but it’s time to accept the bad with the good and move on. So let’s get my desktop configured the way I like.

My desktop

The only things I really need to show on my screen are a few tags (1-3), the taskbar, the layout indicator (although I’ll go months without using anything other than the “max” layout), and battery life on my laptop. I’ve given in to the default color scheme to make it easier for me to update my .awesomerc.lua, since I download the git HEAD on a weekly basis.

Here is a typical view of my streamlined desktop:

screen_thumb

Click here to view full-size

This layout allows me to navigate quickly from one window to another. Now whenever I use a non-tiling window manager, I get irritated by the time wasted on organizing windows on the screen. Shouldn’t the window manager do that? :)

Displaying battery power

Here is a little bash script that I have in my ~/bin directory to display the current battery life as a percentage.

#!/bin/bash

### BATTERY
REM=`awk '/remaining capacity/ { print $3 }' /proc/acpi/battery/BAT0/state`
LAST=`awk '/last full/ { print $4}' /proc/acpi/battery/BAT0/info`
#STATE=`awk '{print $2}' /proc/acpi/ac_adapter/AC0/state`
#if [ "$STATE" = "on-line" ]; then
  #BAT=$(echo $REM $LAST | awk '{printf "Bat: %.1f%%, AC", ($1/$2)*100'})
#else
  #PRESENT=`awk '/present rate/ { print $3}' /proc/acpi/battery/BAT0/state`
  #BAT=$(echo $REM $LAST $PRESENT | \
    #awk '{printf "Bat: %.1f%%", ($1/$2)*100}')
  #  awk '{printf "Bat: %.1f%%, %d min", ($1/$2)*100, ($1/$3)*60}')
#fi
PERCENT=$(echo $REM $LAST | awk '{printf "%d", ($1/$2)*100'})

echo "$PERCENT%"

Next, we’ll need a way to read the output of a system call in Lua. I’ve added this function in my .awesomerc.lua:

function get_command_output (command)
    local c = io.popen(command)
    local output = {}
    i = 0
   return c:read("*line")
end

This code runs the command you pass in, and returns the first line of output. Now we can retrieve the battery life for display in an awesome widget. In the hook_timer function, which I’ve set to run every 10 seconds, you retrieve the output of the battery command and put it in the textbox, like so:

    function hook_timer ()
        -- For unix time_t lovers
        -- mytextbox:set("text", " " .. os.time() .. " time_t ")
        -- Otherwise use:
        -- mytextbox:set("text", " " .. os.date() .. " ")
        mytextbox:set("text", " " .. get_command_output("battery") .. " ")
    end

My .awesomerc.lua

My latest .awesomerc.lua is always located here. Since I’m keeping up with the git HEAD, I try to minimize the differences against the project configuration file, while at the same time making sure I have a customized environment that is fast and efficient.


Related Posts

Tags:
Posted in linux on June 26th, 2008 |

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.