It seems that a conky if statement argument can't be a string, so a string returned by a Lua function won't work. For example, where my interface is "enp2s0", the statement ${if_up enp2s0}
will work, but ${if_up "enp2s0"}
will not.
A workaround is to include the entire conky if statement in a Lua function. For example:
function findInterface()
local handle = io.popen('ip a | grep "state UP" | cut -d: -f2 | tr -d " "')
local result = handle:read('*a'):gsub('\n$','')
handle:close()
return result
end
function conky_ifupInterface()
local ifup = "${if_up " .. findInterface() .. "}"
ifup = ifup .. "Hello${else}"
ifup = ifup .. "Goodbye${endif}"
return ifup
end
The function conky_ifupInterface()
is then called in conkyrc
with the line:
${lua_parse conky_ifupInterface}
Unfortunately, just returning the first line of the if statement isn't sufficient to satisfy the lua_parse
operator. The entire statement through the ${endif}
has to be returned.
Note that the current implementation of findInterface
will cause the conky config to crash on a call to conky_ifupInterface
if the interface is down because findInterface
will return a null value, resulting in the if statement beginning with ${if_up }
. To make it work for testing, I did the following quick and dirty bit...
function findInterface()
local handle = io.popen('ip a | grep "state UP" | cut -d: -f2 | tr -d " "')
local result = handle:read('*a'):gsub('\n$','')
handle:close()
if result == "" then
handle = io.popen('ip a | grep "state DOWN" | cut -d: -f2 | tr -d " "')
result = handle:read('*a'):gsub('\n$','')
handle:close()
end
return result
end
Yuck! I'm sure you can do better. :^)