Some of my scripts for daily use on a linux desktop

Tuesday, April 27, 2010

Radio Box Widget

I like to listen to radio when I am working on my computer. Usually, I listen to FIP radio, the track information played by this radio are very cools and smooths (example for one day : here) but of course I don't use their ugly player or their information popup ...

Guess what, I want to display the tracks I'm listening on my desktop in a conky ;-)
But as there are more radios than "FIP radio" and because the script was not difficult to write, the conky came more "universal" (sorry, a big word for a little thing).
You need conky 1.8.0 because the script use cairo_text_extents_t structure for text alignment.

The script can be downloaded from ubuntu forums : here.

First, I said I don't want a ugly player, so I use vlc with the http interface, like this :

vlc --intf=http

there are others interfaces or others methods to hide a player but I like vlc!

I can control volume with my keyboard or with my browser at this address :

The conky itself doesn't get the track information. This is done by a bash script, (I made one for FIP here but for others radios, you have to work a little bit ...) which write the information in a text file.
So, the conky needs only this text file in input with at least one of the lines bellow (order doesn't matter):

radio:__FIP radio__
artist:Sonny Rollins
title:Come, Gone
album:Way out West

I will call this file : /tmp/radio.txt

In the conkyrc, as usual, call the Lua script :

lua_load ~/scripts/radio-box.lua
lua_draw_hook_post main_radio_box /tmp/radio.txt
${execi 15 ~/scripts/}

You can see that the main_radio_box function needs one parameter : the track information file generated by the
In fact, the function can have 17 parameters but only the first one is mandatory.

If you run the script with only the first parameter, you get that :

useless, isn't it ?

Now, let's add some parameters on the main function :
The x, y coordinates of the top-left corner, to move the box :
lua_draw_hook_post main_radio_box /tmp/radio.txt 100 25

The font and the font size, to change the display :
lua_draw_hook_post main_radio_box /tmp/radio.txt 10 10 Purisa 15

The informations to display, in the form ATBLYR where :

Example :
lua_draw_hook_post main_radio_box /tmp/radio.txt 10 10 Japan 15 RATBLY

Here you can see that the box auto-fit the text, both in height and width.

The cover.
If your script get the cover, you can display it, just set the path in the file /tmp/radio.txt. To display, the cover use 1, other caracter to not display.
lua_draw_hook_post main_radio_box /tmp/radio.txt 10 10 Japan 15 RATBLY 1

If there is less text to dispaly, the cover will resize to the height of the text frame, like this
lua_draw_hook_post main_radio_box /tmp/radio.txt 10 10 Japan 15 ATB 1

The border size :
(In a reel conky, you should write all the parameters on one single line)
lua_draw_hook_post main_radio_box /tmp/radio.txt 10 10 Japan 15 ATBY
1 7

The radius of the corners:
lua_draw_hook_post main_radio_box /tmp/radio.txt 10 10 Japan 15 ATBY
1 3 15

lua_draw_hook_post main_radio_box /tmp/radio.txt 10 10 Japan 15 ATB
1 0 0

Bye, Sonny...

The gap between the two frames:
lua_draw_hook_post main_radio_box /tmp/radio.txt 10 10 Japan 15 ATB
1 3 3 60

The light effect source, possibles values are :
nw : Nord-West (default)
nn : North-North
ne : North-East
ee : East-East
se : South-East
ss : South-South
sw : South-West
ww : West-West

Example for North-North :
lua_draw_hook_post main_radio_box /tmp/radio.txt 10 10 Japan 15 ATB
1 3 3 10 nn

The colors of the frame (it's a linear gradient but if the two values are the same, there is no more gradient):
lua_draw_hook_post main_radio_box /tmp/radio.txt 10 10 Japan 15 ATB
1 3 3 10 ee
FF0000 FFFF00 1 1

The colors of the text :
lua_draw_hook_post main_radio_box /tmp/radio.txt 10 10 Japan 15
ATB 1 3 3 10 ee
F00F00 FCFFC0 1 1 00FF00 00FFFF 1 1

The colors of the border (the same of the main frame if they are not defined, but inversed)
lua_draw_hook_post main_radio_box /tmp/radio.txt 10 10 Japan 15 ATB
1 5 5 10 sw
CCCCCC 000000 1 1 FFFF00 FFFFFF 1 1 000000 DDDDDD 1 1

More classical :
lua_draw_hook_post main_radio_box /tmp/radio.txt 10 10 Japan 15 ATB
1 2 0 10 ww
CCCCCC CCCCCC 1 1 000000 000000 1 1 000000 000000 1 1

While writing this note I add parameters for transparancy:
lua_draw_hook_post main_radio_box /tmp/radio.txt 10 10 Japan 15 ATBLY
1 5 5 10 nw
000000 FFFFFF 0.3 0.3 FF0000 FF0000 0.5 0.5 000000 000000 1 0.2

I think I will add some effects on the cover if I can in the future.

Friday, April 9, 2010

Pie Chart & Ring Widget

Edit: v1.1 15/05/2010, widget can now draw a ring, see at bottom.

Here is another widget for Conky... it's a pie-chart fully customizable. For example, for the circle in the above picture : it's a file system (yes it is!), each sector is proportional to the size of a disk, and two colors are used to draw a sector (one for used space, and one for free space).

It can be downloaded from Ubuntu forums here.

You need Conky 1.8.0 to use it.
To call it in a Lua script, you have to set parameters in a table:
pie_settings= { table1, table2 ...}

Each table has is own parameters, only one is mandatory, it's tableV, a table containing the values to display.

Example of tableV, of course, in real life, table is created by the script :
 -- table of labels and values 
{{label1,conky_variable,conky_argument,convert to Go-Mo-Ko units (true/false) or unit}, ...}
-- all the values are at maximum values in this example
-- last parameter set to true means that the script
will convert the values in Go, Mo ...
local k=1024*1024*1024
tableV={{"/","", k*50, k*50,true},
{"/home","", k*500, k*500,true},
{"/data","", k*1000, k*1000,true},
{"stuff","", k*10, k*10,true}

The Lua file contains a script which use the output of df command, because this widget was made with the disks spaces in mind, but it can be use with any other value, like this (but it's not the purpose of the script, so we won't use it):
 tableV={{"cpu0","cpu", "cpu0",100,"%"},
{"cpu1","cpu", "cpu1",100,"%"},
{"fan","", "${exec sensors | grep 'CPU Fan' | cut -c13-16}", 2000," rpm"},
{"sys","", "${exec sensors | grep 'Sys Temp' | cut -c15-17}", 100,"°C"}

Examples of tablebg and tablefg (background and foreground colors):

With the above parameters and these ones :

we obtain this simple kind of chart

If the number of values in a colors table is less than the number of sectors, like that :

colors restart at the end of the table :

Now, if we change the second parameter of tableV, the value for each sector, we change the way the graph is displayed.
 local k=1024*1024*1024
tableV={{"/", .25*k*50, k*50,true},
{"/home", .50*k*500, k*500,true},
{"/data", .75*k*1000, k*1000,true},
{"stuff", .90*k*10, k*10,true}

This is what I wanted to display on my desktop : the amount of used space on my external or internal drives and the proportion of each drive in the full file system. Like the big arc on first picture, I have to clean some disks ...

Now, have a look to more interestings parameters ;-)
With int_radius=50, (internal radius)

For the angles, (O° is North and angles go clockwise),

Back to full circle and proportional parameter:

Back to proportional=true and adding some gradient and hiding text

By the way, here is an interesting script to toggle the text (or others flags) when the conky is running.
In the Lua script :
 local file ="/tmp/flag-conky","r")
show_text=(file == nil)

And a bash script associated with a keyboard shortcut:
if [ -f $flag ]; then
rm $flag
echo > $flag

The lines options

line_space can be usefull when labels are too close:
--for the second arc (yellow and blue labels are separated)

Last parameter for lines is extend_line which will extend the horizontal line if the text is too long, with extend_line=true on the second arc :

The text options
txt_font and font_size are obvious.
font_color and font_alpha create a linear gradient. To keep text color the same color of the line, set font_color=nil
txt_offset is the space between the line and the text
nb_decimals is the number of decimals for the numbers ;-)

The last parameter txt_format is the string for formating texts. Some tags can be used :

-- &l for label (the first value of tableV)
-- &v for value (the second value of tableV)
-- &m for max value (the third value of tableV)
-- &o for occupied percentage (ie tableV[2]/tableV[3])
-- &f for free percentage (ie tableV[3] - tableV[2]/tableV[3])
-- &n for free value (non-occupied) (ie tableV[3] - tableV[2])

 txt_format="&l &m, free &f",

That's all ;-)

Edit : a last example (code is here ):

Edit for version 1.1, I added the parameter type_arc. If type arc is "r" (the default value) values are displayed on a radial way as before :

but if type_arc="l" (l for linear) then values are dispayed like rings :

Wednesday, April 7, 2010

Square to Rounded image with Lua/imlib

I wanted to display Moon and Earth pictures on my conky but the only images I found where squares images with a black background and the Moon or the Earth in the middle.
So to display the image correctly in the conky, I had to set transparency of the background of the original image. Well, I've done that with imlib and post the resulting script here (or in french here).

I also post the script to get Earth or Moon images from this site :

Of course the images are updated every hour so the night area is reflecting the reality !
And of course again, there are a few parameters to set the latitude, longitude and so on ...

If you are inspired, you can have some amazing conkys like this one :

or that:

I also updated the clock and the calendar wheel (this one is now a widget and it is more easy to use it).
And I discover that the widgets can be called with the dofile() function in the Lua script. No more need to copy/paste the widget but to use a line like this :