Sunday, February 14, 2010

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!

1 comment:

  1. you're welcome! I use Lua for two months now so I discover things just before you !

    ReplyDelete