# Autotest, unit-diff, and color output?

**URL:** <https://www.ruby-forum.com/t/autotest-unit-diff-and-color-output/78800>\
**Category:** Ruby\
**Created:** [December 8, 2006, 6:27pm UTC](https://www.ruby-forum.com/t/autotest-unit-diff-and-color-output/78800 "2006-12-08T18:27:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [December 8, 2006, 6:27pm UTC](https://www.ruby-forum.com/t/autotest-unit-diff-and-color-output/78800/1 "2006-12-08T18:27:28Z")

</div>

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.

thanks,  
Rob

---

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [December 8, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/autotest-unit-diff-and-color-output/78800/2 "2006-12-08T19:09:40Z")

</div>

Rob S. 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.

you can try facets’ ansicode lib written primarily by Florian F…

t.

---

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [December 8, 2006, 7:45pm UTC](https://www.ruby-forum.com/t/autotest-unit-diff-and-color-output/78800/3 "2006-12-08T19:45:47Z")

</div>

Rob S. 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.

---

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [December 9, 2006, 1:28am UTC](https://www.ruby-forum.com/t/autotest-unit-diff-and-color-output/78800/4 "2006-12-09T01:28:35Z")

</div>

On Dec 8, 2006, at 09:26 , Rob S. 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.

It does.

> 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.

The easiest way to do this is to write (or find) a color-capable diff  
executable. This way you don’t have to change unit\_diff, just your  
path.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

I LIT YOUR GEM ON FIRE!

---

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [December 10, 2006, 10:34pm UTC](https://www.ruby-forum.com/t/autotest-unit-diff-and-color-output/78800/5 "2006-12-10T22:34:07Z")

</div>

On 12/11/06, Rob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for the responses everyone. Some quick google searches didn’t  
> turn up any drop in color replacements for diff, though I did find it  
> as a feature request submitted for diff, but way to recently for any  
> code to be written yet.

$ esearch colordiff  
[Results for search key : colordiff]  
[Applications found : 1]

- app-misc/colordiff  
Latest version available: 1.0.5-r2  
Latest version installed: 1.0.5-r2  
Size of downloaded files: 41 kB  
Homepage: [http://colordiff.sourceforge.net/](http://colordiff.sourceforge.net/)  
Description: Colorizes output of diff  
License: GPL-2

martin

---

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [December 10, 2006, 9:31pm UTC](https://www.ruby-forum.com/t/autotest-unit-diff-and-color-output/78800/6 "2006-12-10T21:31:58Z")

</div>

On 12/8/06, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The easiest way to do this is to write (or find) a color-capable diff  
> executable. This way you don’t have to change unit\_diff, just your  
> path.

Thanks for the responses everyone. Some quick google searches didn’t  
turn up any drop in color replacements for diff, though I did find it  
as a feature request submitted for diff, but way to recently for any  
code to be written yet.

- Rob

---

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [December 11, 2006, 2:02am UTC](https://www.ruby-forum.com/t/autotest-unit-diff-and-color-output/78800/7 "2006-12-11T02:02:29Z")

</div>

On 12/10/06, Martin DeMello [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> License: GPL-2
> 
> ```
> 
> martin

Thanks, apparently I should’ve searched on one word instead of “color  
diff”. 🙂

- rob

---

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [December 11, 2006, 11:19am UTC](https://www.ruby-forum.com/t/autotest-unit-diff-and-color-output/78800/8 "2006-12-11T11:19:15Z")

</div>

On 12/11/06, Rob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 12/10/06, Martin DeMello [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > $ esearch colordiff  
> > [Results for search key : colordiff]  
> > [Applications found : 1]
> 
> Thanks, apparently I should’ve searched on one word instead of “color diff”. 🙂

It’s easier when you know it’s there 🙂 I found it the first time by  
searching for ‘diff’ in the portage tree

martin
