Creating Conky text variables with zero padding?

A solution provided by @jasonwryan above:

  1. Create a Lua script for Conky to use. I created mine in a folder I made in ~/.config/conky/scripts, but you can create yours wherever you'd like:

    $ mkdir -p ~/.config/conky/scripts/
    $ vim ~/.config/conky/scripts/conky_lua_scripts.lua
    
  2. Fill the file with the following Lua function:

    function conky_format( format, number )
        return string.format( format, conky_parse( number ) )
    end
    
  3. Import your Lua script file into your Conky configuration file using the lua_load directive

    # ...
    lua_load ~/.config/conky/scripts/conky_lua_scripts.lua
    
    TEXT
    # ...
    
  4. Whenever you'd like to format a value, call the format function we defined earlier. Note that though we named it conky_format, we access it as format using the lua_parse variable:

    # ...
    lua_load ~/.config/conky/scripts/conky_lua_scripts.lua
    
    TEXT
    # ...
    ${lua_parse format %3.0f ${cpu cpu1}}%
    

This nice script allows you to call into Lua formatting engine with any value and format string. The output now looks as expected:

awesome

If you're familiar with printf, you can use the utility to do other awesome formatting hacks.


Almost same effect can be achieved also without lua script by using conditions:

${if_match ${cpu cpu1} < 10}${offset 10}${cpu cpu1}%

Your code would be something similar to:

${cpubar cpu1 6,135}$alignr${...}${if_match ${cpu cpu1} < 10}${offset 10}${endif}${cpu cpu1}%

Note: Offset value (in above case 10) needs to be tweaked according to used font.

More complete example can also use more conditions:

${cpubar cpu1 6,135}$alignr${...}${if_match ${cpu cpu1} < 10}${offset 20}${else}${if_match ${cpu cpu1} < 100}${offset 10}${endif}${cpu cpu1}%

Tags:

Conky