'conky using a function call within an if statement
How do I call a function after loading it in conkyrc? For example:
I'm trying to get the active interface name which is returning properly
${lua conky_findInterface} #gives me device name
function conky_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
How do I use it more dynamically? Such as :
${if_up ${lua_parse "${lua conky_findInterface}"}} #this does not work nor do my other variations
Hello
${else}
Goodbye
${endif}
Solution 1:[1]
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. :^)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | David Yockey |
