Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Outputting in colors

Posted by johnjacky (johnjacky), 2 October 2005

I am writing an expect program and would like to print a message in red if a command fails and green if it as successful.

Is it possible to output messages in colors? if so what command should I refer to ?

I am using simple send_user now, like

send_user "Failed"

Also where can I read more about the flags of send_user, if there are more which I can see from man pages.


Posted by admin (Graham Ellis), 2 October 2005
Yes, it's possible.  Have a look at either Tk  (if you're using a GUI), the character sequences you need to change colour on a terminal (if you're wonking from a command line), or the HTML colour specifications if you're working via a web server.

Options to the Expect commands are fully documented in Don Libes' book - it's my reference of choice for Expect and every expect programmer should have one.   (No, I don't get paid for recommenting it!!)

Posted by Dimon (Dimon), 25 December 2007
I have solve this task with the next way:

# -------------------------------------------------------------------------
# Procedure to inserting text into GUI console with coloring some key words
# Arguments:
#    data - text string to write into console
#    base - scrolltext widget: target for text writing
# Return:
#    none
# 'Console' is an alias of ScrolledText widget
# ------------------------------------------------------------------------
proc InsertTextC {data base} {
# Set the color for error message tag

 set Console $base
 Console tag configure redtag -foreground red

 # text printing
 if {[string length $data]>0} {
    Console insert end $data
    Console see end ; update
 # Calc the carrage return codes in data
 set curret [regexp -all -indices -- {\n} $data match]
 # Read last string from GUI-console
 set endPos [expr [Console index end]-1]

 set startPos [expr $endPos - $curret]
 set lasttext [Console get $startPos $endPos]
 catch {string trim "\n"}
 # search for error indicating lexemas (specific for my application of cause)
 set res [regexp -nocase -line -indices -- {(failure)|(uncomplete)|(error+)} $lasttext indexes]
 # check for 'no error', lexema: it's important!
 if {[regexp -nocase -line -indices -- {error+} $lasttext indexes]} {
   if {[lindex $indexes 0]>=3} {
       set idx1 [expr [lindex $indexes 0]-3]
       set noerr [regexp -nocase -line -start $idx1 -- {No error} $lasttext match]
       if {$noerr>0} { set res 0 }
 if $res>0 {
   # Calc start and stop keyword's positions
   if {[llength $indexes]>0} {
      set idx1 [lindex $indexes 0]
      set idx2 [lindex $indexes 1] ; incr idx2
   # Calc start and stop indexes for position will be marked; format: string.char
   regexp -- {^\d*} $startPos lineNum
   set startPos $lineNum.$idx1
   set endPos $lineNum.$idx2

 # Bind tag to error keyword if it was found
 if $res>0 {
   Console tag add redtag $startPos $endPos

It's really works

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho