linux - Conky 文件中 pre_exec 命令的 Lua "replacement"是什么?

标签 linux lua conky

我不擅长编程,但我试图摆弄一个我喜欢的 conky_rc 文件,我发现它看起来非常简单。

如标题所述,我现在了解到 pre_exec 之前的命令早已被 Lua 删除并取而代之。

不幸的是,除了 https://github.com/brndnmtthws/conky/issues/62 之外,我似乎找不到与此直接相关的任何内容.跟帖 https://github.com/brndnmtthws/conky/issues/146引用它,它的“解决方案”指出:基本上,没有替代品,您应该使用 Lua 或使用非常大的间隔和 execi。

我发现了更多线程,它们都包含关于为什么停止使用此功能的问题,但没有实际答案。所以,重申一下,我对 Lua 完全一无所知(我以前听说过,现在我添加了一些网站明天要看,因为我晚上大部分时间都在试图弄清楚这个 Conky 的东西),我可能会放弃并执行 execi 选项(我的计算机可以处理它,但我只是认为它的效率非常低)。

是否有合适的 Lua 选项?如果是这样,有人可以指导我查看手册或 wiki,或者解释一下吗?或者是“适当的”Lua 解决方案吗?

@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')

最佳答案

comment by asl97在您引用的第一页上似乎提供了答案,但一些解释可能会有所帮助。

asl97 提供了以下通用的 Lua 函数来替代 $pre_exec,前面有一个 require 语句使 io 可用供函数使用:

require 'io'

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

将此代码块添加到您的 conky 配置文件将使该功能可以在其中使用。为了进行测试,我将其添加到 conky.config = { ... } 部分上方。

调用 Lua pre_exec 函数将返回一个字符串,其中包含传递给它的命令的输出。 [[]] 的 conky.text 部分也是一个字符串,因此可以使用 ..< 将其连接到 pre_exec 返回的字符串运算符,如asl97提供的用法部分所示。

在我的测试中,我做了以下愚蠢的事情,它按预期工作,显示“Hello World!”以及 date 函数的输出,在我的 conky 显示的顶部和下方都有间距:

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

当然,更重要的命令可以与 pre_exec 一起使用,如 asl97 所示。

asl97 没有解释的一件事是如何提供如何连接,以便 pre_exec 输出位于 conky 显示的中间,而不仅仅是开头。我测试了一下,发现你可以像下面这样:

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

关于linux - Conky 文件中 pre_exec 命令的 Lua "replacement"是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58360861/

相关文章:

c++ - 使用 Eclipse 调试时如何在 Linux 中查看 C++ shared_ptr 内容?

linux - 如何在多个命令中优先选择一个命令

linux - 与脚本程序相关的 bash 脚本移植问题

python - 我怎样才能将 Lua 嵌入到 Python 3.x 中?

if-statement - 带有 if_match 和 'and' 的 conky

linux - 使用 getopts (bash) 的多个选项参数

lua - 如何在Lua中将字符串转换成表

algorithm - 最大连续和算法的实现

bash - 确定连接是有线还是无线?

linux - 无法在docker上执行conky