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 | No Comments »


webpy - a simple, flexible, CRUD framework

logo

I’m constantly finding myself in need of small, quick, personal database programs. Something light and flexible. Quick to mock up, quick to write, easy to use, painless to dump. And I find myself wanting and writing the same kinds of applications again and again.

Sometimes a text file just isn’t enough. And writing redundant SQL queries can get tiresome. Perhaps I need to print nicely-formatted reports. Perhaps I need some web connectivity for retrieving remote information. Or, what if I want to give this program to someone else to run? How many people know how to do database administration? The data needs to be conveniently stored in one place for the easiest possible backups.

What I really need is a framework. A cross-platform framework that will allow me to create these types of applications in a heartbeat. I should even be looking forward to the two minutes it would take to copy a template project and build a whole new application and have it up and running in minutes. Any changes I want to make to the data model should be immediately reflected everywhere with no extra work. But if I want something powerful, I want the framework to be flexible enough to let me get in and make it so.

Interested? Here’s my solution… » Read the rest of this entry


Related Posts

Tags:
Posted in programming on December 11th, 2007 | No Comments »

Git-grep - fast, easy, and smart

logo

As if there aren’t enough reasons already to love git, here’s another: git-grep. Once again putting git heads and shoulders over subversion and every other version control system.

git-grep allows you to search through all files git knows about, in this revision, or any other — and fast.

» Read the rest of this entry


Related Posts

Tags:
Posted in linux on December 6th, 2007 | No Comments »

git and svn

logo

I’ve been doing research lately into git. For the last couple of years, I have been a big subversion fan. All of my programming projects are kept safe in subversion repositories. Once I moved the databases from the bdb to the fsfs backend, maintenance has been a non-issue.

However, subversion leaves a couple things to be desired. The biggest missing feature is distributed control. When I go away for a weekend with my laptop, I’d like to be able to continue working on projects in a disconnected mode. But without access to my repositories, I can’t do that. So I have to do all the merging manually when I get back, which is a pain for some bigger projects.

Well, git was designed to handle situations like this. It was created by Linus Torvalds to handle the versioning needs of the Linux kernel. It’s super fast, super small, and so far I’ve been super pleased with it. There is documentation, a handbook, and a tutorial that I printed out and have gone through a couple times. I’m feeling pretty comfortable with it, so I’ve converted my subversion repositories, and will post some of my projects soon for the world to play with and send in patches.

Maybe one of my days I will put my home directory in git…


Related Posts

Tags:
Posted in programming on September 15th, 2007 | 2 Comments »