Skip to content

Using Lua scripts (Part 09): Function parameters

lasers edited this page Jan 4, 2019 · 4 revisions

ix: Function parameters

I left off after describing how to send indicator bar setup information from the main conky (conky_main) function to a separate function called indicator_bar which takes the information sent to it and draws a bar indicator based on those settings

we did this in conky_main

indicator_bar(100,100,30,100,tonumber(conky_parse("${cpu}")),100,0.5,0.5,0.5,1,1,0,1,1,1,10,50,0,1,0,1,1,1,0,1,80,1,0,0,1)

putting all our settings right into the function call

while this way works just fine there are some drawbacks to doing it this way

first of all its just hard to keep track of what each number means and second its easy to miss something out (or miss out a comma which are necessary to separate values) as I described earlier a missed entry (or comma) when sending information this way will lead to your script giving an error

i prefer to define my strings, and then duplicate each string name in the function call for example using the multiply function it would look like this

number=16
result=multiply(number)

now we dont have to touch our function call change the number we are sending to the function, we only need change the value we are assigning to the string called "number"

so for our indicator bar setup we would just take the settings lines we already have, and add the call at the end

--SETTINGS FOR INDICATOR BAR
bar_bottom_left_x= 100
bar_bottom_left_y= 100
bar_width= 30
bar_height= 100
bar_value=tonumber(conky_parse("${cpu}"))
bar_max_value=100
--set bar background colors, 0.5,0.5,0.5,1 = fully opaque grey
bar_bg_red=0.5
bar_bg_green=0.5
bar_bg_blue=0.5
bar_bg_alpha=1
--bar border settings
bar_border=1 --set 1 for border or 0 for no border
--set border color rgba
border_red=0
border_green=1
border_blue=1
border_alpha=1
--set border thickness
border_width=10
--color change
--set value for first color change, low cpu usage to mid cpu usage
mid_value=50
--set "low" cpu usage color and alpha, ie bar color below 50% - 0,1,0,1=fully opaque green
lr,lg,lb,la=0,1,0,1
--set "mid" cpu usage color, between 50 and 79 - 1,1,0,1=fully opaque yellow
mr,mg,mb,ma=1,1,0,1
--set alarm value, this is the value at which bar color will change
alarm_value=80
--set alarm bar color, 1,0,0,1 = red fully opaque
ar,ag,ab,aa=1,0,0,1
--end of settings, call drawing function
[b]indicator_bar(bar_bottom_left_x,bar_bottom_left_y,bar_width,bar_height,bar_value,bar_max_value,bar_bg_red,bar_bg_green,bar_bg_blue,bar_bg_alpha,bar_border,border_red,border_green,border_blue,border_alpha,border_width,mid_value,lr,lg,lb,la,mr,mg,mb,ma,alarm_value,ar,ag,ab,aa)[/b]

you set values to all your strings and each string matches each entry in the function call list so that the values are transfered directly between the two

writing your code this spells out everything nice and simply which allows easy entry of settings

you don't have to remember what numbers mean what, you don't have to worry about commas and you are less likely to miss out a value (you would have to delete a whole setting line to do that)

the down side to this is that it takes a lot more lines of code this way.

ANOTHER WAY you can do something half way between these two methods mentioned so far which takes advantage of the fact that lua doesn't mind having its code split onto multiple lines AS LONG AS the information is bounded either by curved brackets () when sending info to functions or curly brackets {} when defining a table

take the rectangle drawing command cairo_rectangle (cr,50,50,50,-100)

this is just like the function we made, except you don't see the rectangle drawing code anywhere in the script (it's a built in function).

we call the function cairo_rectangle and send it the information it needs inside curved brackets ()

BUT once the curved brackets are opened (the opening bracket has to be on the same line as the function call) we can then split up our entries on to separate lines

cairo_rectangle (cr,
50,
50,
50,
-100)

we have to make sure that we have still have commas separating the entries but entering data this way has the advantage that we can write comments on each line and everything still works

cairo_rectangle (cr,
50,--rectangle x position
50,--rectangle y position
50,--rectangle width
-100--rectangle height
)

we could do this just the same with out indicator bar function you can see below i have split some entries onto separate lines but kept others together on the same line where it makes sense

indicator_bar(
750,400,--position x,y
30,--bar width
100,--bar height
tonumber(conky_parse("${cpu}")),--value to send bar
100,--max value
0.5,0.5,0.5,1,--bar background color rgba - grey
1,--want border, 1=yes,0=no
0,1,1,1,--border color rgba - light blue
10,--border width
50,--mid value, point of first color change
0,1,0,1,--low cpu% color rgba - green
1,1,0,1,--mid cpu% color rgba - yellow
80,--alarm value, point of second color change
1,0,0,1--alarm color rgba - red
)

A MORE COMPLEX USE OF FUNCTIONS

the functions i have given as examples are pretty straightforward set up the function, call it and give it the info it needs, the function outputs something for example a number or some graphics

but the use of functions can be more complex

here is an example we are going to have our main conky function, and two additional functions : one function is going to be for drawing arcs the second function will convert degrees into radians and adjust for the cairo_arc quirk so that 0 degrees is the top of the circle

our angle conversion function might look like this

function arad(degrees)
--compensate for cairo_arc quirk
degrees=degrees-90
--convert degrees to radians
radians=degrees*(math.pi/180)
--return angle
return radians
end--function arad

NOTE this line degrees=degrees-90 i do this quite often, as others do in their scripts

im taking the value of the string "degrees" as it was set inside () subtracting 90 from it but putting the result of that operation into a string also called "degrees"

as mentioned earlier, strings of the same name overwrite each other and the second instance of a string called "degrees" will indeed overwrite the first instance BUT not until the original "degrees" string is used in the calculation

as long as you know that you will no longer need the value held in the first "degrees" string after using it in, for example, a calculation then reusing the string name just makes it so that you don't have to keep thinking u new string names!

The arc drawing function might look like this ill use nice short string names here :)

mx will be for middle x position my will be for middle y position rad for radius sa for start angle ea for end angle (i will be giving angles in degrees when i call the function)

i want all my circles to be white with a line thickness of 1, so im going to set color,alpha and line_width in the function itself

function circle(mx,my,rad,sa,ea)
cairo_set_source_rgba (cr,1,1,1,1)
cairo_set_line_width (cr,1)
cairo_arc (cr,mx,my,rad,arad(sa),arad(ea))
cairo_stroke (cr)
end--function circle

the important line to note is this one cairo_arc (cr,mx,my,rad,arad(sa),arad(ea))

i can use the arad function inside the cairo_arc command to convert the degree values we send "circle" into adjusted radian values to use with cairo_arc

then last of all we have our conky_main function

function conky_main ()
--conky function setup lines
--##########################
circle(100,100,50,90,270)
--##########################
--cony function close out lines
end-- of conky main

if we think about the flow of information, we send "circle" the information in the function call inside conky_main

circle receives the list of values and sets its list of strings to those vales in order

it uses the strings my,mx and rad directly but it ships out sa and ea to arad

arad receives the value of sa, puts that value into a string called degrees and converts the value to adjusted radians and sends this new value back to "circle" where is is used as the start value directly in the cairo_arc command

then arad receives the value of ea and returns the result directly into cairo_arc

the chain of functions using functions could keep going for as many functions as you could write and you can of course use as many functions within any other function as you want

next part will introduce tables

Clone this wiki locally