You are not logged in.

#1 2013-06-08 14:05:53

Kalrish
Member
Registered: 2013-02-27
Posts: 62

[SOLVED] Colorize text that has already been printed

I'm making a small script for personal use, whose purpose is to output the state of my cooling devices in a nice way. For now, this is what I have, and, although it may be a bit dirty, it works:

( echo 'n type max cur' ; for P in /sys/class/thermal/cooling_device* ; do echo -n "$P " | tr -d '/sys/class/thermal/cooling_device' ; cat "$P"/{'type','max_state','cur_state'} | tr '\n' ' ' ; echo ; done) | column -t

Its output, in my machine, is the following:

n  type       max  cur
0  Processor  10   0
1  Processor  3    0
2  Fan        1    0
3  Fan        1    0
4  Fan        1    1
5  Fan        1    1

I would like to colorize the output, but using either tput or color escape codes confuses column. Looking for a way to solve this in column has not succeeded; it seems it doesn't have any option to strip escape codes, or to ignore them. So, what can I do? I've thought about modifying the already-printed text, but that doesn't seem to be a very 'elegant' solution (I can't imagine how to do it, either).

Any ideas would be greatly appreciated. Good readings are also welcomed.

Thanks in advance,
Kalrish

Last edited by Kalrish (2013-06-09 12:24:07)

Offline

#2 2013-06-09 09:02:02

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 218

Re: [SOLVED] Colorize text that has already been printed

Try using printf instead of column.

#!/bin/bash

black='\e[0;30m'
red='\e[0;31m'

for d in /sys/class/thermal/cooling_device*; do
	cd "$d"
	x=(${d: -1:1} $(cat type max_state cur_state))
	printf "${black}%2d %-10s ${red}%2d${black} %2d\n" ${x[@]}
done

Last edited by rockin turtle (2013-06-09 09:05:13)

Offline

#3 2013-06-09 12:23:52

Kalrish
Member
Registered: 2013-02-27
Posts: 62

Re: [SOLVED] Colorize text that has already been printed

Thanks, rockin, printf solves my problem.

Marking as solved. Best regards,
Kalrish

Offline

Board footer

Powered by FluxBB