Friday, January 23, 2009

Reinventing a Wheel: Status Bar Clock

I've said it before: freedom has its price. I use an unconventional Window Manager (WM) - awesome, and the price I pay for this choice is that I need, on occasion, to reinvent some wheels. Today's wheel is that innocent looking clock on the status bar (or panel, or taskbar, or whatever you call it).

The awesome (v2.3) status bar resides, by default, at the top of the screen, and it shows the list of tags, tiling mode, list of windows and the awesome logo.

One feature that's lacking from the statusbar, that I really need, is for the statusbar to display both the clock (date and time) and the load average. There are three steps to this:
  1. add the appropriate text fields to the awesome statusbar, by adding the following lines in the statusbar section in ~/.awesomerc (search for the string "mystatusbar" and put the following before the closing curly bracket):

    textbox uptime
    {
    style
    {
    font = "mono 8"
    fg = "#999999"
    }
    text = " 0.00 0.00 0.00"
    align = right
    text_align = right
    width = 0
    mouse
    {
    button = "1"
    command = "spawn"
    arg = "exec x-terminal-emulator -e htop"
    }
    }
    textbox clock
    {
    style
    {
    font = "mono 8"
    }
    text = " Day Mon 00, 00:00 "
    align = right
    text_align = left
    width = 0
    }

    (this also demonstrates how to perform an action when the user clicks the uptime text box)
  2. write a script that continuously generates the text to be displayed and feeds that into awesome-client:

    #!/bin/sh
    #
    while true
    do
    if [ -S ~/.awesome_ctl.0 ]; then
    while true
    do
    echo "0 widget_tell mystatusbar clock text `date +\"%a %b %d, %k:%M\"` "
    echo "0 widget_tell mystatusbar uptime text `uptime | sed 's/.*://; s/,//g'` "
    echo "" # an empty line flushes data inside awesome
    sleep 10
    done | awesome-client
    else
    sleep 1
    fi
    done
    (I prefer the 24-hour "military-style" format)
  3. modify ~/.xprofile to launch this script when the desktop is started, by adding the following lines:
        ps -C "awesome-clock.sh" -o pid= | xargs /bin/kill -9 > /dev/null 2>&1
    ~/bin/awesome-clock.sh &
    (I put these scripts in ~/bin, you're obviously free to do something else).

The clock is now ticking...

(reference: the awesome wiki)

2 comments:

  1. Why are you still using awesome 2.3? Awesome 3 has been out for a while, and the first 3.2 RC came out a few days ago.

    ReplyDelete
  2. Two reasons:

    1) Version 2.3 is what I have available in Debian/Testing. That's as cutting an edge as I'm willing to go right now. I used to be bolder, but I also had more free time on my hands.

    2) I'm rather happy with awesome as is. I was a happy ratpoison user, so awesome 2.3 is an upgrade in my mind. I'd be happy to get tray icons and such working, and the flexibility of Lua scripting, but I'm in no hurry.

    ReplyDelete