Saturday, February 27, 2010



Another, and most likely the last, variation on my valentine flower Lua script and a final entry in the conky of the month competition on the conky blog.

The biggest change here is the use of shading to the flower petals. Also I changed the shapes that were transformed by the output of the various conky objects so that there is no overlap. I think these changes have added quite a bit of eye-candy value to this conky and the effect was quite simple to apply.

The hearts were also drawn with cairo in the Lua script and shaded to match the flowers. Luckily I had a heart shape already on file from my first flower conky. I tweaked it a bit for shape, inspired by a wallpaper I found here.

I made an option to change the number of petals for each flower, but I didn't factor in how this would affect the shading. As a result changing the number of petals alters the shading effect. This should be easily fixed however if I feel like working on the script some more.

As ever, the code is posted on the crunchbanglinux forum here.

Thursday, February 25, 2010

Text Blur and shadow.



Here is a simple script to add a blur like effect to text and numbers.
It can also be used to make shadows for text.

Here is a comparison of the blur effect in use and text without the effect.



The effect works by taking multiple instances of the same text, making them mostly transparent and then overlaying them with a slight offset for each instance. In this case I am displaying the text at various points around a very small circle.

Like so:



The blur effect uses a much smaller circle and calculates transparency by the number of instances of the text you want.

You can get the code from my post on the crunchbanglinux forum here.

Wednesday, February 24, 2010



I took my line graph Lua script, tweaked it a bit and produced the above.
The FSY "heartbeat" blip shows the filesystem used percent, then 1/4 of fsys used.

The MEM goes up to current memory use, stays there for two cycles then goes back to zero.

You can get the code here.

Tuesday, February 23, 2010

I was having some trouble with getting the line graph working properly. In fact a few hours were spent getting frustrated with the lua script because it wasn't doing what I wanted it to do!

But then I had a breakthrough and after that it was pretty straightforward :)
This is what the current script is capable of displaying.



In fact the first figure was what I had been after for a while. I wanted my graph to resemble an electroencephalogram.

I'm sure there is more that can be done from this basis so I may well be posting updates in the future.

You can get the code from my post in the crunchbanglinux forum here. You can also watch a short clip of an earlier form of this script in action.

The problem was the transfer of information from the data generating function to the figure drawing function. The problem was that you can only transfer data held in strings. In the data function I was generating a table something like this:

table={}
n=10
for i=1,n do
table[i]=i
end

This would give me a table with 10 entries, the entries being the numbers 1 to 10.
If i did this:

print (table[i])

I would get this output in the terminal:

1
2
3
4
5
6
7
8
9
10

Which has been fine so far, but the table I was using was much more complicated than this and I just couldn't get things working by simply transferring the string "table[i]" to the figure drawing function. What I really needed was to transfer the whole table to the drawing function.

The breakthrough was the use of this command:
string=table.concat(table, ":")

What this does is to take a table and turn it into a string, in this case, each entry in the table will be seperated from the next by a ":" in the string. So our example table from above, print (string) would output the following:
1:2:3:4:5:6:7:8:9:10

I could then take the string, pass it to the drawing function and use the split string function to turn it back into a table. Then I could work from the table to achieve the figure.
In response to a question asked about vertical graphs on the ubuntu conky thread, I thought that the script I had come up with for bar graphs could be easily modified to give vertical bars. And it was pretty easy.

Then I thought about how I could apply a gradient effect along the bar graph, like you can get in a regular conkyrc, and that wasn't too hard either. So now the script can output graphs like this:



The code to do this has been posted on my original crunchbang linux forum post.


Alternate graphs.



The standard graph outputs from conky are rather uninspiring, so thought I would try and put together some alternatives. The above screenshot is displaying cpu usage over time. Usage is displayed as both a negative and positive value at each point, so you get this kind of "waveform".

You can get the code on the crunchbanglinux forum here.

In the scrip there is a setting that can produce 3 types of graph, the up and down as above, only up or only down as below.



Another improvement over the regular graphs is that you can change which direction the graph moves in.

Other configurables include:

  • colors
  • total height
  • bar width
  • spacing
  • gridlines on or off

I am also working on a line graph, but have run into a few problems, and I havnt got it working how I want it yet. But it may not be possible to do what I want either :)
This is what it currently looks like:



I think this graph has a lot of potential however!
See subsequent blog entries for developments.

Monday, February 22, 2010

When to use "for i = do" and a look at drawing with cairo.

I was fiddling with my updated flowers conky, drawing a bumpy circle with cairo to use as the flowers center. Drawing with cairo via a text file isn't the most user friendly thing you can do, but with some experiementation I had worked out a good way to draw a repeating apttern in a cicle using the cairo_curve_to command.

The approach involved defining a set of circles, and then using the mathematics available in Lua to generate plot points.

Like so:
but first I need to set up some variables!
inner=110
outer1=120

outer2=118
outer3=120

The 4 lines above set out the radius of the circles from which I will be calculating my plot points.
varth1=0.53
varth2=0.42
The above 2 lines will be seen in action later.
hori=150
vert=200

These are positional setups.
Now I can do the drawing.

cairo_translate (cr, hori, vert)
cairo_move_to (cr, 0, 0)
I like to use the translate command for positioning figures. The reason for this is that it allows you to assign the figure "local" coordinates of 0,0 so that you can apply rotations to the figures without cauing problems. When using trandlate in a drawing function you always need to translate back at the bottom of the function. In this case I would translate back with:
"cairo_translate (cr, -hori, -vert)

--inner arc from cairo_set_source_rgba (cr, 1, 1, 1, 1);
text_arc=((2*math.pi/24)*(3))

txs1=0+inner*(math.sin(text_arc))
tys1=0-inner*(math.cos(text_arc))

Above I am setting a starting point for our drawing. The first line is calculating radians for points around the circle. In this case I have divided the circle up into 24 pieces and we are getting the radian value for point number 3. Theres no reason I started at 3, thats just how it happened.
I am also using comments to remind me whats what.

--outer arc through bump1
text_arc=((2*math.pi/24)*(3+varth1))
txs2=0+outer1*(math.sin(text_arc))

tys2=0-outer1*(math.cos(text_arc))
This is the "control" point that will determine the extent of the curve.
Take a look here to see how cairo works out its curves (scroll down a little for curves.)

--outer arc to
text_arc=((2*math.pi/24)*(4))

txs3=0+outer2*(math.sin(text_arc))

tys3=0-outer2*(math.cos(text_arc))
cairo_move_to (cr, txs1, tys1)

This is the coodinates of where the curve will finish.

cairo_curve_to (cr, txs1, tys1, txs2, tys2, txs3, tys3)
And above the curve is being drawn. Because I am drawing very simple curves (arcs really but I find the use of curve_to easier and more understandable than arc_to) I am basically getting this effect:



