2

In conky, how do I nest a variable within a template?

EXAMPLES:

${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)

I've also tried (and failed):

${combine ${template2 ${gw_iface}}}
${combine ${template2} ${gw_iface}}

Here is "template2":

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}]]

Thanks for the help.

wonder
  • 33
  • 4

1 Answers1

0

Templates are a little limited as you cannot evaluate the parameters before they are passed through. One workaround is to use a bit of lua code to do the eval explicitly and then parse the template. For example,

conky.config = { 
    lua_load = '/tmp/myfunction.lua',
    ...
};
conky.text = [[
 ${lua myeval template2 ${gw_iface}}
]]

Create the lua file, /tmp/myfunction.lua holding

function conky_myeval(tpl, var1)
 v = conky_parse(var1)
 cmd = "${"..tpl.." "..v.."}"
 return conky_parse(cmd)
end

The lua function takes the name of the template, tpl, and the parameter to evaluate, var1. It evaluates the latter using conky_parse() to, say, string "xxx", then constructs a new string "${template2 xxx}", which is parsed and returned as the value of the ${lua} call.

The same can be done for the longer example ${execpi ...} too.

meuh
  • 11,500
  • 2
  • 29
  • 45
  • Hello meuh. Thank you for your response. I was trying to avoid using external scripts. But it appears that may be my only option. Thank you for the information. – wonder Jun 27 '21 at 20:26