So the way conky works with lua is that you need to declare your lua script in the conky configuration file, and I keep my conky scripts in the amazingly creatively-named ~/.conky/scripts, so my conky has a line in the header section that says
Code:
lua_load = '~/.conky/scripts/conky.lua',
and in my conky lua script (called conky.lua) there's a function called conky_context_colour that takes an expression, three colours, and two numerical values as arguments, and returns the results of the expression coloured based on its value relative to those other values. So, for example, I can call
Code:
${lua_parse context_colour ${hwmon 2 temp 1} ${color4} ${color2} ${color3} 45 75}${color}°C
to display my CPU temperature as one colour if it's less than 45°C, another colour if it's more than 75°C, and a third if it's between them. I use the colorn entries in the header section, but I could have different colours for each call of context_colour if I wanted.
The first part of conky.lua is a boilerplate section. I don't really understand exactly what it does, but it's
Code:
require 'cairo'
function conky_main()
if conky_window == nil then return end
local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
cr = cairo_create(cs)
cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE)
local updates=tonumber(conky_parse('${updates}'))
if updates>5 then
end-- if updates>5
cairo_destroy(cr)
cairo_surface_destroy(cs)
cr=nil
end-- end main function
The actual context_colour function is
Code:
function conky_context_colour( expression,
colour1,
colour2,
colour3,
low_value,
high_value)
local foo=conky_parse(expression)
local bar=tonumber(foo)
if bar < tonumber(low_value)
then colour=colour1
else
if bar > tonumber(high_value)
then colour=colour3
else colour=colour2
end
end
return colour..foo
end
I have that function in a lua script rather than doing it in conky because I didn't want to have to keep evaluating the expression over and over, and the nested ifs make the conky config really hard to read. You might notice that the function call from conky names the function context_colour, but in the script it's named conky_context_colour. I can't remember why that is, but it needs to be like that for some reason.
Obviously this function is dealing with numerical values (you can see that I'm using tonumber() to make sure it's a number before I compare the values) but you can amend it to compare strings. Having already used a script for all the temperatures and fan speeds, I then had somewhere to put other random functions that do similar things, so the displayed information about the mpd that's running on my raspberry pi stereo gets a different image if it's playing or paused, and my laptop shows the signal strength of its WiFi graphically. It's all quite fun once you get started.