Jens Peters
Jens Peters

Reputation: 2161

Conky - Lua Config - Custom variables

I want to use custom variales in my Lua config of conky to share config between computers. Why does the following does not work, it uses simple Lua code:

conky.config={..}

-- set variables
work = "COMPUTERNAME"
lan  = "wlp9s0"
-- compare with current host name (conky's global variable)
if work == nodename then
  lan = "enp3s0"
end 

-- use $lan in conky's text
conky.text = [[${color yellow}$lan ${alignr}${addr wlp9s0}]]

I did not find any documentation or example how to use custom defined variables. $lan is not resolved and printed as ${lan}

Upvotes: 1

Views: 6395

Answers (1)

Oka
Oka

Reputation: 26355

Without using Conky, I'm going to make an answer based on some assumptions I've made after reading the various configuration sections found on the wiki.

It appears to me that the 'variables' one uses in the conky.text field, and other template fields, are not part of the Lua environment. That is to say, that the $ and ${} syntax probably does not perform environment lookups to interpolate values. This might also mean that the nodename variable you're comparing with is actually nil.

In any event, if your lan variable is not being interpolated, the quick fix is to simply concatenate your strings:

conky.text = [[${color yellow}]] .. lan.. [[ ${alignr}${addr wlp9s0}]]

Or consider writing your own string iterpolation function, if you want a cleaner looking string:

local function interp (s, t)
    return s:gsub('(#%b{})', function (w)
        return t[w:sub(3, -2)] or w
    end)
end

conky.text = interp([[${color yellow}#{lan} ${alignr}${addr wlp9s0}]], {
    lan = lan
})

Note, in the event that nodename is not part of the Lua environment, you can try using io.popen to run hostname manually, and then read from the file handle it returns.

Upvotes: 4

Related Questions