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 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Outputting in colors

Posted by johnjacky (johnjacky), 2 October 2005
Hi,

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.

Thanks.

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
 }
 return
}


It's really works




This page is a thread posted to the opentalk forum at www.opentalk.org.uk 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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho