2

I have started using Conky some days ago, and I'm willing to create my own configuration. I have added some colors, cool ASCII art and learned the basics.

However, I don't like the default progress bars coming with Conky, and I would like to create something like a string of 50 '#' signs or 'rectangles' (219th character in the ASCII table), being the first 20 green, the following 20 yellow and the last 10 red.

I'd like to implement it as a fs_bar, being green when having plenty of free space, yellow when it's half full and red when I should free some files, but showing the three colours in the two last cases. I'm attaching an image with a pretty similar result.

I am running AwesomeWM in Arch Linux, and my Conky version is 1.10.5.

xvlaze
  • 279
  • 3
  • 15

1 Answers1

3

You can do something simple like this, which uses execpi to run a shell script every 30 seconds that parses the output of df / and converts it into a long string of conky color commands and \# characters (since # is used for comments):

${execpi 30  df --output=pcent / | awk 'NR==2 {
  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
  printf "%50s"," "
 }' }
${color}

My df --output=pcent outputs 2 lines; the second one is a percentage used, eg 69%. I tried this on conky 1.9.


If your ~/.conkyrc file has been converted to format version 1.10 then it will contain a line

conky.text = [[

Make sure you add the above script before the final closing ]]. Also, in 1.10 colours given as numbers (eg #ff0000 above) no longer begin with # so you should use ff0000 and so on in the script. To simplify, put the following script into a separate file somewhere in your PATH, say ~/mydf, make it executable (chmod +x ~/mydf), and then put that filename in ~/.conkyrc, eg ${execpi 30 ~/mydf /}

#!/bin/bash
df --output=pcent "${1?}" | awk 'NR==2{
   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
   printf "%50s"," "
  }'

If you want to put the whole script in the ~/.conkyrc file, you will need to increase the default buffer size or the command will be truncated to 256 characters. This leads to errors like

sh: -c: line 0: unexpected EOF while looking for matching `''

To do this, in 1.10 add a line inside the conky.config={...} part, making sure you separate the settings with a comma (,):

text_buffer_size = 400,

In conky 1.9 add a line before the TEXT section:

text_buffer_size 400

To stop the window resizing as the number of characters printed increases, a final printf "%50s"," " adds a second line of spaces of the maximum length. Alternatively, add a configuration setting for the minimum size of the window in pixels, eg minimum_size 500 (or minimum_size=500, for 1.10), where the value to use depends on the font width of the # character.

meuh
  • 49,672
  • 2
  • 52
  • 114
  • My output is an empty extra line at the end of Conky. I have added it to the end of my conf file. – xvlaze Nov 26 '16 at 20:33
  • Try running the command (from `df ...` to the `}'` in your shell. It should output something like `${color #00ff00}\#\#\#\#...`. – meuh Nov 26 '16 at 20:37
  • I tried it before and seemed to work as expected. My output is ${color #00ff00}\#\#. – xvlaze Nov 26 '16 at 20:45
  • You could try adding a final `$hr` command at the end for a horizontal line. Or try replacing the `\\#` which might be troublesome, with say `X`. If you run conky from the terminal does it write any error messages? What version conky do you have? There were changes in 1.10. – meuh Nov 26 '16 at 20:54
  • I copypasted your snippet and my output was: _$ conky conky: desktop window (9a) is root window conky: window type - desktop conky: drawing to created window (0x1400001) sh: -c: line 0: unexpected EOF while looking for matching `'' sh: -c: line 7: syntax error: unexpected end of file ^Cconky: received SIGINT or SIGTERM to terminate. bye!_ – xvlaze Nov 26 '16 at 21:51
  • I compiled a version 1.10 and got the same error. I dont know what is causing it. I updated my answer with a cleaner solution, putting the shell script in a separate file and calling it with a single parameter. This worked for me. – meuh Nov 26 '16 at 21:57
  • I'm getting these errors: `awk: cmd. line:4: = r "\\#" } awk: cmd. line:4: ^ syntax error awk: cmd. line:6: = y "\\#" } awk: cmd. line:6: ^ syntax error` ...when calling the script like `${execpi 30 ~/Scripts/dfbar /}`. – xvlaze Nov 26 '16 at 23:47
  • Your editor may have introduced an error when copying the script. It seems to have split the awk lines, as `= r...` is on awk line 3 not 4! (do NOT move discussion to chat, please). – meuh Nov 27 '16 at 09:34
  • Thank you! However, I'm annoyed about why Conky 1.10 can't read that script directly form the conf file and throws an error. Is there a place to report it or be given an answer? – xvlaze Nov 27 '16 at 11:42
  • I updated my answer with an explanation of the error, and how to fix it. – meuh Nov 27 '16 at 13:11
  • I'm experiencing that, if the bar becomes longer than the actual Conky screen, it will resize the whole screen in order to show its whole content, while default bars in Conky don't do that. How can i make my bar 'responsive' in that way? – xvlaze Nov 27 '16 at 17:28
  • To stop resize you can specify a minimum window size. See answer edit. Consider starting a new question. – meuh Nov 27 '16 at 17:56
  • Unfortunately didn't work for me. I opened a new question in https://unix.stackexchange.com/questions/326521/how-are-progress-bars-programmed-in-conky-and-how-can-i-apply-this-in-my-own-cus – xvlaze Nov 28 '16 at 09:30