Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm running some automated tests and while I'm debugging the info scrolls by in my Windows command prompt terminal. Test case results either start with ok or not ok. Is there any way to get the not ok messages to show up in red?

I'd much rather customize the Windows terminal than monkey around with the test automation architecture.

share|improve this question
3  
I don't think this is possible with the default command prompt. Something like hamiltonlabs.com/Cshell.htm might be of interest to you. –  Ramhound Sep 5 '13 at 16:32
    
Does this point you in the right direction? [Stackoverflow Question][1] [1]: stackoverflow.com/questions/2048509/… –  Gryphoenix Sep 5 '13 at 16:33
    
Doing some of the same things, I do the CLS thing first, then display the results in the desired color . leave the results up while doing the next thing (so it is there for time) Then clear again for the next colored results. Toss in some background flashing for error. Add in some sounds for yea and nay, It certannly would be nice if the colors of the text stayed, instead of changing all the text to one color. –  Psycogeek Sep 5 '13 at 19:53

1 Answer 1

Getting selective color output in a command cosole is difficult. In the days of DOS you could use ANSI escape codes via ANSI.SYS. But that does not work on modern Windows machines (definitely not 64 bit machines, and possibly not even 32 bit machines)

You can use PowerShell to output a single line in color. For example:

powershell write-host -foregroundcolor Red "Hello world"

But the context switching is very slow.

There are also batch file hacks that use FINDSTR to selectively output strings in color. See How to echo with different colors in the Windows command line.

But both of the above techniques require you to provide the color when the content is generated. But you want to selectively color the text after it has already been generated.

There certainly isn't any native utility that can do what you want. Perhaps there is a 3rd party tool that can search stdin and highlight matches in red, but I'm not familiar with any.

Instead of using color, you might consider reformatting each line that contains the string of interest. For example, matching lines could be prefixed with a string like >>>>>>>>>>. To accomplish this you need two things: 1) A regex line based search and replace utility, 2) the ability to pipe your test results to the utility.

I have written a hybrid JScript/batch utility called REPL.BAT. It performs a regex search and replace on stdin and writes the result to stdout. The utility is pure script, so no 3rd party executeables are needed. It runs on any Windows machine from XP onward. Full documentation is built into the script.

Assuming REPL.BAT is in your current directory, or better yet, somewhere within your PATH:

yourAutomatedTestCommand | repl ".*\bnot ok\b.*" ">>>>>>>>>> $&" i

The command above will only modify lines that contain not ok (case insensitive) at word boundries. There are many options for how you reformat the line of interest. For example, entire lines can be added both before and after the line using the X option coupled with the \n newline escape sequence.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.