How to change color of Conky text via file/script?

1

I have a file which contains one word, "green", "yellow" or "red" (this file is updated via a cron job.

I would like to use that color to display text in Conky. The actual text would stay the same, but the color would update depending on that file. Does anyone know of a way to make this work? Thanks!

Trevor

Posted 2011-12-08T17:30:36.447

Reputation: 133

Answers

2

Create a bash-script that outputs ${color yourcolor} :

#!/bin/bash

read -r </path/to/color-file color
echo -n '${color '$color'}'

And run it in conky (every 10 seconds):

${execpi 10 /path/to/script.sh} Colored text here {color}

Something nested like ${color ${head -1 /path/to/color-file}} might also work.

micke

Posted 2011-12-08T17:30:36.447

Reputation: 3 001

Here's an alternate line #4 for your script which will even work if your color file contains some garbage (e. g. control characters) beyond the 1st line for some reason: awk 'NR==1{print}' color-file. I'd always avoid cat in these cases because it is always risky to blindly assume the file will only contain one neat line. A blank line following the first line might already induce problems. – syntaxerror – 2014-10-11T13:08:36.690

You're right @syntaxerror I edited the answer and also updated the bash-script – micke – 2014-10-11T15:28:56.403

Looks pretty good now, thanks for the quick update ... except that I'm still not sure that this will work in Conky without the first $ in the echo line being escaped with a backslash. Conky is always picky about these things.:) Even though I might be wrong. – syntaxerror – 2014-10-11T17:38:17.207

Nah, it's cool. No need for escaping inside single quotes. – micke – 2014-10-11T21:24:48.727

0

Note that you will need execp or execpi to do the above as exec and execi do not parse the output of the script

execp: Executes a shell command and displays the output in conky. warning: this takes a lot more resources than other variables. I'd recommend coding wanted behaviour in C and posting a patch. This differs from $exec in that it parses the output of the command, so you can insert things like ${color red}hi!${color} in your script and have it correctly parsed by Conky. Caveats: Conky parses and evaluates the output of $execp every time Conky loops, and then destroys all the objects. If you try to use anything like $execi within an $execp statement, it will functionally run at the same interval that the $execp statement runs, as it is created and destroyed at every interval.

This also works with simply cat'ing a file such as:

${execpi 15 cat /path/to/file.log}

DRAD

Posted 2011-12-08T17:30:36.447

Reputation: 9