如何在Conky中实现基本的Lua功能? [英] How to implement a basic Lua function in Conky?

查看:198
本文介绍了如何在Conky中实现基本的Lua功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向Conky添加一个函数,该函数打印字符串的长度以用于调试.在名为test.lua的文件中的代码非常简单:

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

...然后我像这样加载它.在我的conky.config部分中,我有:

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

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

...在我的conky.text部分中:

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

${lua test "fooo"}

...其中,test是函数的名称,而fooo是要测试的字符串.

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

预期的结果应该是在Conky中打印4,但是我得到的却不是:

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?

推荐答案

关于如何在Conky中实现功能的一些指导:

  • 首先:您必须在使用功能名称之前使用conky_. 否则,在运行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
    

    ... 在您的终端机中,您的Conky将不留任何与您的额外代码相关的值.关于您的功能将不会打印任何内容.

    ...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:

    • 主文件(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 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
    

  • 这篇关于如何在Conky中实现基本的Lua功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