So the above drawing made a curve out, now we will curve back in, from point txs3,tys3 to coordinates txs5,tys5 and that curve will be controlled by point txs4,tys4 as below.

--outer arc through
text_arc=((2*math.pi/24)*(4+varth2))
txs4=0+outer3*(math.sin(text_arc))
tys4=0-outer3*(math.cos(text_arc))
--outer arc to
text_arc=((2*math.pi/24)*(5))
txs5=0+inner*(math.sin(text_arc))
tys5=0-inner*(math.cos(text_arc))
cairo_curve_to (cr, txs3, tys3, txs4, tys4, txs5, tys5)

This gives me one bump like this:



Then originally I did this:
--outer arc through bump2
text_arc=((2*math.pi/24)*(5+varth1))
txs6=0+outer1*(math.sin(text_arc))
tys6=0-outer1*(math.cos(text_arc))
--outer arc to
text_arc=((2*math.pi/24)*(6))
txs7=0+outer2*(math.sin(text_arc))
tys7=0-outer2*(math.cos(text_arc))
cairo_curve_to (cr, txs5, tys5, txs6, tys6, txs7, tys7)
--outer arc through
text_arc=((2*math.pi/24)*(6+varth2))
txs8=0+outer3*(math.sin(text_arc))
tys8=0-outer3*(math.cos(text_arc))
--outer arc to
text_arc=((2*math.pi/24)*(7))
txs9=0+inner*(math.sin(text_arc))
tys9=0-inner*(math.cos(text_arc))
cairo_curve_to (cr, txs7, tys7, txs8, tys8, txs9, tys9)
--outer arc through bump3
text_arc=((2*math.pi/24)*(7+varth1))
txs10=0+outer1*(math.sin(text_arc))
tys10=0-outer1*(math.cos(text_arc))
--outer arc to
text_arc=((2*math.pi/24)*(8))
txs11=0+outer2*(math.sin(text_arc))
tys11=0-outer2*(math.cos(text_arc))
cairo_curve_to (cr, txs9, tys9, txs10, tys10, txs11, tys11)
--outer arc through
text_arc=((2*math.pi/24)*(8+varth2))
txs12=0+outer3*(math.sin(text_arc))
tys12=0-outer3*(math.cos(text_arc))
--outer arc to
text_arc=((2*math.pi/24)*(9))
txs13=0+inner*(math.sin(text_arc))
tys13=0-inner*(math.cos(text_arc))
cairo_curve_to (cr, txs11, tys11, txs12, tys12, txs13, tys13)

etc etc...

Drawing the lines for the curves one by one, out then in, out then in... until I got all the way around, closed the path and filled it in. This was was pretty easy, just copy and paste and edit the numbers. I also pretty much knew that I didn't have to number the strings in order but I did it anyway. For example:
string=1
print (string) --> 1
string=2
print (string) -->2

But I could see that there was an obvious pattern and was a candidate for using the:

"for i = x,y do" command.

So I looked at the first bump I had drawn, assigned i a value of 1 and then edited the values so that the points I was plotting on the circles were expressed via i, point 3 being i+2 and point 4 being i+3. And setting up a string to supply the number of points, substituting the string into the code. What I ended up with is this:

number=12
for i = 1,number do
cairo_set_source_rgba (cr, 1, 1, 1, 1);
--inner arc from
text_arc=((2*math.pi/number)*(i+2))
txs1=0+inner*(math.sin(text_arc))
tys1=0-inner*(math.cos(text_arc))
--outer arc through bump1
text_arc=((2*math.pi/number)*((i+2)+varth1))
txs2=0+outer1*(math.sin(text_arc))
tys2=0-outer1*(math.cos(text_arc))
--outer arc to
text_arc=((2*math.pi/number)*(i+3))
txs3=0+outer2*(math.sin(text_arc))
tys3=0-outer2*(math.cos(text_arc))
cairo_move_to (cr, 0, 0)
cairo_line_to (cr, txs1, tys1)
cairo_curve_to (cr, txs1, tys1, txs2, tys2, txs3, tys3)
--outer arc through
text_arc=((2*math.pi/number)*((i+3)+varth2))
txs4=0+outer3*(math.sin(text_arc))
tys4=0-outer3*(math.cos(text_arc))
--outer arc to
text_arc=((2*math.pi/number)*(i+4))
txs5=0+inner*(math.sin(text_arc))
tys5=0-inner*(math.cos(text_arc))
cairo_curve_to (cr, txs3, tys3, txs4, tys4, txs5, tys5)
end
cairo_close_path (cr)
cairo_fill (cr)

But it wasn't quite so simple. For some reason the shapes of the bumps changed (they were alot more pointy instead of round) and I had to add these lines or the middle of my drawing did not get filled in:
cairo_move_to (cr, 0, 0)
cairo_line_to (cr, txs1, tys1)

But it was quite easy to fiddle with the settings to make the bumps rounded and voila!.

So when you have a repeating pattern, of you want multiple instances of a figure, then it's a good bet that you can cut down the line count by using this technique. You can also affect other things with the "i" term. Achieving a position change from figure to figure you could do something like:

horizontal = 10*i
vertical = 10

in this case you would get a horizontal line of figures, spaced 10 apart.

But you could also do something like:
cairo_set_source_rgba (cr, 1, 1, 1, 0+(i/10)

So if "i" has a range of 1 to 10, the first figure would have an alpha of 0.1 while the tenth figure would have an alpha of 1.

Or you can fit "i" into any number of equations and have it affect any number of variables.

And if you want more figures, just increase the range of "i" without having to add any additional lines of code.

Friday, February 19, 2010



Three Dimensional Bars! Well, I guess to be more accurate they would be called isometric?
I had been thinking about doing this for a while and it seemed like an obvious idea :)

Anyway, I thought this project would be easy, and to begin with it was. Getting a bar on Lua is pretty easy, then all I would have to do is draw a top and a side and get everything moving correctly. This all happened quickly until I started to look at the lines!

Unfortunately I couldn't simply take the shapes I had drawn to form the "faces" with fill, and overlay them using stroke. The corners stuck out when the line width was increased and the different line unison options in cairo were not going to work. So I had to re-do the lines from scratch, and it took far too may attempts and corrections before I finally got it right.

With this script you can specify:
  • face color
  • line color
  • bar width ("depth" is always set to width/2)
  • bar height
  • bar position
  • wire-frame outline on or off
  • and font, font size, font color and font position
You can get the code on the crunchbanglinux forum here.

Thursday, February 18, 2010



More ring meter variations.
I have added the ability to draw:
  • anticlockwise indicator
  • rounded ends to indicator lines
  • rounded end borders
  • and square end borders
You can get the cod on the crunchbanglinux forum here.

Wednesday, February 17, 2010



I have improved my circlewriting function significantly! No more annoying fiddling about to get it right, the function puts everything in the right place first time. I have kept the option to fine tune through variables to take into account the variations in fonts. I find this function works best with a monospace font anyway.

