Modify Conky to handle variable length values

8

1

I currently experimenting with DWM and Conky to create a minimal looking desktop to use for coding and things like that. I've got something nice going on, but there's something I'm having an issue with at the moment. Right now, my Conky TEXT section is centre aligned, which looks nice, but it is also returning information about CPU usage. If the CPU usage is flickering between, say, 9 and 10 %, the overall size of the text is different, and because it's centre aligned, it moved around a bit. Colours removed for a second for clarity, here's a simple version of the line in question...

${alignc}CPU: ${cpu}%

The difference, of course, between the two of

CPU 9%

CPU 10%

is what's causing the issue. How would I go about left-padding the 9 with two zeroes, then 10 with one zero, and then 100 with no zeroes? The only thing I can see vaguely related in the documentation has to deal with decimal place padding, which isn't what I need at all.

JBirch

Posted 2010-08-20T05:24:52.860

Reputation: 456

Answers

6

A solution (can't find anything better now, but it works) involves the use of a custom Lua function, here's how you should do:

  1. Create a file for the Lua function, say ~/.conky_lua_scripts.lua with:

    function conky_pad( number )
        return string.format( '%3i' , conky_parse( number ) )
    end
    

    This will pad the number with spaces (imo nicer), if you want zeros just replace '%3i' with '%03i'.

  2. In your .conkyrc add before the TEXT section:

    lua_load ~/.conky_lua_scripts.lua
    
  3. Finally to print a padded value type in your TEXT section something like:

    ${alignc}CPU: ${lua_parse pad ${cpu}}%
    

I tried to keep the Lua function as simple as I can, but you can make a more generic one, if you want, so you can manage any number/value or even change its alignment.

cYrus

Posted 2010-08-20T05:24:52.860

Reputation: 18 102

This works great, hadn't added extra lua scripts before! I updated the function by adding a padding arg, also made it able to pad strings by changing number to value and do: string.format(string.format('%%%is', conky_parse(padding)), conky_parse(value)) – mVChr – 2017-03-08T22:10:11.163

I'm afraid I'm not in a position to test it, but it looks valid to my (reasonably novice) Conky eye. And it's a cool little hack to boot ^^ – JBirch – 2010-08-20T09:28:51.117

Let me know, once you've tested it, for any issues. – cYrus – 2010-08-20T09:50:32.130

5

Conky finally has it built-in. These options do the trick:

use_spacer left
pad_percents 2

sgtpep

Posted 2010-08-20T05:24:52.860

Reputation: 256

1what (precisely) do they do ? – Nikana Reklawyks – 2016-03-10T23:28:36.790

1

I had a similar concern while trying to display percentages as, say 04% 05% etc, instead of 1%, 5%, 0%, etc (to avoid the text "jumping around"). I was able to code a simple if structure using $if_match

${if_match ${cpu cpu0}<10}0${endif}${cpu cpu0}%

^^^ What this does is print a 0 in the tens column if ${cpu cpu0} is less than 10. Then it prints the ones column digit. Then it prints the % symbol.

$if_match will print, run, or execute whatever is between itself and the ${endif}

${if_match [COMPARISON]}
...commands                  <<< all i do is print a 0.  heh
${endif}

Here, I'll break it down into components with comments, multi-line

${if_match ${cpu cpu0}<10}  ### is the cpu load less than 10% ?
0                           ### if so, print a 0 !!!
${endif}                    ### thanks bye i had a really good time
%                           ### output formatting.  so it says 08% 
                              # instead of 08

petey

Posted 2010-08-20T05:24:52.860

Reputation: 11

0

I usually align the text (CPU) to the left and the values to the right, and specify their exact position. This way the values are "extending" (from 9 to 10 for example) into the empty space between CPU and value.

Patkos Csaba

Posted 2010-08-20T05:24:52.860

Reputation: 1 542

That was one consideration, but from a design standpoint, I really want them in the middle. – JBirch – 2010-08-20T09:28:01.460