Sunday, February 7, 2010

Getting started with Lua and Cairo.

I have no programming background, and hadn't written a single script before a couple of months ago when I installed crunchbang linux on my computer. I began writing simple shell script in bash to get some things working in conky (using the exec and execi conky functions). Mostly my scripts were just if x=y then z elif etc etc etc. I also learnt how to use sed grep and awk functions which proved very useful for a number of aplications such as here. This conky takes the html code from a page in the crunchbanglinux site and extracts the information, displaying the active topics on the forum in conky.

Calling scripts in conky with execpi is fine, except that if you need to call the script every second or so, cpu usage gets too high to justify.

So the solution is Lua scripts and Cairo. All the functions of conky plus the ability to perform calculations and complex functions in the same place while minimising cpu usage.

But, it was certainly a daunting prospect and at first, looking at some of the Lua scripts I had come across, it seemed like too big of an undertaking. But then I just decided that I would have have a go anyway!

So I turned to google with such inquiries as "How to get started with lua cairo".
One of the first pages I came to was this.

Th advice by londonali1010 on that page was very helpful. In particular this:
"The easiest way to start is to modify someone else's work, just to get used to the syntax. Just find a script you like and start tweaking. Maybe change colours or shapes, placement of objects, or combine elements from different scripts."

I took this advice and downloaded the rings lua script. And I looked through it. In conjunction to this I read through the Cairo tutorial.

I managed to decipher some of the Lua script and applied the parts I understood to a script myself. This is what I came up with...

and heres the script below: for simplicity I have only included 1 bar.

require 'cairo'
function conky_draw_lines()
local updates=conky_parse('${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)
cairo_select_font_face (cr, "White Rabbit", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size (cr, 12.0);

--Line 1 settings
local a_arg=conky_parse('${cpu}')
local a_thick=10
local a_red=1
local a_green=0
local a_blue=0
local a_alpha=1
local a_start=10
--line 1 settings end
local a_num=tonumber(a_arg)
--line 1 background
cairo_set_source_rgba (cr, a_red, a_green, a_blue, 0.5);
cairo_set_line_width (cr, a_thick);
cairo_move_to (cr, a_start, a_start);
cairo_rel_line_to (cr, 100, 100);
cairo_stroke (cr);
--line 1 indicator
cairo_set_source_rgba (cr, a_red, a_green, a_blue, a_alpha);
cairo_set_line_width (cr, a_thick);
cairo_move_to (cr, a_start, a_thick);
cairo_rel_line_to (cr, a_num, a_num);
cairo_stroke (cr);
--line 1 title
cairo_set_source_rgba (cr, a_red, a_green, a_blue, a_alpha);
cairo_move_to (cr, a_start+a_thick, a_thick);
cairo_rotate (cr, 45*math.pi/180)
cairo_show_text (cr, "CPU");
cairo_rotate (cr, -45*math.pi/180)

My first Lua Cairo thing. Go here and I'll talk you through it line by line.

No comments:

Post a Comment