There are 2 function:
"circlewriting" writes text across the top of a circle, on the outside.
"circlewritingdown" writes text along the bottom of a circle on the inside.
Like so:



You can get the code for these functions here on the crunchbanglinux forum.

Several of my previous Lua scripts have used predecessors of this function. For those, just delete the old function, put in the new functions and make sure that when you activate the function you are giving it all the right information. In the code in the link above I have made the way that you feed the function data a little easier:

--text must be in quotes
text=" Circle Writing "
--font name must be in quotes
font="White Rabbit"
fontsize=18
radius=80
positionx=100
positiony=150
colorred=1
colorgreen=1
colorblue=1
coloralpha=1
letterposition=0
letterrotation=0
circlewritingdown(cr, text, font, fontsize, radius, positionx, positiony, colorred, colorgreen, colorblue, coloralpha, letterposition, letterrotation)

Information is entered line by line with well described strings.

You can also use this kind of display for "live" information. Here I am using the addzero100 function so that the numbers are always 3 digits, although because now the "text" string length is read automatically, the display will alter itself with altering length of "text".



Here is the text strings that achieved the above:

cpu=tonumber(conky_parse('${cpu}'))
text=(" CPU " .. (addzero100(cpu)) .. "% ")

mem=tonumber(conky_parse('${memperc}'))
text=(" MEMORY " .. (addzero100(mem)) .. "% ")

and for reminder here is the addzero100 function:

function addzero100(num)
if tonumber(num) < 10 then
return "00" .. num
elseif tonumber(num) <100 then
return "0" .. num
else return num
end
end

I have now included the ability to set a start and finish position for the displayed text so that you can achieve results like this:





I have updated my post on the crunchbanglinux forum here. The code there outputs the above image. There is also a brief discussion regarding overcoming the alignment problems that are inherent in this function.
ANOTHER UPDATE :)




I was looking at the code I had written and the cairo commands to draw the flower shapes clearly had repeating patterns, so I decided to use the old "for i =" trick to make the code smaller. This worked and I managed to cut down the line count quite considerably. Then I realized that having done that I could change the range that i is equal to. The end result is that you can now specify the number of petals for each flower, and the bumpy middle always has 2x the number of petals.





Conky flowers revisited.
Go here to the crunchbang forum for the latest code.

UPDATE - I have updated the code in the post above so that the Lua script has my improved circlewriting function.
UPDATE2 - I wasn't completely happy with the circular middles, they seemed too hard. So I have enabled the option to draw bumpy middles as so:



Some significant changes and some significant improvements.
First the improvements:
  • flower are placeable independently
  • flowers can be rotated in place without anything else being effected
  • flowers can be used with any wallpaper and allow transparency effects and overlap

The third improvement was accomplished by redesigning the flower's color change mechanism. In the original the flower color change "effect" was created by placing a circle that changed size in response to a numerical input behind a flower "cutout" (see my crunchbang post to see what I'm talking about). But with the revisit, each petal moves independently from each other (except they are all fed the same number although it would be possible to have each petal represent a separate data output). Everything is contained within the flower shape.

There are some things I need to work on. The circlewriting function needs work. I think I know how to get it working better, but right now it is far too fiddly, adjusting the "variables" to make everything look right.

Anyway, this is yet another (or an alternative) entry in the conky blog valentine competition.

Monday, February 15, 2010


Here is another Lua script. For the code and to see it in action go here to my post on the crunchbang forum.


require 'cairo'

I have describe the below function in my full screen lua script. This function is here because at first I was just trying to generate numbers arranged around a circle, and I wanted all the numbers to be 3 digits constantly. When I first wrote the function It was to output numbers around a circle. I suppose I need to re-write this script for the sake of clarity :)
function addzero100(num)
if tonumber(num) <>
return "00" .. num

return "0" .. num

else
return num
end

end


The below function is the figure drawing function. This was taken from the circle-writing function I wrote for my valentines flower conky.

function circlewriting(inum, text, ival, font, fsize, radi, horiz, verti, tred, tgreen, tblue, var1, var2)
Above is the function name followed by a long list of strings that the function needs to work.

inum - "length" ie the number of instances that the data will move to. In the above image this number was 10.

text - the actual data that is being represented, in this case cpu usage.

ival - in the below function an array is used to generate the data and in that array we will be using the code line:
for i = 1, tonumber(len_t) do
len_t is the same number as "inum" above. So "i" is all the numbers between 1 and 10.

font - to set the font for the text output
fsize - sets the font size
radi - sets the radius of the circle around which the bars or text will be displayed.
horiz - horizontal, x, position.
verti - vertical, y, position
tred - output red color value
tgreen - output green color value
tblue - oputput blue color value
var1 - variable 1
var2 - variable 2

variables 1 and 2 are used to fine tune the position and orientation of the bars or text around the circle.

deg=360/inum
Above is a quick calculation to work out how many degrees there are between each bar. This will be used for the rotation of each bar.

Now comes the hard math!
text_arc=((2*math.pi/inum)*ival+var1)
txs=0+radi*(math.sin(text_arc))
tys=0-radi*(math.cos(text_arc))
The above 3 lines are equations to work out the positions of each bar around the circle.
As we all know: circumference = 2 x pi x radius
and to calculate coordinates for each point around a circle
x=SIN(degrees x n)*radius
and
y=COS(degrees x n)*radius

Where "n" is the number order of the thing that is being displayed. For example, on a clock face there are the numbers 1 to 12. So to get the x,y position of those numbers, n would be 0 for 12, 1 for 1, 2 for 2 etc.

But Lua only works with radians so the first line is converting degrees to radians.
The next 2 calculate the x and y plot points.

cairo_select_font_face (cr, font, CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_NORMAL);

cairo_set_font_size (cr, 22);
cairo_set_source_rgba (cr, tred, tgreen, tblue, ival/10);
The above lines are setting the font, font size and color.

Below are the lines that generate the bars. I worked out how to apply the rotation to the bars "locally" and position them with the translate command.

First the position is translated into position.
cairo_translate (cr, txs+horiz, tys+verti)
Then the bar is rotated below. You might wonder why I am taking the "deg" string, set earlier, and performing calculations on it to convert degrees into radians, when I have already done that 8 lines earlier when I was calculating the x,y plot points of my bars. The answer is simple... when I first wrote the function for the flowers.lua my math skills re circles were extremely rusty, so instead of trying to work out the x,y equations for myself, I borrowed then from dissecting londonali1010's air clock lua script (found here) without really understanding the math but knowing it worked. So when I implemented the rotation feature I wrote the calculations seperately. A case of historical artifacts. Just another thing to clean up in this script :)
cairo_rotate (cr, (deg*(ival+var2)*(math.pi/180)))
Then the rectangle is drawn at "local" position 0,0.
cairo_rectangle (cr, 0, 0, 20, (text*-1))
cairo_fill (cr)

Then the rotation is reset.
cairo_rotate (cr, ((deg*(ival+var2)*(math.pi/180)*-1)))
And finally the translation point is reset. Remember that all cairo transformations are cumulative so if these reset steps were not done then the next bar would be translated and rotated relative to the first bars position and rotation.
cairo_translate (cr, -1*(txs+horiz), -1*(tys+verti))

Here are the lines that generate the text output. I decided to comment them out as I liked the lookof the bars better. But they can be reinstated easily
--you can reinstate these lines to get a text output around the circle
--cairo_move_to (cr, txs+horiz, tys+verti);
--cairo_rotate (cr, (deg*(ival+var2)*(math.pi/180)))
--cairo_show_text (cr, (addzero100(text)))
--cairo_rotate (cr, ((deg*(ival+var2)*(math.pi/180)*-1)))
end
end
Thats the end of the figure drawing function.
Next comes the data generating function.

function conky_draw_graph()
Above is the name of the function, this is the function we will call in conky.
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)
Above is the "canvas" setup just set out a little differently from how I have done it before.

local updates=tonumber(conky_parse('${updates}'))
if updates==1 then
len_t=10
t1={}
end
The above lines are particularly important. Here we are initializing the variable. Basically we are telling Lua that the string called "t1" is a table. We are also setting the "length" of the figure with len_t. It took me a little figuring out to understand why these lines had to be here and why they were set only when conky update number = 1.
The reason is that if the line "t1={}" were moved below with the rest of the function (which has to start when conky updates >3 to prevent a segmentation fault when using the conky object ${cpu}) then with each cycle, the line:
"if updates> 3 then"
would be true and everything below that line would be performed and the data stored table "t" would be reset each time. We are relying on the data in the table to "remember" the past cpu usage numbers so t1={} can only be set once.
if updates> 3 then
Below is the array that will generate the data and store it in the table.
And here is the all important "i"
for i = 1, tonumber(len_t) do
Here we are saying that "i" is equal to every number from 1 to len_t (set above). So to think about this array more simply we will say that "i"=1
if t1[i+1]==nil then t1[i+1]=0 end
The above line is a error checking line to stop the "tried to call a nil value" error that can cause the script to stop functioning.
t1[i]=t1[i+1]
So when "i"=1 then
t1[1]=t1[2].
As we have seen before, numbers, or strings in square brackets relate to an entry in a table, in this case table "t1". This doesnt make much sense until we get to the next couple of lines:
if i==len_t then
t1[len_t]=tonumber(conky_parse('${cpu}'))
end
So when "i" = the upper limit of its range, len_t (which in this case is 10) then t1[10]=current cpu usage. Now everything else is set because
t1[9]=t1[10]
t1[8]=t1[9]
t1[7]=t1[8]
to
t1[1]=t1[2]

Now you might think that if everything equals everything else then why isn't t1[1] to t1[10] all the same number? What you need to think about here is how the Lua script is working with conky. Say you have your conkyrc set to update every second, then every second the Lua script is run from the top, so actually the example I gave above about how the table is set up is the wrong way round.

Because the script lines are performed in order from top to bottom here is what is happening: (or this is how it makes sense to me)

on the first pass at time 1:
t1[10]=cpu usage at time 1

then on the next pass at time 2
t1[9]=t1[10] *but* this is the t1[10] from the previous pass where t1[10]=cpu at time 1
because t1[9] is set every so slightly before t1[10] is reset to equal cpu usage at time 2

and so on it goes so that
t1[10] = current cpu usage at time t
t1[9] = cpu usage at t-1
t1[8] = cpu usage at t-2
t1[7] = cpu usage at t-3
etc...

--circlewriting (inum, text, ival, font, fsize, radi, horiz, verti, tred, tgreen, tblue, var1, var2)
Above I am reminding myself of all the variable i need to send the circlewriting function and below I have called the function with all of the relevant info.
circlewriting(len_t, t1[i], i, "Mono", 22, 40, 200, 200, 0, 0, 0, -0.25, 0)
Below we are ending the part of the function that began "if updates> 3 then" It is important to put the call to the figure writing function above this end.
end
Below we are freeing some memory, althoug in action I havnt noticed any difference from when I use these lines to when I do not use these lines.
cairo_destroy(cr)
cairo_surface_destroy(cs)
end
And then we close the function.
end
I will probably revisit this explanation again soon to go over some parts more thoroughly.

Sunday, February 14, 2010

This approach has the advantage of being much more compact and using less functions. I will be feeding the variables straight into the drawing function. The lua is to create bars and circle charts like so.



You can get the whole code here on the crunchbang forum.

Here is the code lines in question:
The first line of the function that draws the bars is:
function draw_bar(co, width, height, across, down, bgr, bgg, bgb, bga, inr, ing, inb, ina, lw, lr, lg, lb, la, rotate)
you can see the name of the function followed by a long list of strings. These are all the strings that will be set later when the function is called. Here is a description of each string:

co = conky object data to display
width = how wide the rectangle will be
height = how tall the rectangle will be
across = x position
down = y position
bgr = red component of background color
bgg =green component of background color
bgb = blue component of background color
bga = alpha component of background color
inr = red component of indicator color
ing = green component of indicator color
inb = blue component of indicator color
ina = alpha component of indicator color
lw = width of the boundary line
lr = red component of line color
lg = green component of line color
lb = blue component of line color
la = alpha component of line color
rotate = rotation in degrees that you want applied to the bar

pretty straightforward variables, and I tried to make it so that you could more or less work out what the string names meant. Now here is the line when the function is called:
draw_bar(cpu, 150, 20, 75, 50, 0, 1, 0, 0.5, 0, 1, 0, 1, 10, 1, 0, 0, 0.5, 0)
Each value is passed up to the draw_bar function.
So that:
co=cpu
width=150
length=20
across=75
down=50
...etc etc.

Then you can call the same function again but feed it a different set of information like so:
draw_bar(mem, 150, 40, 250, 30, 0, 0, 1, 0.5, 0, 0, 1, 1, 20, 0, 1, 0, 0.5, 0)

Not as user friendly as the settings table approach but far less code and alot less complicated. Another important aspect to this approach is that it is more like a "tool" that can be used in a Lua script that contains multiple elements. All you need is the figure drawing function above the function that you will be calling in conky, then if you want a bar or circle meter in your setup, call the function with a single line setup.
I knew it could be done more easily.
Thanks to the coding skills of wlourf, the code to generate the moving bars/moving dots lua (here) is now significantly shorter! Here is wlourf first post on the crunchbang forum.

I knew it was possible and I knew it would involve the technique that wlourf used. I just haven't gotten to grips with the workings of arrays!

And here is a simplified code that generates the moving dot conky as I did before:

