问题描述
在 conky 中,如何在模板中嵌套变量?
示例:
${template2 enp0s25} <- WORKS (fixed string)
${template2 ${gw_iface}} < FAILS (nested variable)
${template2 ${execpi 10 ls -d /sys/class/net/enP* 2> /dev/null | sed -e 's,/sys/class/net/,'}} <- FAILS (nested variable command)
我也尝试过(但失败了):
${combine ${template2 ${gw_iface}}}
${combine ${template2} ${gw_iface}}
这里是“模板 2”:
template2 = [[
${if_existing /proc/net/route \1}Wired Ethernet ("\1"):
- MAC: ${execi 5 cat /sys/class/net/\1/address} IP: ${addr \1}
- Max: ${execi 5 /sbin/ethtool '\1' 2>/dev/null | sed -n -e 's/^.*Speed: //p'}${goto 190}${if_match ${downspeedf \1} > 0}${font :bold:size=14}${endif}Down: ${downspeedf \1}kB/s${font}${goto 370}${if_match ${upspeedf \1} > 0}${font :bold:size=14}${endif}Up: ${upspeedf \1}kB/s${font}
${endif}]]
感谢您的帮助。
解决方法
模板有一些限制,因为您无法在参数通过之前对其进行评估。一种解决方法是使用一些 lua
代码显式执行 eval,然后解析模板。例如,
conky.config = {
lua_load = '/tmp/myfunction.lua',...
};
conky.text = [[
${lua myeval template2 ${gw_iface}}
]]
创建lua文件,/tmp/myfunction.lua
持有
function conky_myeval(tpl,var1)
v = conky_parse(var1)
cmd = "${"..tpl.." "..v.."}"
return conky_parse(cmd)
end
lua
函数采用模板名称 tpl
和要计算的参数 var1
。它使用 conky_parse()
评估后者,例如字符串“xxx”,然后构造一个新字符串 "${template2 xxx}"
,该字符串被解析并作为 ${lua}
调用的值返回。
对于较长的示例 ${execpi ...}
也可以这样做。