From: Simon Strandgaard Date: 2004-05-18T16:02:22+09:00 Subject: Re: Highlighting for unit testing output (console) Gavin Sinclair wrote: > I'll personally award three kudos points to anyone who implements a patch > to apply some level of highlighting to the console output of a unit test. > > It should try to load the 'text/highlight' package available on the RAA > (or some other package if appropriate) and continue gracefully without > highlighting if that library is not available. I have spend some time this morning.. attempting to replace the $stdout and $stderr filedescriptors with a pipe. Whenever something is outputted to stdout it could be colored green, and red output if stderr. I have done it before.. but no success today. If you read this, then it means I never made it. Earlier I have used fork, where the childprocess writes into one end of the pipe, and where the parentprocess reads from the other end. However there is no forking here, so what matters is correct replacement of filedescriptor#1... however its behaving weird. server> expand -t2 col.rb BLACK = "\x1b[30m" RED = "\x1b[31m" GREEN = "\x1b[32m" #puts "#{RED}hello#{BLACK} world" def capture(&block) x = $stdout.dup p x, x.isatty p $stdout, $stdout.isatty r1, w1 = IO.pipe # stdout fdes = [r1] thread = Thread.new do loop do res = select(fdes, nil, nil, 0.1)[0] if res.include?(r1) unless r1.eof x.print "#{GREEN} #{r1.read} #{BLACK}" else fdes.delete r1 thread.kill if fdes == [] end end end end #$stdout.reopen(w1) $stdout = w1 op = [$stdout.inspect, $stdout.isatty.inspect] block.call w1.flush #w1.close $stdout.reopen(x) sleep(0.5) # r1.eof? thread.kill r1.close #$stdout.reopen(x) #$stdout = STDOUT puts op.join("\n") end puts "before" capture do puts "stdout" $stderr.puts "stderr" puts "stdout" end puts "after" server> -- Simon Strandgaard