From: Paul Lutus Date: 2006-12-09T03:45:05+09:00 Subject: Re: autotest, unit-diff, and color output? Rob Sanheim wrote: > I'm using autotest with all its test driven goodness, and I believe > that uses unit-diff to present a "saner" view of test difference > output. > > I'd like to do colorize the output from unit-diff so its similar to > what you see on a changeset on trac, for instance. Does anyone know > anything that can colorize diffs in ruby, or know of a good starting > point? Or at least where to start for outputting color in the > terminal, so I can do "#{color.red} stuff" instead of using control > characters. An easy, lightweight way to go would be to write a script that creates an HTML file out of the diff data, popping in color-change tags as required, using your own choice of colors and syntax rules. This would be very easy to do, once you have decided how you want the output to look. Of the available choices, HTML is actually very easy to exploit for a formatting task like this. Whoops, I just saw your having mentioned a terminal. Okay, then, you can use ANSI colors instead, using a similar process, this time the script could be a filter, the last step before displaying the data on the terminal. Same syntax rules, different way to create the colors. > so I can do "#{color.red} stuff ... Okay, here is an example: ------------------------------------------------- #!/usr/bin/ruby -w class ANSIColors ANSIColors::Col_hash = { :black => 30, :red => 31, :green => 32, :yellow => 33, :blue => 34, :magenta => 35, :cyan => 36, :white => 37 } def ANSIColors::color(s) return "\033[#{Col_hash[s]}m" end end puts "#{ANSIColors::color(:red)} this should be red." puts "#{ANSIColors::color(:green)} this should be green." puts "#{ANSIColors::color(:blue)} this should be blue." puts "#{ANSIColors::color(:black)} this should be black." ------------------------------------------------- Try it on your terminal. If it doesn't work, chances are your terminal is not ANSI-color enabled. -- Paul Lutus http://www.arachnoid.com