1

I'm trying to customize my progress bars in Conky (battery_bar, fs_bar...) in order to have a layout other than the default one, which looks like:

Default progress bar in Conky

Following this answer I managed to create a filesystem usage bar, and with some code modifying, a battery status one, looking like this.

Battery bar right now, no modifications.

This bar is generated following the following script, a variation of the one suggested for in the previous answer:

#!/bin/bash

cat /sys/class/power_supply/BAT0/capacity | awk 'NR==1{
   n = ($1+0)/2; yellow = 20; red = 40;

   if(n>=red) {
        r = "${color ff0000}";
        for(;n>=red;n--)
                r = r "\\#"
   }

   if(n>=yellow){
        y = "${color ffff00}";
        for(;n>=yellow;n--)
                y = y "\\#"
   }

   g = "${color 00ff00}";

   for(;n>0;n--)
         g = g "\\#";

   print g y r
  }'

My problem is that the bar's length is constant, and it will constantly resize the Conky window until it's able to show the 100% of its capacity, full size. This obviously forces my Conky window size to be at least the length of those custom bars, deforming the screen.

As far as I have experimented, I can see that Conky's default bars are 'responsive' to the windows size they are given, and never cause problems in this aspect; as they resize themselves without a problem. I would like to know how are they programmed in order to apply the same algorithm to my code the cleanest way.

xvlaze
  • 837
  • 1
  • 10
  • 30

2 Answers2

3

One thing you can do fairly easily is add some programming in lua to change the font size just before drawing the bar. The size would be calculated from the window width divided by 50. If using proportional fonts you might need some small scale factor to account for the fact that a font of a given size might have a # character of a different width.

Create a file to hold the lua script, say ~/mylua.lua with the following

-- called with (50) return "${font DejaVu Sans Mono:size=13.6}"
function conky_mysetfont(numchars)
 if conky_window.width<=0 then return "" end
 fontname = "DejaVu Sans Mono"
 scale = 1.2
 fontsize = conky_window.text_width/tonumber(numchars)*scale
 -- print("result=",fontsize) -- debug
 return "${font "..fontname..":size="..fontsize.."}"
end

The -- starts a comment. If you remove the one in front of print, you should see something like result= 13.6 on stdout if you run conky from the terminal. The function takes a parameter, the length of your bar, ie 50 characters. It returns a conky command string like ${font somefont:size=13.6}. .. is a concatenation operator. The above choses a fixed-width font DejaVu Sans Mono and an approximate scale of 1.2.

In your ~/.conkyrc, add in the conky.config = {...} part (for 1.10) a line

lua_load = '~/mylua.lua',

to load in your code. In the conky.text = [[...]] part, replace the line where call your script, eg

${execpi 30 ~/mydf /}

with

${lua_parse conky_mysetfont 50}
${execpi 30 ~/mydf /}
$font

i.e. call your lua function, passing the number of characters, your original script, then reset the original default font and size.

In conky 1.9 when you resize the window with the mouse, this code will change the font size to match, but in 1.10 the size changes only when the window changes size due to some internal trigger. It seems this is a regression.

Note that many people don't have problems with resizing because they display conky on their fixed-size desktop background. Also, once you start using lua, an alternative to using text for bars is to get lua to draw any sort of graphics such as coloured lines and boxes. You can read about this in the wiki, and see amazing examples of what is possible.

meuh
  • 11,500
  • 2
  • 29
  • 45
  • This seems to make my `#` bigger every second. I don't know the cause of the error. – xvlaze Dec 07 '16 at 16:13
  • Try removing the `--` comment from the print to see what sizes it is returning. Run conky in the terminal to get any messages. Does the window change width? – meuh Dec 07 '16 at 16:27
  • Uncommenting the `print` line, the display is: `result= 17.712 result= 17.712 result= 23.184 result= 23.184 result= 30.024 result= 30.024 `. The window gets bigger and bigger, until covering the whole screen. – xvlaze Dec 07 '16 at 16:35
  • Reduce the `scale=` value, eg to `0.9` until the bar at its max value stays inside the window without making it grow. – meuh Dec 07 '16 at 17:00
  • This works, but every time I launch Conky I see my custom bar reducing tiself until it reaches its proper size. Conky's default bars won't do that. Is there any way to make my bars behave like the default ones? Also, if you know how are they programmed and can explain it to me or show me the source code path I'd be really thankful. – xvlaze Dec 08 '16 at 21:47
  • As I said, conky bars are drawn using the graphics primitives, not as characters. Follow the tutorials given in the [wiki](https://github.com/brndnmtthws/conky/wiki) link and you can program coloured rectangles of any size that fit in the existing window. – meuh Jul 28 '17 at 14:20
0

possibly not 100% an answer to your question but you may give it a try I am using conky 1.10.6 on a raspberry pi with KDE Desktop. I am using one line to display most of the file systems (vfat partition excluded with option -x).

${execpi 60 df -h --output=source,target -x vfat| grep '^/dev/' | cut --characters=6- | awk '{ print $1," ", $2,"${alignr}${fs_size " $2 "}","${alignr}${color blue}${fs_bar 11,100 " $2"}\n\n\n,${alignr}${voffset -39}${color white}${fs_used_perc " $2 "}%" }'}

Result:

Conky

cheers

MikeOffenbach
  • 109
  • 1
  • 5