Tuesday, February 9, 2010

Tips for writing Lua scripts and getting them working in conky

Some of these will be very basic.

1. Always launch your conkyrc through the terminal. You will get feedback about errors here that are invaluable for fixing them. More on this later

2. Setup a folder where you are going to keep all of your conky configs and create a conky_start shell script
mine contains the following:

#!/bin/bash
conky -c ~/.conky/.conkyrc_tabletest

You need to make this script executable. I do this through my file manager as I'm not familiar with the terminal commands to do this.

This script will launch the conky I have saved as conkyrc_tabletest in the folder ".conky" in my home directory. Obviously you muct have created a conky before you can try and launch it!

Then launch the conky in the terminal as follows:

$ /home/mcdowall/.conky/conky_start

I have my regular conky config launch on startup, but I always launch a config I'm working on by editing this conky_start file. Just make sure that if you have a conky (or more than 1) running on your system that the conky you are about to launch will not interfere. My everyday conky is set to top_right... so I set a conky I'm working on to open elsewhere.

3. You are going to have to stop and restart the conkyrc *alot*

a command like:

$ killall conky

should do the trick. If your conky quits on you and just disappears always perform the above command. Don't just start it up again or you will get multiple instances on conky running and things will start to slow down.

4. Regarding Lua scripts in general, use the print function to troubleshoot when something isn't working right.

for example I have the following code:

cpunum=conky_parse ('${cpu}')
cpu=tonumber(cpunumber)

but for some reason things aren't working as expected. So put in the print command as so:

cpunum=conky_parse ('${cpu}')
cpu=tonumber(cpunumber)
print (cpu)

If the above code is correct then you will get the output of cpu printed only in the terminal window. Of course if there is something wrong above this point that has broken the script, you won't get anything printed out.

The above code is probably too simple to go wrong, but when you are trying more complicated things, like string editing with gsub or the string:split function or trying to work out some tricky mathematics, the print command is invaluable.

5. Put enough comments into the script so you know whats going on.
When you go back to a script and want to edit something, it's much easier if you have divided your script and functions into descriptive sections with comments. -- before text makes the text a comment.

6. Try and use descriptive names to your strings.
As above, when you come back to look at a script, a string names cpu_num gives you a good clue about what the string is about... while something more arbitrary like "a18" doesn't.

Terminal Output

Sometimes the terminal output is very helpful, sometimes it's not. I would say the most common thing I see in the terminal is this:

Conky: llua_do_call: function conky_draw_shape execution failed: attempt to call a nil value

This happens particularly because I don't have enough end's in my scripts. I should get into the habit of writing my scripts out properly like this:

function
if x==y then
...................return z
..............else
...................return a
.............end
end
(ignore the ... thats just so the format is preserved!)
But I don't.

Another thing about the above error is that if you go to your Lua script and edit the script so that you think you have corrected the error, many times when you save the Lua script the terminal just keeps on repeating the error making you think that it isn't fixed. When you get the
attempt to call a nil value error it is a good idea to issue the killall conky command. Then restart the conkyrc.

But before you restart, scroll back through the error messages
and look at the lines just after the conkyrc was launched... this is where the terminal will tell you why it's giving the errors. For example:

mcdowall@mcdowall-desktop:~$ /home/mcdowall/.conky/conky_start
Conky: llua_load: /home/mcdowall/lua/table.lua:176: 'end' expected (to close 'function' at line 151) near ''
Conky: forked to background, pid is 16955
mcdowall@mcdowall-desktop:~$
Conky: desktop window (87) is root window
Conky: window type - desktop
Conky: drawing to created window (0x2c00001)
Conky: drawing to double buffer
Conky: llua_do_call: function conky_draw_shape execution failed: attempt to call a nil value

and we see:
Conky: llua_load: /home/mcdowall/lua/table.lua:176: 'end' expected (to close 'function' at line 151) near '

eof = end of function which means that indeed there is a missing end ( 'end' expected).

Go to the line in question (here it's telling us that the function which begins on line 151 should have an end on line 176) and fix the problem and try again.

mcdowall@mcdowall-desktop:~$ /home/mcdowall/.conky/conky_start
Conky: llua_load: /home/mcdowall/lua/table.lua:92: '}' expected (to close '{' at line 76) near 'in_blue'

this error is because of a missing comma on line 76.

So the terminal is invaluable!

Sometimes the helpful part of the error gets repeated in the terminal, and usually when this happens, fixing the problem in the Lua script and saving the script are enough to get past it.

No comments:

Post a Comment