Some of my scripts for daily use on a linux desktop

Showing posts with label scripts. Show all posts
Showing posts with label scripts. Show all posts

Friday, February 18, 2011

HowTo run multiple Lua scripts in a Conky

Hello,

Here is a little HOWTO for running more than one Lua script at the same time.


1. Method for two scripts with Conky objects

This can be done for two scripts with the two functions of Conky : lua_draw_hook_pre and lua_draw_hook_post


lua_load /home/wlourf/lua/scriptA.lua
lua_draw_hook_pre functionA

lua_load /home/wlourf/lua/scriptB.lua
lua_draw_hook_post functionB


lua_draw_hook_pre is called before Conky parse the TEXT section and lua_draw_hook_post is called after the Conky parse the TEXT section. So effects are not very nice because the two calls are not synchronized.




2. Method with a Lua script for 2 or more scripts

Now, here is an example for 2 scripts, but you can modify it for three, four ... scripts :
Save the two scripts (scriptA.lua with conky_functionA() and scriptB.lua with conky_functionB() in the same folder : /home/wlourf/multi/ for my example :
Create a third script loadAll.lua, and paste that :

package.path = "/home/wlourf/multi/?.lua"
require 'scriptA' --for scriptA.lua ".lua" is not required here
require 'scriptB'

function conky_main()
conky_functionA()
conky_functionB()
end


and in the conkyrc, call the conky_main function with lua_draw_hook_pre or lua_draw_hook_post, like this :

lua_draw_hook_post main



3. With some variables

Now, sometimes, we need to use the same parameters in more than one script (colours, x and y position ...). So we have to write them in the conky_main() function so they can be used in the functions conky_functionA() and conky_functionB()

To avoid memory leaks, we have to declare the variables in local, so these variables will be available only for the conky_main() function, then we have to "pass" them to the functions, like that :

function conky_main()
local varX = 25
local varY = 50
conky_functionA(varX,varY)
conky_functionB(varX,varY)
end



and in the functions :
function conky_functionA(varX,varY)
table_config ={...,
x=varX,
y=varY,..
}
end



Ok, passing functions from the conky_main() function to other functions can be boring when we have a lot of parameters. The easiest way to pass the parameters is to group them into a table and to pass the table :
function conky_main()
local table_param={
varX = 25,
varY = 50
}
conky_functionA(table_param)
conky_functionB(table_param)
end


and in the functions :
function conky_functionA(t)
table_config ={...,
x=t.varX,
y=t.varY, ...
}
end


t.varX is the variable varX from the table t, it can be written t.["varX"]

That's all. I hope this little HowTo will help you to use multiple Lua scripts. This is my own way to manage multiple scripts, it can be done with others methods dofile, loadfile or instances !

Happy Conkying ;-)

Wednesday, March 31, 2010

Automatic logout for kids


I have a little problem with my little kid : when he plays on computer, I have to tell him at least 10 times to stop when his time is up !
So I wrote this conky (to use in Gnome) that will display remaining time and logout the session !
And after that, if he tries to login again, the session will run only 30 seconds ;-)

Of course, it's for a small kid who doesn't know commands like "killall conky" or tricks like that !

First, create a hidden folder in the kid session :
mkdir ~/.logout/

and create the next 3 files in the folder:
the conkyrc-logout

double_buffer yes
no_buffers yes

#own_window yes
own_window_type desktop
own_window_transparent yes
own_window_hints undecorate,sticky,skip_taskbar,skip_pager,below

border_inner_margin 0
border_outer_margin 0

# X font when Xft is disabled, you can pick one with program xfontsel
font 7x13

# Use Xft?
use_xft yes


# Xft font when Xft is enabled
xftfont monospace-18

# Text alpha when using Xft
xftalpha 0.5

# Draw shades?
draw_shades yes

# Stippled borders?
stippled_borders 0

# Default colors and also border colors
default_color yellow
default_shade_color blue

alignment br
gap_x 1
gap_y 1

# Add spaces to keep things from moving about? This only affects certain objects.
use_spacer right

# Subtract file system buffers from used memory?
no_buffers yes

TEXT
Stop in ${exec ~/.logout/time_left.sh}



The script time_left.sh which send remaining time to the conky :

#! /bin/bash

#st in seconds
st=$(~/.logout/read_time.sh)

RUNTIME=$((st))
SECOND=$((RUNTIME%60))
MINUTE=$((RUNTIME/60%60))
HOUR=$((RUNTIME/3600))