require 'cairo'
function dotdraw(cr, num, inum, length, hori, vert, width, height, dotsize, dotr, dotg, dotb, dota)
cairo_set_source_rgba (cr, dotr, dotg, dotb, dota)
modnum=(num*(height/100))
cairo_arc(cr, ((width*inum)+hori), (vert-modnum), dotsize, 0,2*math.pi)
cairo_close_path(cr)
cairo_fill (cr)
end


function conky_draw_graph()
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==1 then
len_t=20
t1={}
end
if updates> 3 then
for i = 1, tonumber(len_t) do
if t1[i+1]==nil then t1[i+1]=0 end
t1[i]=t1[i+1]
if i==len_t then
t1[len_t]=tonumber(conky_parse('${cpu}'))
end
--dotdraw(cr, num, inum, length, hori, vert, width, height, dotsize, dotr, dotg, dotb, dota)
dotdraw(cr, t1[i], i, len_t, 10, 200, 10, 200, 3, 0, 0, 0, 1)
end
cairo_destroy(cr)
cairo_surface_destroy(cs)
end
end

Just a few hundred lines shorter! And using the array you can have a graph as long as you want without increasing the size of the code. All you have to do is change a variable.

Also important, in addition to the use of an array to generate the data, is that the graph drawing function (dotdraw at the top) is separated from the data generating function (conky_draw_graph). This means that the data can be fed easily into as many different kinds of drawing functions as you can think of to represent it. I can think of a number of ways of presenting "graph" like data and now all i have to do is create the drawing function and feed it the data.

In addition this method seemed ideal to apply to creating a text scroll function.
Here is the result:



The code and a video clip are posted on the crunchbang forum here (it is still a work in progress).
I have never liked the native scroll function in conky because it is not continuous. If you have a scroll length of 100 say, then it will scroll your message but then follow it up with 100 empty spaces. Unfortunately this lua scroll has its drawbacks too. Principally that the information is static as it moves across, cpu usage will not change and time will not update, until the strings are generated for the next pass.

However, I think this scroll would be ideal for such things as time and date and to display the titles of music tracks.

I'm certainly going to be seeing what interesting formats I can come up with!

Thursday, February 11, 2010




























These two graphs both display cpu usage over time. The blue graph came first and then I modified the Lua script to generate the dot chart. I had been thinking of tackling an animated bar chart for a while and decided that I should just give it a go. But it wasn't easy!

Just to conceptualize how it was going to work in the beginning took a while...
I had to think of a way to capture cpu usage information at one particular time, store that information and display it somehow while capturing new information every cycle and linking that all together to get the illusion of a moving chart.

I had (with the help of the crunchbanglinux forum) found a way to implement a timer in lua:

local updates=conky_parse('${updates}')
update_num=tonumber(updates)
local timer=(update_num % 4)+1

Parse conky to get the updates number then using the line above, every time the updates number is divisible cleanly by 4 (ie no remainder) then timer =1 (without the +1 it would equal 0)
The result is that the timer counts 1,2,3,4,1,2,3,4,1,2,3,4 etc etc. This was the basis for the animation.

I started with trying a 4 bar graph and after a little figuring out I got to the point where i could get the bars set and moving across, but when the timer reset so did the bars. The problem was that I could pass information down the script by referencing strings:
num1=number
num2=num1
num3=num2
num4=num3
But to get the table working i needed this:
A->B->C->D->a->b->c->d
B->C->D->a->b->c->d->A
C->D->a->b->c->d->A->B
D->a->b->c->d->A->B->C

So that I get a full cycle, so that the next step I can reset back top the beginning. But going from dABC which existed at the bottom of the script back to the top for ABCD, I had to take the information for ABC (from dABC) and move it up the script so that ABC in ABCD is the same ABC from dABC. That makes sense right?

So I needed a function. Functions are how you move information from the bottom of the script to the top!

Anyway. Look here at the code on the crunchbang forum and you will see what I ended up with. The script is pretty long. The reason for this is that, as you can see from the above example, for a 4 step animation you actually need 8 steps, each step containing 4 bits of information. So for 4 steps you need 32 bits of information.

I ended up with a 10 step graph which needed 200 bits of information. If i wanted a 20 step graph then that is 20x2x20=800 bits of info.

If anyone has an easier way then I would be only too glad to hear it!

The other drawback with the graph drawing function, as I have written it, is that you can only feed it one set of information. Unlike other functions where you can feed it say cpu info and settings and also feed it memperc information and settings and get 2 outputs. This function you cant do that...something just doesn't work. It would be easy enough to copy the function, and change its name, then call the newly named function with different information in the conky display function. However, it is also the case that there arent many other conky outputs that are suited to a graph.

Downspeed and upspeed graphs can be generated by conky, and could be generated from my graph drawing script... but extra steps would need to be taken to adjust for the changing units... B to KiB to MiB.

Maybe I'll find a more compact way of doing this... although running this script, total "resting" cpu usage on my system is around the 2's or 3's so it is hardly intensive even for it's size.

Wednesday, February 10, 2010

Hot on the heels of my custom bars lua comes another script for drawing bars and rings. And after figuring out most of what goes into making a settings table work with cairo in a lua script... I decided to go with another approach.

Instead of setting up a settings table at the beginning of the script, then parsing the table with one function and feeding the parsed information into another function which converts the table values into local strings... I went with defining the values when calling the draw function. It maybe isn't as user friendly as the settings table way but in less than half the lines of my custom bars lua I have created a script that can generate the same bars and also generate ring meters.

Also I think the way that I have set up the code will make it easier to inset these meters into another Lua script. All it requires is that the meter drawing functions are placed above the function to be called in conky. Then you can use the functions with just a couple of setup lines.

Of course the ring meters are taken from londonali1010's ring meter script here. Many thanks to londonali1010 for all her excellent and inspirational work!




I have also enabled the addition of boundary rings for the ring meters. The outer and inner boundary rings can be set values for width, color and alpha individually.

The code in posted in the crunchbanglinux forum conky config thread here.
I have just updated the code to allow much easier use of rotation with the bars. Bars rotate in place and not relative to 0,0 in the new code.
Here is the annotated Lua script for my custom bars.



For simplicity I have put in the settings for only 1 bar.

require "cairo"

So first we have the table below.
The table has a format like this:
table={{A1=1,B1=2,C1=3,D1=4},{A2=1,B2=2,C2=3,D2=4},{A3=1,B3=2,C3=3,D3=4}}
but for ease of use and so that the settings can be edited easily the table is split up line by line as follows:
BUT you still have to have all of the brackets (I'm not sure that they have to be curly brackets, but they certainly work.) and all of the commas or you will get an error.
table = {
{
I have used comments throughout the table so that each setting is easily identified. The comments do not interrupt the format of the table.
--conky object for output
co='cpu',
--rectangle width
rw=150,
--rectangle height
rh=50,
--NOTE about width and height. For a bordered rectangle with line width lw (specified below)
--the border is drawn 1/2 lw inside and 1/2 lw outside the boundary of the rectangle, so that
--final width=rw+lw and final height=rh+lw. I did not automate this below as it is possible
--to specify a line width (lw) greater than either width (rw) or height (rh).
--eg if you want a rectangle 200 pixels long and a border line width of 8, then rw=100-8=92
Above is a note about how to set up the heights and widths. If something isn't obvious to anyone who might use the script it's nice to explain things,
--background rectangle color set
bg_red=1,
bg_green=0,
bg_blue=0,
--background rectangle alpha
rab=0.5,
--indicator rectangle color set
in_red=0,
in_green=1,
in_blue=0,
--indicator rectangle alpha
rai=1,
--border line width
lw=40,
--border line color set
l_red=1,
l_green=0,
l_blue=1,
--border line alpha
la=0.2,
--position, x and y
rx=200,
ry=150,
--set rotation in degrees (will require x and y to be reconfigured)
rot=0
},
}
So we have all of the variables set. Each variable will be passed into the various functions and applied to the bars when they are drawn.

There are 2 functions and one sub function in this script. From looking at my last annotated script a function is written and then called below, and then when it is called information is passed up to the function. Here, to see how the information is passed I'll look at the second function first. This is the function that we will be calling in conky:
function conky_draw_shape()
this is the name of the function
local updates=conky_parse('${updates}')
update_num=tonumber(updates)
if update_num > 5 then
if conky_window==nil then return end
local w=conky_window.width
local h=conky_window.height
local cs=cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, w, h)
cr=cairo_create(cs)
The above should be familiar. It sets the 5 cycle delay and the surface onto which cairo will draw. Next comes a local function. This function called "parse" which takes the table entry "co" (the conky object whose output will be displayed by the bar) and performs the conky_parse command on it.
local function parse(cr,pt)
Here is the local function name and the strings that will be set when the function is used below.
local str=''
local value=0
str=string.format('${%s}',pt['co'])
The pt part is the important part (note that there is nothing important about the term pt, it is just the name of a string and could be anything. Perhaps pt stands for parse table). pt is one of the strings that is set by the parse function from information fed to the function when it is called. Also you should remember 'co' as one of the variables we set in the settings table.
str=conky_parse(str)
value=tonumber(str)
The function is parsing 'co' the conky object, running that through the tonumber command and then storing the information in a string named "value"
if value == nil then value = 0 end
draw_table(cr,pt,value)
The line above is calling the first function in the script, and feeding the function 3 things: cr, pt and value. As we saw the "value" string is generated above and it is that string that will be passed to the draw_table function.
end
Here is the end of the local function. Below is the remaining commands of the "global" function called "draw_shape"
for i in pairs(table) do

parse(cr, table[i])
These 2 lines is where the "magic" happens :) These lines read the contents of the table, and pass each set of variables to the parse function. In this case the letter "i" is extremely important and cannot be substituted. Perhaps "i" stands for "instance" or "index", I don't know exactly. So basically what is being done is that for every set of data ie {A1=1,B1=2,C1=3,D1=4} in the settings table the "parse" function is being called and fed the cr string as well as every string that the set contains ( A1, B1, C1, D1).

As you can see the parse function is being sent 2 things cr and table[i]. Then as we saw the parse function (local function parse(cr,pt)) takes cr and stores it as cr and takes the output of table[i] and stores it as pt. Then in the parse function we see pt['co'] which is the same as "table['co']". So the table is being searched for the string co, and the value of co is being formatted (str=string.format('${%s}',pt['co'])) and stored in the string called "str".

So for the data set "table= {A1=1,B1=2,C1=3,D1=4}"
table['A1']=1
table['B1']=2
table['C1']=3
table['D1']=4

and as in the parse function table[i] = pt[i] so that

table['A1']=1=pt['A1']
table['B1']=2=pt['B1']
table['C1']=3=pt['C1']
table['D1']=4=pt['D1']

I hope that all makes sense.
So to recap...
  • function draw_shape is generating the strings: cr and table[i]
  • function parse is being fed cr (but not doing anything with it) and table[i] and renaming table[i] as the string pt.
  • parse is looking through the string "pt" for the table setting 'co' then parsing and formatting the output and storing it as the string "value".
parse is then feeding the strings "cr", "pt" and the new string "value" up to the function settings_table.
end
end
end

Now we are going back to the point after the settings table (confused yet? :) )
Here we have the first function in the Lua script and we have already seen where the strings cr, pt and value are being fed from.
function draw_table(cr, pt, value)
local width=pt['rw']
local height=pt['rh']
local bgalpha=pt['rab']
local indalpha=pt['rai']
local across=pt['rx']
local down=pt['ry']
local lwide=pt['lw']
local lalpha=pt['la']
local bgcolr=pt['bg_red']
local bgcolg=pt['bg_green']
local bgcolb=pt['bg_blue']
local incolr=pt['in_red']
local incolg=pt['in_green']
local incolb=pt['in_blue']
local lcolr=pt['l_red']
local lcolg=pt['l_green']
local lcolb=pt['l_blue']
local rotate=pt['rot']
Above are a number of lines that are searching the table information (stored in the string pt) for the various bits and pieces of information. Then when the specific bit of information is found, that information is being set as a local string. For example:
local width=pt['rw']
string pt is being searched for the table setting "rw" (rectangle width) and storing the value of this setting in the local string "width".
--indicator calculation
local inum=(((width-lwide)/100)*value)
The above calculation allows the bars to be resized while maintainig the correct proportions of the indicator line.
--set initial rotation
cairo_rotate (cr, rotate*math.pi/180)
Here we are applying the local string rotate (initially set in the settings table as "rot") and performing the rotation, if any.
--background bar
cairo_rectangle (cr, (across+(lwide/2)), (down+(lwide/2)), (width-lwide), (height-lwide))
cairo_set_source_rgba (cr, bgcolr, bgcolg, bgcolb, bgalpha);
cairo_fill (cr)
Above we are drawing the background bar using the local strings we set above. The calculations in the code are to account for the way that the outline of rhe rectangle is drawn. This was explained towards the top of the script.
--indicator bar
cairo_rectangle (cr, (across+(lwide/2)), (down+(lwide/2)), inum, (height-lwide))
cairo_set_source_rgba (cr, incolr, incolg, incolb, indalpha);
cairo_fill (cr)
Above we generate the indicator bar.
--border line
cairo_set_line_width (cr, lwide);
cairo_rectangle (cr, across, down, width, height)
cairo_set_source_rgba (cr, lcolr, lcolg, lcolb, lalpha);
cairo_stroke (cr)
And above we draw the outline, if any.
--resets rotation
cairo_rotate (cr, (rotate*-1)*math.pi/180)
Then we reset rotation.
end
Below you can look through the functions that have been described above, but in the correct order.
function conky_draw_shape()
local updates=conky_parse('${updates}')
update_num=tonumber(updates)
if update_num > 5 then
if conky_window==nil then return end
local w=conky_window.width
local h=conky_window.height
local cs=cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, w, h)
cr=cairo_create(cs)
local function parse(cr,pt)
local str=''
local value=0
str=string.format('${%s}',pt['co'])
str=conky_parse(str)
value=tonumber(str)
if value == nil then value = 0 end
draw_table(cr,pt,value)
end
for i in pairs(table) do
parse(cr, table[i])
end
end
end
That's the end.
Here is an approach that achieves the same result as a settings table, in that you can achieve multiple outputs from a single instance of a function. But I think that this next approach is easier to implement and considerable shorter code wise. And here is my first take at explaining the use of arrays in a Lua script.

Tuesday, February 9, 2010

I thought it was time that I tried to get to grips with calling multiple instances of a function via a settings table. I tried it out with something simple at first, and paying close attention to the ring meters script by londonali1010 as a reference here is the end result:



A script that generates indicator bars. While not groundbreaking I thought it might add to the tools of the conky maker :)

They are fully configurable and you can change:
  • which output to display
  • background color and alpha
  • indicator color and alpha
  • border color and alpha
  • size
  • position
  • rotation
via a settings table

Go here for the code. I shall probably be making some modifications in the future.
I shall also post an annotated copy of the lua script and do my best to explain how the whole thing works
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.
I was reading through the crunchbanglinux forum and there was a thread here that began as a question about vector graphics formats but went onto become a discussion of guitar amp schematics. I looked at the linked schematics and I immediately saw the potential to use the scematic, or at least part of it, for the basis of a conky setup!

And here it is...


I recreated the schematic with cairo and then placed the various graphs bars and text outputs around it. I used the conkyrc to generate the upspeed and downspeed graphs and the memory usage bar. Everything else was made in lua. I took alot of the code from my full screen conky setup here. In the screen.lua I had already worked on formatting many of the conky object outputs, and I needed the same approach here so that I could be as faithful to the original schematic as possible.

The lua script is rather long so it's probably best that I link to my crunchbang forum post that contains the code here.

Sunday, February 7, 2010


Here is an annotated copy of my screen.lua

First off, this Lua uses more than 1 function. I can still only call a single function in conky, but in that function (called conky_draw_it below) I can use other functions.

This first 2 functions are simple enough, but show an important principal of using functions. What these functions do is to add zeros to numbers. So that percentages are always 3 digits so that when cpu usage is 5 % then 005 is displayed. Similarly time in hours, for example, is always 2 digits so that 2am is displayed as 02. This is important otherwise the changing number of digits will move everything around or overlap.
function addzero100(num)
The (num) part is important. This is a string that will be set later when the function is used. Here is an example of how this function will be called "cairo_show_text (cr, (addzero100(swap)));" As you can see the place of (num) has been taken by (swap) when the function is used. The information stored in the string "swap" is given to the addzero100 function and is stored as "num". Then the function operates on the information stored in "num" and returns the result in the place of (addzero100(swap)). So if the information is "swap" was 8 (ie 8% of swap was in used) then 008 will be displayed.
if tonumber(num)
return "00" .. num
So if a number can have 3 digits, but has only a 1 digit value, 2 zeros are added.
elseif tonumber(num)
return "0" .. num
Otherwise if it has a 2 digit value 1 zero is added
else
return num
Otherwise if it has a 3 digit value nothing is added. The function below shold be self explanatory. :)
end
end
function addzero10(num)
if tonumber(num)
return "0" .. num
else
return num
end
end

