6

I've got a strange issue with my Conky setup:

enter image description here

What I'm looking to get rid of/fix is the fact that my CPU percentages (using ${cpu cpuX}) won't seem to pad properly. I'd like all values to be aligned vertically so that statuses never wiggle. Here's excerpts from my conky file:

# ...
pad_percents 3
# ...
${cpubar cpu1 6,135}$alignr${...}${cpu cpu1}%

How can I right align and pad CPU percentage values so they stop throwing off my layout? The equivalent printf would be %3.0f so that values will appear like this:

$ "%3.0f" % (1,) 
'  1'
$ "%3.0f" % (13,)
' 13'
$ "%3.0f" % (100,)
'100'

How can I make this happen in Conky for my CPU percentage?

2
  • 1
    See: superuser.com/questions/178179/…
    – jasonwryan
    Dec 6, 2012 at 21:07
  • Thanks, I wrote a Lua printf function which takes a format string and a value, works great! Only thing now is to set a monospace fonnt so we never see a wiggle due to difference in character widths. Dec 6, 2012 at 21:17

2 Answers 2

11

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.

1
  • 1
    conky: Syntax error (/home/jhartley/.config/conky/conky.conf:22: '=' expected near '~') while reading config file. Jan 21, 2020 at 2:56
3

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}%
1
  • 1
    An alternative to the 'offset' is to have 'if_match' simply output a zero or a space, using something like ${if_match ${cpu cpu0}<10}0${endif}${cpu cpu0}% Jan 21, 2020 at 3:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .