Thursday, August 02, 2012

Color-coded conky

I've used Conky in many Linux OSes, including on my Raspberry Pi and additionally in FreeBSD.
However, I came across a very useful enhancement a few days ago when I stumbled on this thread post.

This simple script allows you to include alerts in your conky display such that the item is displayed in different colors depending  on whether its value is normal, high or critical.
A more elaborate description is provided here.

And it works great!
Here's an example of the conky line I used to display one of the CPU core temperatures:
Core0 Temp: ${alignr} ${execpi 6 /usr/bin/sensors | grep 0: | paste -s | cut -c16-19 | xargs ~/Scripts/colorize_CPU.sh}°C ${color}
where ~/Scripts colorize_CPU.sh is the following:
 #!/bin/bash
# colorize_CPU.sh
COOL=70
WARM=90
if [[ $1 -lt $COOL ]]
   then echo "\${color2}"$1    # COOL
elif [[ $1 -gt $WARM ]]
   then echo "\${color4}"$1  # HOT
else echo "\${color3}"$1                        # WARM
fi
exit 0
The variables color2, color3 and color4 are defined in ~/.conkyrc as green yellow and red.
 As a result, CPU temperatures below 70°C are displayed in green. Between 70-90°C in yellow and in red when it's in excess of 90°C.

While this script works fine, there is a way in conky to get the same effect. Amazingly, I've only ever seen this hidden in this thread post (look at the only TEXT line in .conkyrc).
This permits if-then-else in .conkyrc which can incorporate changing the color of whatever variable you want depending on its last-measured value.
Here's the code I used in .conkyrc to provide the same effect as I got with the script above but this time using conky's if-then-else:
${if_match ${execpi 6 /usr/bin/sensors | grep 0: | paste -s | cut -c16-17}<=70}${color2}${else}${if_match ${execpi 6 /usr/bin/sensors | grep 0: | paste -s | cut -c16-17}<=90}${color3}${else}${color4}${endif}${endif}Core0 Temp: $alignr ${execpi 6 /usr/bin/sensors | grep 0: | paste -s | cut -c16-17}°C $color
In addition, I wanted to color-code my Gmail inbox such that no unread mails was shown in green, yellow for one new mail and red for two or more.
This required modifying the gmail_parser.py script (Copyright (C) 2006 Baishampayan Ghose ).

The Readmail section which, in the original, is


def readmail(feed, maxlen):
'''Parse the Atom feed and print a summary'''
atom = feedparser.parse(feed)
print '${color3} %s new email(s)\n' % (len(atom.entries))
for i in range(min(len(atom.entries), maxlen)):
print '          ${color2}"%s"' % fill(atom.entries[i].title,40)
#uncomment the following line if you want to show the name of the sender
# print '          ${color2}%s' % atom.entries[i].author
if len(atom.entries) > maxlen:
print ' ${color}more...'
is changed to
def readmail(feed, maxlen):
'''Parse the Atom feed and print a summary'''
atom = feedparser.parse(feed)
if len(atom.entries) == 0:
print '${color2} % s new email(s)\n' % (len(atom.entries))
elif len(atom.entries) == 1:
print '${color3} %s new email(s)\n' % (len(atom.entries))
else:
print '${color4} %s new email(s)\n' % (len(atom.entries))
for i in range(min(len(atom.entries), maxlen)):
print '          "%s"' % fill(atom.entries[i].title,40)
#uncomment the following line if you want to show the name of the sender
# print '          ${color2}%s' % atom.entries[i].author
if len(atom.entries) > maxlen:
print ' ${color}more...'

Now, the conky in my Ubuntu 12.04 displays section titles in gray, anything that is essentially invariant in white and variables in either green/yellow or red depending on whether the vaue is normal, worrying or critical.
I like this as it really adds a whole extra dimension to what was already very useful.
See the screenshot.

1 comment:

  1. Thanks for that link. That was exactly what I was looking for.
    Also I love that desktop background image. Wish we could see more ;)

    ReplyDelete