Topic: HOW TO : using lua scripts in conky
Using Lua scripts in conky
this how to is based on what i know... not what is known ![]()
part 1 - how does a lua script work
this post!
part 2 - displaying text and working with colors
http://crunchbanglinux.org/forums/post/180248/#p180248
part 3 - drawing lines
http://crunchbanglinux.org/forums/post/180359/#p180359
part 4 - drawing rectangles, cirlces and arcs
http://crunchbanglinux.org/forums/post/180388/#p180388
part 5 - making a bar meter and a circle meter
http://crunchbanglinux.org/forums/post/180661/#p180661
part 6 - some more drawing calculations and introduction to IF's
http://crunchbanglinux.org/forums/post/180798/#p180798
PART 7 - adding our alarm color change feature and looking at more complex if statements
http://crunchbanglinux.org/forums/post/181076/#p181076
PART 8 - turn our code into a function
http://crunchbanglinux.org/forums/post/181338/#p181338
PART 9 – sending information to functions
http://crunchbanglinux.org/forums/post/181629/#p181629
PART 10, sending info to functions using tables
http://crunchbanglinux.org/forums/post/183756/#p183756
Part 11a FOR LOOPS and A CPU HISTORY CHART
http://crunchbanglinux.org/forums/post/184830/#p184830
Part 11b CPU HISTORY CHART CONTINUED
http://crunchbanglinux.org/forums/post/185253/#p185253
Part 11c DRAWING THE CPU HISTORY DATA
http://crunchbanglinux.org/forums/post/185753/#p185753
Part 12a DRAWING A CLOCK and circular things
http://crunchbanglinux.org/forums/post/186112/#p186112
Part 12b CLOCK CONTINUED
http://crunchbanglinux.org/forums/post/186159/#p186159
-----------------------------------------------------------------------------------------------
PART 1 how does a lua script work?
in conky there are several lua related settings and objects
primarily for using lua script there are 2 settings
lua_load
lua_draw_hookthat come before TEXT
lua_load is where you give conky the location of the script to use
eg
lua_load /home/username/scripts/script.lua
so that one is pretty easy to understand
the second line
lua_draw_hook (can be lua_draw_hook_pre or lua_draw_hook_post)
tells conky which function to run from the lua script that was loaded.
This takes a little more explanation.
A bare bones lua script for use in conky might look like this
--this is a lua script for use in conky
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)
local updates=tonumber(conky_parse('${updates}'))
if updates>5 then
--##############################
print ("hello world")
--##############################
end-- if updates>5
cairo_destroy(cr)
cairo_surface_destroy(cs)
cr=nil
end-- end main functionline by line we have
--this is a lua script for use in conky
anything preceded by -- is a comment in lua
you can write a longer comment section starting with --[[ and ending with ]] like so
--[[ this is a comment
and it can span multiple
lines until you end it with ]]next we have
require 'cairo'
cairo is the name of the graphics library that lua will be using to get all those fancy graphics showing up in conky
this line loads up that library
then
function conky_main()
this is our main function and the one we will set in the conkyrc for lua_draw_hook
if you open a lua script and not sure which function to set in conkyrc, look for "conky_" in the function name
the next 3 lines are standard setup lines and are ONLY required for the main function that will be called in the conkyrc
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)This is creating the "surface" onto which the text and graphics will be drawn. If you want the lua script to draw anything to conky you need these setup lines.
then we have lines about conky updates
local updates=tonumber(conky_parse('${updates}'))
if updates>5 thenthese lines are only important if you plan on reading cpu% in the lua script, but its generally a good idea to have these lines anyway (one less thing to cause an error)
NOTE if you are not going to be using conky_parse("${cpu}") then by all means remove these lines
BUT if you remove the line that says "if updates>5 then" you MUST also remove the matching "end" from lower down in the script. This is why i like to comment what my ends are ending ![]()
end-- if updates>5this lines
local updates=tonumber(conky_parse('${updates}'))has a few important things to note about it...
1. the term "local" at the beginning (i'll cover that later)
2. the use of strings and the = sign
in lua you hold information in strings by saying:
string = information
information can be other strings, plain text, numbers etc (more about that later)
eg
string1=5
string2=9
string3=string1*string3in this case string3 holds the value 45
string names cannot start with numbers and cannot contain spaces
if a string lower down in the script has the same name as higher, the info in the first string just gets overwirtten
eg
string1=7
print (string1) --> 7
string1=9
print (string1) --> 93. the use of tonumber
if you want lua to do calculations or comparisons based on a number, it can be a good idea to make sure you are setting a string as a number by using tonumber(), particularly when you are taking numbers generated by conky objects or from other means. Just because the output of a conky object is a number DOES NOT mean that when it is set to a string lua will recognise it as a number! It can be a big pain to go tracking down an error to realise it was because the script was not setting an output as a number
4. the use of conky_parse
you can get the output of any conky object into lua this way
eg
cpu=conky_parse("${cpu}")
memory=conky_parse("${memperc}")
home_used=conky_parse("${fs_used /home}")you can even use things like if_ objects from conky using conky_parse, which can be useful as switches
internet=conky_parse("${if_up wlan0}1${else}0${endif}")now the string "internet" will have a value of 1 or 0 depending on the outcome of if_up
next line in our lua is
if updates>5 then
this if statement is closed lower in the script by an end
end-- if updates>5
i always like to comment about what exactly the "end" ends as in many script the end that closes out "if updates>5 then" can be tricky to pick out and can be a long way down the script
what we are saying is if updates (the value of which is set with conky_parse) is greater than 5 then do everything between the "then" and the closing "end"
missing ends can be a pain to locate
every "if" "for" "while" or other loop needs a matching end to close it!
NOTE the value of updates is the number of times that conky has updated as set by this line in your conkyrc
update_interval 1because it was set using conky_parse to read the conkt object ${updates}
so if you have your interval set to 1 (ie every second), you are going to wait 6 seconds from the time conky starts to the time you see anything from lua if you are using the "if updates>5" line
if update_interval was 10, you would be waiting 60 seconds
clearly you don't want to wait that long to see your lua
later on I'll deal with executing commands in lua (analogous to exec or execi in the conkyrc)
there we will see the use of the conky update interval as a timing mechanism and in that case we can think in more detail about how conkyrc update_interval and the lua script interact
so next we have the body of the script, the stuff we want the lua script to do
--##############################
print ("hello world")
--##############################i like to use obvious comment lines to differentiate between the setup parts of the functions and the the body of the functions so that i can navigate easier ![]()
the command "print ()" will not show anything up in conky, but will be printed in the terminal
always run conky from the terminal when testing out a lua script!
print ()
can be a very useful way of finding error in the script if things aren't working
then we come to closing out the function
end-- if updates>5
cairo_destroy(cr)
cairo_surface_destroy(cs)
cr=nil
end-- end main functionthese lines close out the if function and do some cleaning up
one thing that lua can do is to eat up memory over time
these lines help to avoid that.
the final end ends the main function and your done.
I have a script named blank.lua which is just that, a script set up with an empty main function, so i don't have to remember to type out all the setup lines each time ![]()
NOTE about writing the code
remember! any examples i write are writte as if i were writing a real script
(in fact as i go on i'll check my code examples to make sure they really do what i say they do
)
this is how I write my code
one point to make in particular is about the use of spaces in the code
as you can see, i like to write everything tight together
red,green,blue,alpha=1,1,1,1and use few spaces
but in fact lua is very versatile/forgiving in terms of spaces and tabs
for example
value=16
print (value) -->16
value = 16--with spaces
print (value) -->16
value = 16--tabbed
print (value) -->16
value = 16--any kind of spacing you want
print (value) -->16this also applies to things like the rectangle command
cairo_rectangle (cr,50,50,60,40)
cairo_rectangle (cr, 50 ,50 ,60 ,40 )
cairo_rectangle (cr, 50 , 50 ,60 ,40 )all give you the same rectangle
however, its always a good idea to keep things neat and tidy!
Last edited by mrpeachy (2012-02-03 23:17:31)
I have a blog, it's mostly about conky and lua stuff... go here.




