Modify Conky to handle variable length values

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.


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

use_spacer left
pad_percents 2

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