if [ "$SECOND" -lt 10 ]; then
SECOND="0$SECOND"
fi
if [ "$MINUTE" -lt 10 ]; then
MINUTE="0$MINUTE"
fi
if [ "$HOUR" -lt 10 ]; then
HOUR="0$HOUR"
fi

echo $HOUR":"$MINUTE":"$SECOND
if [ "$RUNTIME" -lt 0 ]; then
gnome-session-save --force-logout
fi




The script read_time.sh which read remaining time and save it in a file .counter.txt
The default time for a day is one hour (3600 secondes) on line 4.


#! /bin/bash

log=~/.logout/.counter.txt
duration=3600

if [ ! -f $log ]; then
echo "no file"
echo $(date +%Y%m%d)";"$duration > $log
fi

today=$(date +%Y%m%d)

reste=-1
line=`tail $log`

xday=$(echo $line | cut -d";" -f1)
if [ $xday -eq $today ]; then
reste=$(echo $line | cut -d";" -f2)
hasdate=true
if [ $reste -lt 0 ]; then
reste=30
fi
reste=$((reste-1))
else
reste=$duree
fi
echo $today";"$reste > $log

echo $reste



To run it at startup, add this line in gnome startup:
conky -c ~/.logout/conkyrc-logout

Voilà ! I'm sure there is other to do that, but I like the display of the remaining time...
Make sure the paths are OK for in the three files and don't forget to make the scripts executables


The script in french here.

Friday, March 19, 2010

Smiley Widget



Here is a new widget which can give at least four informations if you can read it, which is not really easy at first glance.
Each eye and the smile can be linked to a conky variable or not and color can change.
The code for the script can be downloaded on ubuntu forum or on CH forum (or in french here).

Parameters are explained in the Lua script, but there is a short example :

draw_smiley(200,175,120,4,0xFFFF00,false,true,false,
  "downspeedf", "wlan0",1000, false,
  "upspeedf", "wlan0",100, false,
  "wireless_link_qual_perc","wlan0",100,false,
  "acpitemp","",80,
  -0.5)

And the function with named arguments
function draw_smiley(xc,yc,radius1,shadow_size,color0,compo_r,compo_g,compo_b,
  le_name,le_arg,le_max,le_inv,
  re_name,re_arg,re_max,re_inv,
  mouth_name,mouth_arg,mouth_max,mouth_inv,
  color_name,color_arg,color_max,
  rotation)


From last to first :
-last line :rotation is the angle in radian of rotation of the smiley
-lines 2 to 5 are where we choose values to display. On line 2, it will display the download speed of wlan0, the value need to be in percent so we have to set the max value (1000 in my case). The last parameters will inverse the value if needed, ie. if value is 80%, it will draw a 20% value.
- first line : xc, yc, radius1 are for positionning the smiley in the conky window
- first line : shadow_size will draw a shadow ;-)
- first line : color0, compo_r, compo_g, compo_b are parameters for having color changing, for the example above:
0xFFFF00,false,true,false,
with
"acpitemp","",80,
The color will change between 0 and 80, at zero, color will be 0xFFFF00 (yellow) and when $acpitemp will increase, green composante will be affected, so when 80°C is reached, the color will be 0xFF0000 (red).

Last warnings :
Fixed values can be used instead of conky variables with name="" and arg=value (80% in this case): "",80,100,false,

If you call more than one widget : xc, yc and rotation are RELATIVES to previous widget.

Voilà!

Monday, March 1, 2010

Bleeding heart ...



I made this amazing heart with a Lua script. The verticals lines are linked to some conky variables (cpu, download speed ...).
It's up to the user to choose the shape he wants to draw, for example a breathing heart :


Well, I am not specially interesting by hearts but I tried to adhere to the theme of the conky contest of this month : Saint Valentine's Day/Massacre. So, if you like this script, you can vote for it on the conky blog here.

A last video made with the same script :


All the stuff can be downloaded from the ubuntu forum : here. There is also an HOWTO if you want to draw your own shapes!

I think, I will stop with this moving stuff for a while !

Happy Conkying ! And if someone can explain me the difference between "bleeding" and "bloody" ...

Tuesday, February 23, 2010

Links Index !

This page is the reference points for all the little scripts for Conky I posted around.
It will make life easier for people using theses scripts when they're looking for latest versions.

Recents updated scripts are on top!
First link is the link to the download page, second link is the link to the presentation page in this blog