Skip to content
Georgi Valkov edited this page Dec 26, 2012 · 1 revision

Conky features built in Lua support since 1.7.1. Lua is a "lightweight, reflective, imperative and functional programming language, designed as a scripting language with extensible semantics as a primary goal" (source] which makes it ideal for integration into Conky. To get started using Lua in Conky, you'll need a copy of Conky 1.7.1 or newer. Configure Conky with the --enable-lua option to enable Lua support at compile time.

@TODO@

Short Example

Place the following in your conky config:

lua_load <path to the luascript>
TEXT
${lua short}

... and place the following in your luascript:

function conky_short()
  return "A short lua example"
end

A bit more useful example

Here is a really quick example of using Lua with Conky. This script will colour the top values according to CPU and memory usage.

In your conkyrc, add the following:

lua_load <path to script below>
...
TEXT
${color}Name              PID    CPU%   MEM%
${lua_read top_cpu_colour  ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}}
...
${color}Mem usage
${lua_read top_mem_colour  ${top_mem name 1} ${top_mem pid 1} ${top_mem cpu 1} ${top_mem mem 1}}
...

Save the following Lua script somewhere to be loaded from the above conkyrc:

-- Conky Lua scripting example
--
do
	-- this function changes Conky's top colour based on a threshold
	function conky_top_colour(value, default_colour, upper_thresh, lower_thresh)
		local r, g, b = default_colour, default_colour, default_colour
		local colour = 0
		-- in my case, there are 4 CPUs so a typical high value starts at around ~20%, and 25% is one thread/process maxed out
		local thresh_diff = upper_thresh - lower_thresh
		if (value - lower_thresh) > 0 then
			if value > upper_thresh then value = upper_thresh end
			-- add some redness, depending on the 'strength'
			r = math.ceil(default_colour + ((value - lower_thresh) / thresh_diff) * (0xff - default_colour))
			b = math.floor(default_colour - ((value - lower_thresh) / thresh_diff) * default_colour)
			g = b
		end
		colour = (r * 0x10000) + (g * 0x100) + b -- no bit shifting operator in Lua afaik

		return string.format("${color #%06x}", colour%0xffffff)
	end

	-- parses the output from top and calls the colour function
	function conky_top_cpu_colour(arg)
		-- input is ' ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}'
		local cpu = tonumber(string.match(arg, '(%d+%.%d+)'))
		-- tweak the last 3 parameters to your liking
		-- my machine has 4 CPUs, so an upper thresh of 25% is appropriate
		return conky_top_colour(cpu, 0xd3, 25, 15) .. arg
	end

	function conky_top_mem_colour(arg)
		-- input is '${top_mem name 1} ${top_mem pid 1} ${top_mem cpu 1} ${top_mem mem 1}'
		local mem = tonumber(string.match(arg, '%d+%.%d+%s+(%d+%.%d+)'))
		-- tweak the last 3 parameters to your liking
		-- my machine has 8GiB of ram, so an upper thresh of 15% is appropriate
		return conky_top_colour(mem, 0xd3, 15, 5) .. arg
	end
end