我想通过在tcl中使用puts命令来更改控制台中显示的文本颜色,以简化调试.我看到很多文章是abt tk但不是tcl.fyi,我在Windows 7上使用活动tcl.
我试过其他人提供的以下代码(http://www.tek-tips.com/viewthread.cfm?qid=1283356)但是徒劳无功:
puts "Why not \033\[34mG\033\[31mo\033\[33mo\033\[34mg\033\[32ml\033\[31me\033\[0m first ?"
请咨询.
您引用的代码适用于我(OSX,Terminal.app; Tcl 8.4,8.5和8.6),我希望它在Linux上也能正常工作.(在Windows上,控制台以不同的方式工作会有所不同.)在Linux上失败的原因表明问题不在Tcl中,而在于其他地方; 我猜它是在你的终端,它不想尊重颜色代码.另一个外部机会是你的终端出于某种原因更喜欢不同的逃脱序列.
解决第二个问题的方法是这样的:
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!
Run Code Online (Sandbox Code Playgroud)
如果这是第一个问题 - 你的终端只是不会做颜色 - 那么你就会被卡住,直到你改变你的终端.对不起,它真的很简单.