How to change text color with puts in tcl

I would like to change the color of the text displayed in the console using the puts command in tcl to make debugging easier. I have seen many abt tk articles but not tcl. fyi, I am using active tcl on Windows 7.

I tried 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 recommendation.

+3


source to share


1 answer


The code you are citing works for me (OSX, Terminal.app; Tcl 8.4, 8.5 and 8.6) and I expect it to work just as well on Linux. (This would be different on Windows, where the console works very differently.) That this fails for you on Linux indicates that the problem is not in Tcl, but rather elsewhere; I would assume it is in your terminal that doesn't want to respect color codes. Another external possibility is that your terminal prefers different escape sequences for some reason.

The way to solve the second problem is as follows:



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 clearer than using those escapes directly too!

      

If this is the first problem - your terminal just won't do color - then you're stuck until you change your terminal. Sorry, it really is that simple.

+6


source







All Articles