Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a basic Lua function in Conky?

I am trying to add a function to my Conky which prints the length of a string for debug purposes. The code, inside a file called test.lua, is pretty trivial:

function test(word)
return string.len(word)
end

...and I load it like this. In my conky.config section I have:

lua_load = '/home/xvlaze/test.lua',
lua_draw_hook_pre = 'test'

...in the conky.text section I have:

${lua test "fooo"}

...where test is the name of the function and fooo the string to test.

The expected result should be a printed 4 in Conky, but instead of that I get:

conky: llua_do_call: function conky_test execution failed: /home/xvlaze/test.lua:2: attempt to index a nil value (local 'string')
conky: llua_getstring: function conky_test didn't return a string, result discarded

I have browsed through the documentation, but I can't find anything. Does anybody know where the failure is?

like image 478
xvlaze Avatar asked Mar 09 '23 12:03

xvlaze


1 Answers

Several guidances on how to implement functions in Conky:

  • First of all: YOU MUST USE conky_ BEFORE YOUR FUNCTION'S NAME. Otherwise, you will get the following error when running your Conky:

    attempt to call a nil value
    
  • Secondly: YOU MUST ALWAYS RETURN A VALUE. I don't mind repeating it - it is crucial. Otherwise, you will get:

    function foobar didn't return a string, result discarded
    function_result
    

    ...in your terminal, and your Conky will be left empty of values related to your extra code. Nothing will be printed regarding your function.

  • Last but not least: YOU MUST ALWAYS CALL YOUR FUNCTION LIKE:

    lua_load = '/path/to/function.lua',
    -- Whatever content...
    ${lua function_name function_parameter1 function_parameterN} -- In case you use more than one parameter.
    

In summary, a dummy function template could be:

  • MAIN FILE (conky.conf):

    conky.config = {
        -- Whatever content... Lua styled comments.
        lua_load = '/path/to/function.lua',
    }
    
    conky.text = [[
        # Whatever content... In this section comments are started with '#'!    
        ${lua function_name parameter}
    ]]
    
  • FUNCTION FILE:

    function conky_function_name(parameter)
        -- Whatever content... Remember this is Lua, not conky.text syntax. Always use '--' comments!
        return whatever -- No return, no party. A function MUST always return something!
    end
    
like image 66
xvlaze Avatar answered Mar 20 '23 06:03

xvlaze