Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change color text using puts in tcl

Tags:

tcl

I would like to change the text color displayed in the console by using puts command in tcl to ease the debugging. I saw a lot article is abt tk but not tcl. fyi, i am using active tcl on windows 7.

i have try on the code below provided by others(http://www.tek-tips.com/viewthread.cfm?qid=1283356) but in vain: puts "Why not \033\[34mG\033\[31mo\033\[33mo\033\[34mg\033\[32ml\033\[31me\033\[0m first ?"

Pls advice.

like image 956
user981714 Avatar asked Jan 09 '23 23:01

user981714


1 Answers

The code you quote works for me (OSX, Terminal.app; Tcl 8.4, 8.5 and 8.6) and I would expect it to work just as well on Linux. (It'd be different on Windows, where the console works in a very different way.) That it fails for you on Linux is an indication that the problem is not in Tcl but rather somewhere else; I'd guess that it is in your terminal, which doesn't want to honour the colour codes. The other outside chance is that your terminal prefers different escape sequences for some reason.

The way to work around the second problem is like this:

proc color {foreground text} {
    # tput is a little Unix utility that lets you use the termcap database
    # *much* more easily...
    return [exec tput setaf $foreground]$text[exec tput sgr0]
}

puts "Why not [color 4 G][color 1 o][color 3 o][color 4 g][color 2 l][color 1 e] first?"
# Hmm, that's clearer than using those escapes directly too!

If it's the first problem — your terminal just won't do colour — then you're stuck until you change your terminal. Sorry, it really is as simple as that.

like image 146
Donal Fellows Avatar answered Feb 08 '23 17:02

Donal Fellows