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

Updated . Posted . Visible to the public.

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.

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
Last edit
Keywords
repeat, minutes, execute, monitor, result
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2013-05-15 14:28)