Here is another function. I found this here. It performs a very useful function in that it takes a string and splits it up into bits and puts the bits into a table. I'm a little hazy about how it works, but it works.
function string:split(delimiter)
local result = { }
local from = 1
local delim_from, delim_to = string.find( self, delimiter, from )
while delim_from do
table.insert( result, string.sub( self, from , delim_from-1 ) )
from = delim_to + 1
delim_from, delim_to = string.find( self, delimiter, from )
end
table.insert( result, string.sub( self, from ) )
return result
end
Ok, here we get to the function that contains all the stuff that I want displayed. This function will be using the functions above. Note that you cant use a function until it has been written... meaning that the function you are going to use must be above the function in which you want to use it.
function conky_draw_it()
local updates=conky_parse('${updates}')
update_num=tonumber(updates)
if update_num > 5 then
if conky_window==nil then return end
local w=conky_window.width
local h=conky_window.height
local cs=cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, w, h)
cr=cairo_create(cs)
The above sets the delay and the "canvas".
--sets font, font size and color
cairo_select_font_face (cr, "White Rabbit", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (cr, 200);
cairo_set_source_rgba (cr, 1, 1, 1, 1);
In this case I initially wanted to use the same font, font size and color for all the text so I set that all up above.
--this section generates numbers only uptime ddhhmm
This section takes the output of the conky object ${uptime} which would normally be, for example, "1d 3h 25m 12s" and turns it into "010325" (no seconds)
local uptime=conky_parse("${uptime}")
utdcalc=string.gsub(uptime, "[%d ]", "")
string.gsub was anew Lua command for me. The above takes the string "uptime" finds all numerical digits and spaces and replaces them with nothing... ie it deletes them. This is what is being looked for: "[%d ]". %d means any digit, and when placed in [] it finds digits and spaces. It would be different if the search was for "%d ". Without the [] it is now searching for a digit followed by a space only. The end result is 1d 3h 25m 12s -> dhms. This will be used in an if command below.
utime=string.split(uptime, "%a ")
Above I'm using the string split function as defined earlier in the script. As you can see, I am feeding the split string function 2 things... a string (uptime) and a search object "%a ". %a matches any letter. I am searching for a letter followed by a space. So it takes "1d 3h 25m 12s" and turns it into:
1
3
25
12
so that:
utime[1] = 1 (days)
utime[2] = 3 (hours)
utime[3] = 25 (minutes)
utime[4]= 12 (seconds)
if utdcalc=="hms" then
If uptime is less than 24 hours there will be no days in the output and therefore the following will be set as strings...
utd="00"
uth=addzero10(utime[1])
utm=addzero10(utime[2])
Also here we are using the addzero10 function, so that 3 (hours) -> 03
else
If there is a day component to uptime then "utdcalc" will not be "hms" (it will be dhms), so instead the following strings will be set.
utd=addzero10(utime[1])
uth=addzero10(utime[2])
utm=addzero10(utime[3])
end
Kernel, Down speed and Up speed uses a similar process as used for uptime; a combination of string.gsub and string.split.
--this section prints number only kernel information
local kernel1=conky_parse("${kernel}")
kernel=string.gsub(kernel1, "[%a .-]", "")
--this section formats downspeed
local downspeed=conky_parse("${downspeed}")
downnum=string.gsub(downspeed, "[%a ]", "")
dwnspcalc=string.split(downnum, "%p")
dwnumunit1=string.gsub(downspeed, "[%p%d]", "")
dwnumunit=string.gsub(dwnumunit1, "iB", "")
if dwnumunit=="M" then
dspmb=string.gsub(downnum, ".$", "")
dsnum=(math.random(5,20))/100
Here is a feature of the conky that I implimented later on. math.random(5,20) generates a random number between 5 and 20 every cycle. I used this number divided by 100 in the line below to adjust the aplha of the text cairo displays.
cairo_set_source_rgba (cr, 1, 1, 1, dsnum);
.................................................^ here is the random number string dsnum
cairo_move_to (cr, 784, 800);
cairo_show_text (cr, (dspmb) .. (dwnumunit));
else
dsnum=(math.random(5,20))/100
cairo_set_source_rgba (cr, 1, 1, 1, dsnum);
cairo_move_to (cr, 784, 800);
cairo_show_text (cr, (addzero100(dwnspcalc[1]) .. dwnumunit));
end
--this section formats upspeed
local upspeed=conky_parse("${upspeed}")
upnum=string.gsub(upspeed, "[%a ]", "")
upspcalc=string.split(upnum, "%p")
upumunit1=string.gsub(upspeed, "[%p%d]", "")
upumunit=string.gsub(upumunit1, "iB", "")
Below is the section that formats the name of the top process, as measured by cpu usage so that only the first 4 characters of the name are displayed, as I only had space for 4 characters in the conky. This one gave me some trouble as when you use the string:split function, the search criteria used to delineate the splits is deleted when the string is split. But I discovered a string.gsub trick below that solved the problem.
--this section formats top process
local topproc1=conky_parse("${top name 1}")
topproc=string.gsub(topproc1, "[%p]", "")
First I removed all the punctuation characters. %p matches all punctuation.
top1=string.upper(topproc)
Then I put all the text into uppercase
top2=string.gsub(top1, "%a", "%1-")
Abpve is the gsub trick... it matches all letters (%a) and puts a hyphen (-) between them. So that CONKY becomes C-O-N-K-Y-
top=string.split(top2, "%p")
I was then able to split the string based on punctuation and turn the resulted split into a table.

Time outputs below use Lua os.date rather than conky_parse('${time}')
--this section formats time outputs
--hours
hrs=os.date("%H")
--minutes
min=os.date("%M")
--seconds
sec=os.date("%S")
--year
yr=os.date("%y")
--month
mnt=os.date("%m")
--day
day=os.date("%d")

Below are all the common conky outputs to parse
--this sectiion formats common conky outputs
--cpu
local cpunum=conky_parse("${cpu}")
cpu=tonumber(cpunum)
--mem
local memnum=conky_parse("${memperc}")
mem=tonumber(memnum)
--hdd
local hddnum=conky_parse("${fs_used_perc /}")
hdd=tonumber(hddnum)
--swap
local swapnum=conky_parse("${swapperc}")
swap=tonumber(swapnum)

Below is the section that displays all of the above pieces through cairo.
--prints text
--uptime
utmnum=(math.random(5,20))/100
Above is the random alpha generator in use again.
cairo_set_source_rgba (cr, 1, 1, 1, utmnum);
cairo_move_to (cr, 261, 480);
cairo_show_text (cr, (utd .. uth .. utm))
--kernel
knum=(math.random(5,20))/100
cairo_set_source_rgba (cr, 1, 1, 1, knum);
cairo_move_to (cr, 0, 800);
cairo_show_text (cr, (kernel))
--upspeed
upsdnum=(math.random(5,20))/100
cairo_set_source_rgba (cr, 1, 1, 1, upsdnum);
cairo_move_to (cr, 261, 320);
cairo_show_text (cr, (addzero100(upspcalc[1]) .. upumunit));
--top process
tpnum=(math.random(5,20))/100
cairo_set_source_rgba (cr, 1, 1, 1, tpnum);
cairo_move_to (cr, 0, 640);
cairo_show_text (cr, (top[1]) .. (top[2]) .. (top[3]) .. (top[4]));
Above is how to print the first 4 characters from the name of the top process. In cairo a space followed by 2 periods and then another space, such as above, basically means print next to. So that if the top process is conky, top[1]=C, top[2]=O, top[3]=N and top[4]=K and cairo is told to display this as "CONK"
--time hours
thnum=(math.random(5,20))/100
cairo_set_source_rgba (cr, 1, 1, 1, thnum);
cairo_move_to (cr, 0, 160);
cairo_show_text (cr, (hrs));
--swapperc
swnum=(math.random(5,20))/100
cairo_set_source_rgba (cr, 1, 1, 1, swnum);
cairo_move_to (cr, 261, 160);
cairo_show_text (cr, (addzero100(swap)));
--month
monnum=(math.random(5,20))/100
cairo_set_source_rgba (cr, 1, 1, 1, monnum);
cairo_move_to (cr, 653, 160);
cairo_show_text (cr, (mnt));
--cpu
cnum=(math.random(5,20))/100
cairo_set_source_rgba (cr, 1, 1, 1, cnum);
cairo_move_to (cr, 915, 160);
cairo_show_text (cr, (addzero100(cpu)));
--time minutes
cairo_set_source_rgba (cr, 1, 1, 1, thnum);
cairo_move_to (cr, 0, 320);
cairo_show_text (cr, (min));
--day
cairo_set_source_rgba (cr, 1, 1, 1, monnum);
cairo_move_to (cr, 784, 320);
cairo_show_text (cr, (day));
--year 20
yrnum=(math.random(5,20))/100
cairo_set_source_rgba (cr, 1, 1, 1, yrnum);
cairo_move_to (cr, 1043, 320);
cairo_show_text (cr, "20");
--time seconds
cairo_set_source_rgba (cr, 1, 1, 1, thnum);
cairo_move_to (cr, 0, 480);
cairo_show_text (cr, (sec));
--year 10
cairo_set_source_rgba (cr, 1, 1, 1, yrnum);
cairo_move_to (cr, 1043, 480);
cairo_show_text (cr, (yr));
--hdd used
hdnum=(math.random(5,20))/100
cairo_set_source_rgba (cr, 1, 1, 1, hdnum);
cairo_move_to (cr, 522, 640);
cairo_show_text (cr, (addzero100(hdd)));
--memory
mnum=(math.random(5,20))/100
cairo_set_source_rgba (cr, 1, 1, 1, mnum);
cairo_move_to (cr, 915, 640);
cairo_show_text (cr, (addzero100(mem)));
ebd
end

And that is that Lua!
For more information you can go here and look at another annotated script that deals with the use of a settings table and uses multiple functions.