简体   繁体   English

Conky 文件中 pre_exec 命令的 Lua “替换”是什么?

[英]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.我不擅长编程,但我试图摆弄一个我喜欢的 conky_rc 文件,我发现它看起来很简单。

As the title states, I have now learned that the previous command of pre_exec has been long removed and superseded by Lua.正如标题所述,我现在了解到 pre_exec 的先前命令早已被删除并被 Lua 取代。

Unfortunately, I cannot seem to find anything directly related to this other than https://github.com/brndnmtthws/conky/issues/62 .不幸的是,除了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.线程https://github.com/brndnmtthws/conky/issues/146引用了它,它的“解决方案”指出:基本上,没有替代品,您应该使用 Lua 或使用非常大的间隔和执行。

I have found a few more threads that all include the question as to why this function was discontinued, but with no actual answers.我发现了更多的线程,它们都包含关于为什么这个 function 被停产的问题,但没有实际的答案。 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).所以,重申一下,我对 Lua 完全一无所知(我以前听说过,现在我已经添加了一些网站供明天查看,因为我花了大部分时间试图弄清楚这个 Conky 的事情),我可能会放弃并执行 execi 选项(我的计算机可以处理它,但我只是认为它的效率非常低下)。

Is there an appropriate Lua option?是否有合适的 Lua 选项? 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?还是“正确”的 Lua 解决方案?

@Vincent-C It's not working for your script is because the function ain't getting call. @Vincent-C 它不适用于您的脚本,因为 function 没有接到电话。 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. 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基本上只需调用 io.popen 东西并将其连接到 conky.text

 conky.text = [[ a lot of stuff... ${color green} ]]; o = io.popen('fortune -s | cowsay', 'r') conky.text = conky.text.. o:read('*a')

The comment by asl97 on the first page you cited appears to provide an answer, but a bit of explanation would probably help. asl97在您引用的第一页上的评论似乎提供了答案,但一些解释可能会有所帮助。

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: 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.将此代码块添加到您的 conky 配置文件将使 function 可在其中使用。 For testing, I added it above the conky.config = {... } section.为了测试,我将它添加到conky.config = {... }部分之上。

Calling the Lua pre_exec function will return a string containing the output of the command passed to it.调用 Lua pre_exec function 将返回一个字符串,其中包含传递给它的命令的 output。 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. [[]]的 conky.text 部分也是一个字符串,因此可以使用..运算符将它连接到 pre_exec 返回的字符串,如 asl97 提供的用法部分所示。

In my test, I did the following silly bit, which worked as expected, to display "Hello World!"在我的测试中,我做了以下傻事,它按预期工作,显示“Hello World!” and the output of the date function with spacing above and below each at the top of my conky display:date为 function 的 output 在我的 conky 显示屏顶部的上方和下方各有间距:

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.当然,更严重的命令可以与 pre_exec 一起使用,如 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. asl97 没有解释的一件事是如何提供如何连接,以便 pre_exec output 位于 conky 显示的中间,而不仅仅是开始。 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 --
]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM