Read more

How to: Build a "generic observer" widget for the awesome window manager

Arne Hartherz
May 15, 2013Software engineer at makandra GmbH

If you want a widget for awesome Show archive.org snapshot that runs a command regularly (every X seconds) and puts the output into your awesome panel, this is for you.

Illustration online protection

Rails professionals since 2007

Our laser focus on a single technology has made us a leader in this space. Need help?

  • We build a solid first version of your product
  • We train your development team
  • We rescue your project in trouble
Read more Show archive.org snapshot

Put the code below into your ~/.config/awesome/rc.lua. It supplies two methods:

  • execute_command will run a command and return its output. Multiple lines will be joined into one line. The code is from the awesome wiki Show archive.org snapshot .

  • widget_with_timeout is a method that takes a command to run and a timeout in seconds. It'll return an object which contains a widget that you can put into your panel.
    ^
    -- From: http://awesome.naquadah.org/wiki/Awesome_3_configuration
    function execute_command(command)
    local fh = io.popen(command)
    local str = ""
    for i in fh:lines() do
    str = str .. i
    end
    io.close(fh)
    return str
    end

    -- Helper function to build all you need
    function widget_with_timeout(command, seconds)
    local object = {}
    object.command = command
    object.widget = widget({ type = "textbox" })

    object.update = function ()
      local result = execute_command(object.command)
      object.widget.text = " " .. awful.util.escape(result) .. " "
    end
    
    object.timer = timer({ timeout = seconds })
    object.timer:add_signal("timeout", object.update)
    object.timer:start()
    
    object.update() -- Initialize immediately
    return object
    

    end

Now use it in your rc.lua like this:

-- Get current track from DeaDBeeF, every 3 seconds
nowplaying = widget_with_timeout('deadbeef --nowplaying "%a - %t"', 3)

-- Remember to add the widget to your panel:

mywibox[s].widgets = {
  ...,
  nowplaying.widget,
  ...
}
Arne Hartherz
May 15, 2013Software engineer at makandra GmbH
Posted by Arne Hartherz to makandra dev (2013-05-15 16:28)