Search code examples
linuxluaconky

What is the Lua "replacement" for the pre_exec command in Conky files?


I'm not great at programming, but I was trying to fiddle around with a conky_rc file I liked that I found that seemed pretty straight-forward.

As the title states, I have now learned that the previous command of pre_exec has been long removed and superseded by Lua.

Unfortunately, I cannot seem to find anything directly related to this other than https://github.com/brndnmtthws/conky/issues/62. The thread https://github.com/brndnmtthws/conky/issues/146 references it, and its "solution" states: Basically, there is no replacement and you should use Lua or use a very large interval and execi.

I have found a few more threads that all include the question as to why this function was discontinued, but with no actual answers. So, to reiterate mine, I have absolutely no knowledge of Lua (I've heard of it before, and I've now added a few websites to look at tomorrow since I have spent most of the evening trying to figure out this Conky thing), and I'll probably just give up and do the execi option (my computer can handle it but, I just think it's so horribly inefficient).

Is there an appropriate Lua option? If so, would someone please direct me to either the manual or wiki for it, or explain it? Or is the "proper" Lua solution this?

@Vincent-C It's not working for your script is because the function ain't getting call. from the quick few tests I did, it seem lua_startup_hook need the function to be in another file that is loaded using lua_load, not really sure how the hook function thingy all works cause I rather just directly use the config as lua since it is lua.

Basically just call the io.popen stuff and concat it into conky.text

conky.text = [[ a lot of stuff... ${color green} ]];

o = io.popen('fortune -s | cowsay', 'r') conky.text = conky.text ..
o:read('*a')

Solution

  • The comment by asl97 on the first page you cited appears to provide an answer, but a bit of explanation would probably help.

    asl97 provides the following general purpose Lua function to use as a substitute for $pre_exec, preceded by a require statement to make io available for use by the function:

    require 'io'
    
    function pre_exec(cmd)
        local handle = io.popen(cmd)
        local output = handle:read("*a")
        handle:close()
        return output
    end
    

    Adding this block of code to your conky configuration file will make the function available for use therein. For testing, I added it above the conky.config = { ... } section.

    Calling the Lua pre_exec function will return a string containing the output of the command passed to it. The conky.text section from [[ to ]] is also a string, so it can then be conactenated to the string returned by pre_exec using the .. operator, as shown in the usage section provided by asl97.

    In my test, I did the following silly bit, which worked as expected, to display "Hello World!" and the output of the date function with spacing above and below each at the top of my conky display:

    conky.text = pre_exec("echo; echo Hello World!; echo; date; echo")..[[
        -- lots of boring conky stuff --
    ]]
    

    More serious commands can, of course, be used with pre_exec, as shown by asl97.

    One thing that asl97 didn't explain was how to provide how to concatenate so that the pre_exec output is in the middle of the conky display rather than just the beginning. I tested and found that you can do it like the following:

    conky.text = [[
        -- some conky stuff --
    ]]..pre_exec("your_important_command")..[[
        -- more conky stuff --
    ]]