Search code examples
awesome-wm

Execute custom command in awesomewm tooltip


I have a simple widget to display free memory:

myFreeMemory = awful.widget.watch('bash -c "free -h | awk \'/^Mem:/ {print $3}\'"', 1)

This line produces a single number.

I would like to create a tooltip for it that runs a custom command:

local free_memory_tooltip = awful.tooltip
{
    objects        = { myFreeMemory },
    timer_function = function()
        return "free -h"
    end,
    font = "monaco 18",
    timeout=0,
    opacity=0.9,
    bg="#000000",
    fg="#ffffff",
    align="top_left"
}

Instead of return "free -h", what should I put to execute this command and return the textual output?


Solution

  • Simplest solution might be to use return io.popen("free -h"):read("*a"), but this uses io.popen which one best avoids in AwesomeWM.

    Best solution might be to write a parser for /proc/meminfo yourself. Yuck.

    Intermediate solution would be something like the following:

    local last_result = ""
    local function my_timer_function()
        local cmd = "free -h | awk '/^Mem:/ {print $3}'""
        awful.spawn.easy_async_with_shell(cmd, function(result)
            last_result = result
            free_memory_tooltip:set_markup(last_result)
        end)
        return last_result
    end
    -- Call it once to initialise its state; otherwise you would get a brief flicker of empty text
    my_timer_function()