Syntax for Conky conditionals

Add functionality to your desktop
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
AUserInEssex

Syntax for Conky conditionals

Post by AUserInEssex »

Dear all

I've got a simple question, but I can't find an answer by searching the web or indeed (though this may be a problem with the search tools, or my 'search-fu') this forum.

What is the syntax for Conky conditionals? That is, how do 'if' statements pair with 'endif's and 'else's? I can't work it out.

EDIT: I add the following. The way that Conky seems to handle spaces makes trying to figure such stuff out a nightmare. For formatting lines in a standard, logical way - with indentation and line breaks - changes the way the script functions. Arg!
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Habitual

Re: Syntax for Conky conditionals

Post by Habitual »

if the /media/flash mount is mounted, display USB is x% Free, else "USB is Not Mounted"

Code: Select all

${if_mounted /media/flash}${goto 600}${color red}USB$color is ${fs_free_perc /media/flash/}% Free$color${else}${color red}USB Not Mounted$color${endif}
One stop shop for all things conky...http://conky.pitstop.free.fr/wiki/index ... =Main_Page

Personally, I use goto statements instead of offset. Or I used to before I quit coding with conky.
AUserInEssex

Re: Syntax for Conky conditionals

Post by AUserInEssex »

Thanks, Habitual. So the syntax is as follows, right?

$[If-type command (e.g. if_mounted)]
[command(s) to be executed if the if-command evaluates as true]
${else}
[command(s) to be executed if the if-command evaluates as false]
${endif}

If the above is - broadly - correct, will it work with multiple commands between the 'else' and the 'endif'? Also, would it work with nested if-statements?

Thanks.
Habitual

Re: Syntax for Conky conditionals

Post by Habitual »

AUserInEssex wrote:Thanks, Habitual. So the syntax is as follows, right?

$[If-type command (e.g. if_mounted)]
[command(s) to be executed if the if-command evaluates as true]
${else}
[command(s) to be executed if the if-command evaluates as false]
${endif}

If the above is - broadly - correct, will it work with multiple commands between the 'else' and the 'endif'? Also, would it work with nested if-statements?

Thanks.
I have never seen nested if statements in a conky config.
That doesn't mean it won't accept them, just means I've never seen 'em used that way.
Multiple commands (like in a bash script) between else and endif? Never seen that either.
AUserInEssex wrote:changes the way the script functions. Arg!
What script is that?
What are you trying to 'do' exactly with conky?
AUserInEssex

Re: Syntax for Conky conditionals

Post by AUserInEssex »

What I am trying to do is the standard show-network-statistics stuff. (Well, my Conky does other things too - but the other parts of it are working fine.)

I developed my script from scripts I found on the web - and at least one of those scripts contained nested conditionals, I think. Anyhow, I ended up with nested conditionals. I have had to take them out, though - because, upon discovering that the script was not working as expected, I made some changes, and the nested part of the script stopped working. I have since removed all nesting, which does not make the script run entirely as I want but at least it works up to a point.

I will consider posting the script, but what I really would like is full(ish) documentation of how Conky handles if-else-endif code.
Habitual

Re: Syntax for Conky conditionals

Post by Habitual »

I have a 411 email out to a friend of mine who knows some stuff about conky that I do not.

I will advise.

Version ?
Possible references:
https://bbs.archlinux.org/viewtopic.php?id=60530
Last edited by Habitual on Tue Apr 07, 2015 10:53 am, edited 2 times in total.
AUserInEssex

Re: Syntax for Conky conditionals

Post by AUserInEssex »

Great. Thank you.

EDIT: I seem to have worked it out (with the help of this page). One can nest, and the if-else format is as I said it was - and the scope of an 'else' extends until an 'endif', that 'endif' closing the whle conditional (i.e. an 'else' clause does not require any additional 'endif'). So things - nesting - work like this:

Code: Select all

$[If-type command (e.g. if_mounted)]
    [command(s) to be executed if the if-command evaluates as true - and can include more conditionals, as follows]
    $[If-type command]
        [command(s) to be executed if the if-command evaluates as true]
        ${else}
                [command(s) to be executed if the if-command evaluates as false]
    ${endif}
${else}
     [command(s) to be executed if the if-command evaluates as false]
${endif}
Two notes.

(1) The way 'else' works means that, when one indents as I have above, the code looks wrong. For the logic of Conky is not this:

Code: Select all

If [something]
{
    [Do something]
}
else
{
    [Do something]
}
Rather - if I make no mistake (and of course Conky does not actually use the braces) it is this (!):

Code: Select all

If [something]
{
    [Do something]
    else
    {
}
(2) As I said: if one uses indentation/spacing as I have above, this will affect the text layout of the conky. So one has to strip them out (or, I suppose, use 'goto' or 'offset' commands to undo their effects).

Finally: here is (a snippet of) my working code.

Code: Select all

${if_existing /proc/net/route eth0}
${voffset -3}${font PizzaDude Bullets:size=12}O${font}${goto 30}${upspeed eth0}${alignr}${upspeedgraph eth0 8,60}
${voffset 5}${font PizzaDude Bullets:size=12}U${font}${goto 30}${downspeed eth0}${alignr}${downspeedgraph eth0 8,60}
${voffset 5}${font Webdings:size=14}~${font}${goto 30}Ethernet${else}${if_existing /proc/net/route eth1}
${voffset -3}${font PizzaDude Bullets:size=12}O${font}${goto 30}${upspeed eth1}${alignr}${upspeedgraph eth1 8,60}
${voffset 4}${font PizzaDude Bullets:size=12}U${font}${goto 30}${downspeed eth1}${alignr}${downspeedgraph eth1 8,60}
${voffset 5}${font Webdings:size=14}~${font}${goto 30}Ethernet${else}${if_existing /proc/net/route wlan0}
${voffset -3}${font PizzaDude Bullets:size=12}O${font}${goto 30}${upspeed wlan0}${alignr}${upspeedgraph wlan0 8,60}
${voffset 5}${font PizzaDude Bullets:size=12}U${font}${goto 30}${downspeed wlan0}${alignr}${downspeedgraph wlan0 8,60}
${voffset 5}${font Webdings:size=14}~${font}${goto 30}${wireless_link_qual_perc wlan0}%${alignr}${wireless_link_bar 8,60 wlan0}${else}${if_existing /proc/net/route wlan1}
${voffset -3}${font PizzaDude Bullets:size=12}O${font}${goto 30}${upspeed wlan1}${alignr}${upspeedgraph wlan1 8,60}
${voffset 5}${font PizzaDude Bullets:size=12}U${font}${goto 30}${downspeed wlan1}${alignr}${downspeedgraph wlan1 8,60}
${voffset 5}${font Webdings:size=14}~${font}${goto 30}${wireless_link_qual_perc wlan1}%${alignr}${wireless_link_bar 8,60 wlan1}
${else}Network Unavailable${endif}${endif}${endif}${endif}
I adapted this code - and took its 'PizzaDude' font - from . . er, I am not sure where. What the code does it display network information. The nested conditionals are (roughly) about showing ethernet (wired) connection if it is on, but wireless connection if it is on and wired is off.
Habitual

Re: Syntax for Conky conditionals

Post by Habitual »

Great job and well done!
A for effort and A+ for initiative.
I am not sure if I can contribute anything further to your situation atm.
AUserInEssex

Re: Syntax for Conky conditionals

Post by AUserInEssex »

That's fine. Thanks for the encouragement!
Habitual

Re: Syntax for Conky conditionals

Post by Habitual »

and the 411 was answered.

Have fun!
Locked

Return to “Compiz, Conky, Docks & Widgets”