# \[ANN\] ANSI v1.4.0 released

**URL:** <https://rubytalk.org/t/ann-ansi-v1-4-0-released/64051>\
**Category:** ruby-talk\
**Created:** [5 November 2011 13:50 UTC](https://rubytalk.org/t/ann-ansi-v1-4-0-released/64051 "2011-11-05T13:50:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [5 November 2011 13:50 UTC](https://rubytalk.org/t/ann-ansi-v1-4-0-released/64051/1 "2011-11-05T13:50:00Z")

</div>

ANSI 1.4.0 has been released.

The ANSI project is a collection of ANSI escape code related libraries enabling ANSI code based colorization and stylization of output. It is very nice for beautifying shell output.

\* home: [http://rubyworks.github.com/ansi](http://rubyworks.github.com/ansi)  
\* code: [http://github.com/rubyworks/ansi](http://github.com/rubyworks/ansi)  
\* docs: [http://rubydoc.info/gems/ansi](http://rubydoc.info/gems/ansi)  
\* mail: [http://groups.google.com/group/rubyworks-mailinglist](http://groups.google.com/group/rubyworks-mailinglist)

New release adds a HexDump class for colorized byte string dumps and fixes some minor cell size issues with the Table class. This release also modernizes the build config and changes the license to BSD-2-Clause.

Changes:

\* Add HexDump class.  
\* Fix cell size of tables when ANSI codes are used.  
\* Fix extra ansi codes in tables without format.  
\* Modernize build configuration.  
\* Switch to BSD-2-Clause license.

---

<div class="post-metadata">

**Author:** ![Kazuyoshi\_Tlacaelel](https://avatars.discourse-cdn.com/v4/letter/k/48db29/32.png) [@Kazuyoshi\_Tlacaelel](https://rubytalk.org/u/Kazuyoshi_Tlacaelel)\
**Post date:** [4 December 2011 02:00 UTC](https://rubytalk.org/t/ann-ansi-v1-4-0-released/64051/2 "2011-12-04T02:00:31Z")

</div>

Alternatively you can use isna which has simpler approach.

> **[ktlacaelel/isna](https://github.com/ktlacaelel/isna)**
>
> isna - Simple Ansi Library.

require 'rubygems'  
require 'isna'  
"Hello World".to\_ansi  
"Hello World".to\_ansi.red  
puts "Hello World".to\_ansi.red  
puts "Hello World".to\_ansi.underline.blue  
puts "Hello World".to\_ansi.blink.yellow  
puts "Hello World".to\_ansi.cyan  
puts "Hello World".to\_ansi.negative.cyan  
puts "Hello World".to\_ansi.green\_background.yellow  
puts "Hello World".to\_ansi.green\_background.bright.red  
puts "Hello World".to\_ansi.green\_background.red  
puts "Hello World".to\_ansi.green\_background.dark.red  
# \*bright\* and \*dark\* only work when a background is applied.  
puts "Normal String" + ' concatenated-with-a ' + "Ansi  
String".to\_ansi.green.to\_s

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Marc\_Heiler](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@Marc\_Heiler](https://rubytalk.org/u/Marc_Heiler)\
**Post date:** [4 December 2011 05:44 UTC](https://rubytalk.org/t/ann-ansi-v1-4-0-released/64051/3 "2011-12-04T05:44:55Z")

</div>

To be honest, I think ruby should include an ANSI solution in its  
stdlib.

Right now I am using the most stupid approach possible - I aliased  
CONSTANTS to the ANSI colours. Then I use them like:

puts RED+'Hello '+GREEN+' world.'

I found it to be easier for my projects to however use variable names  
that denote the colours. And often enough I then do something like:

puts 'The file '+cfile(file)+' could not be found.'

Which colourizes the file variable (which should be a file).

Other ways could be:

puts 'The file '+file.colourize+' could not be found.'

Where the colourize method somehow is told how to colourize something,  
depending on whether it is a colour or a symlink or something.

But for this, class String has to be extended and I am a bit  
conservative when modifying core classes these days. I'd wish there  
would be a way to not globally modify core classes, and instead limit  
them to a specific project.

But we really need to have one ansi solution within Ruby - I don't mind  
what way, but it's crazy to have everyone cook up their own solutions  
instead.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [4 December 2011 02:44 UTC](https://rubytalk.org/t/ann-ansi-v1-4-0-released/64051/4 "2011-12-04T02:44:11Z")

</div>

Fluid notation can be nice and simple, but it also has limitations. If you  
want to dynamically change the ansi code, it will be less elegant:

&nbsp;&nbsp;background = :green  
&nbsp;&nbsp;foreground = :red

&nbsp;&nbsp;puts "Hello  
World".to\_ansi.send("#{background}\_background").send(foreground)

Where as ANSI:

&nbsp;&nbsp;puts "Hello World".ansi("on\_#{background}", foreground)

Rather than have Yet Another ANSI gem (there are at least three others  
besides ansi) why not contribute to existing gem?

---

<div class="post-metadata">

**Author:** ![GS1](https://avatars.discourse-cdn.com/v4/letter/g/4491bb/32.png) [@GS1](https://rubytalk.org/u/GS1)\
**Post date:** [4 December 2011 12:34 UTC](https://rubytalk.org/t/ann-ansi-v1-4-0-released/64051/5 "2011-12-04T12:34:41Z")

</div>

There's also my "Col" library, created when I wanted efficient control  
over terminal colors without busting into core classes.

> **[GitHub - gsinclair/col: high-level console color formatting](https://github.com/gsinclair/col)**
>
> high-level console color formatting. Contribute to gsinclair/col development by creating an account on GitHub.

&nbsp;&nbsp;&nbsp;&nbsp;require 'col'

&nbsp;&nbsp;&nbsp;&nbsp;puts Col("Hello world!").red.bold  
&nbsp;&nbsp;&nbsp;&nbsp;puts Col("Hello world!").rb  
&nbsp;&nbsp;&nbsp;&nbsp;puts Col("Hello world!").fmt [:red, :bold]  
&nbsp;&nbsp;&nbsp;&nbsp;puts Col("Hello world!").fmt :rb

&nbsp;&nbsp;&nbsp;&nbsp;puts Col("Hello ", "world!").fmt :red, :green  
&nbsp;&nbsp;&nbsp;&nbsp;puts Col("Hello ", "world!").fmt "r,g"

&nbsp;&nbsp;&nbsp;&nbsp;puts Col("Hello ", "world!").fmt [:red, :bold], [:green, :bold]  
&nbsp;&nbsp;&nbsp;&nbsp;puts Col("Hello ", "world!").fmt "rb,gb"

&nbsp;&nbsp;&nbsp;&nbsp;puts Col("Hello ", "world!").fmt [:bold], [:cyan, :italic, :on\_white]  
&nbsp;&nbsp;&nbsp;&nbsp;puts Col("Hello ", "world!").fmt "\_b,ciow"

&nbsp;&nbsp;&nbsp;&nbsp;puts Col("Hello ", "world!").fmt [:blue, :on\_yellow], [:on\_green]  
&nbsp;&nbsp;&nbsp;&nbsp;puts Col("Hello ", "world!").fmt "b\_oy,\_\_og"

&nbsp;&nbsp;&nbsp;&nbsp;puts Col.inline( "Hello ", :red, "world!", :blue )

&nbsp;&nbsp;&nbsp;&nbsp;puts Col.inline(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Hello ", [:red, :bold, :on\_white],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"world!", :b\_oy  
&nbsp;&nbsp;&nbsp;&nbsp;)

Gavin

> **···**
>
> On Sun, Dec 4, 2011 at 1:00 PM, Kazuyoshi Tlacaelel \<kazu.dev@gmail.com\> wrote:
> 
> > Alternatively you can use isna which has simpler approach.
> > 
> > [GitHub - ktlacaelel/isna: Simple Ansi Library.](https://github.com/ktlacaelel/isna)
> > 
> > puts "Hello World".to\_ansi.red  
> > puts "Hello World".to\_ansi.underline.blue  
> > [...]

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [6 December 2011 15:34 UTC](https://rubytalk.org/t/ann-ansi-v1-4-0-released/64051/6 "2011-12-06T15:34:12Z")

</div>

There is a tendency to overly fret extensions to core classes. Used  
thoughtfully they make code more manageable and readable, and in practice  
rarely ever present a problem. Just follow some basic rules:

\* Utilize a popular library of extensions when you can.  
\* Don't override Ruby's own methods.  
\* Add only obvious and generally useful methods.  
\* Put your extensions in a separate core\_ext file/directory and document.  
\* Be conscious of your core extensions and those in your dependencies.

Maybe I will write a blog post up on the topic. In any case the ANSI gem  
only defines 4 core extensions:

\* String#ansi  
\* String#ansi!  
\* String#unansi  
\* String#unansi!

That's it.

> **···**
>
> On Sunday, December 4, 2011 12:44:55 AM UTC-5, Marc Heiler wrote:
> 
> > But for this, class String has to be extended and I am a bit  
> > conservative when modifying core classes these days. I'd wish there  
> > would be a way to not globally modify core classes, and instead limit  
> > them to a specific project.

---

<div class="post-metadata">

**Author:** ![Wayne\_Brisette](https://avatars.discourse-cdn.com/v4/letter/w/91b2a8/32.png) [@Wayne\_Brisette](https://rubytalk.org/u/Wayne_Brisette)\
**Post date:** [6 December 2011 16:02 UTC](https://rubytalk.org/t/ann-ansi-v1-4-0-released/64051/7 "2011-12-06T16:02:15Z")

</div>

I wanted to thank everybody for putting me on the right track with the array  
into a hash. That method is pretty bulletproof for what I'm doing and very fast.  
I had to do an .inverse on the hash when I was done because my keys were on the  
wrong side, but this is exactly what I needed.

Wayne

---

<div class="post-metadata">

**Author:** ![Kazuyoshi\_Tlacaelel](https://avatars.discourse-cdn.com/v4/letter/k/48db29/32.png) [@Kazuyoshi\_Tlacaelel](https://rubytalk.org/u/Kazuyoshi_Tlacaelel)\
**Post date:** [28 December 2011 23:01 UTC](https://rubytalk.org/t/ann-ansi-v1-4-0-released/64051/8 "2011-12-28T23:01:56Z")

</div>

I actually went over all of the implementations of ansi before writing  
my own. What made me go over and write a new thing is that ansi does way  
too many things besides ansi. I think that ansi should only take care of  
ansi-related features! and their extensibility should be done apart. If  
anyone wants to join gems am willing to help!

I see what you are trying to achieve there: you can do that pretty  
easily with ISNA since it is very simple and extendible!

&nbsp;&nbsp;require 'rubygems'  
&nbsp;&nbsp;require 'isna'

&nbsp;&nbsp;&nbsp;&nbsp;class String

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def ansi background, foreground  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to\_ansi.send("#{background}\_background").send(foreground).to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;puts "Hello World".ansi(:red, :green)

You can see more cool examples on how to extend it here!

> **[GitHub - ktlacaelel/isna: Simple Ansi Library.](https://github.com/ktlacaelel/isna)**
>
> Simple Ansi Library. Contribute to ktlacaelel/isna development by creating an account on GitHub.

Thomas Sawyer wrote in post #1034974:

> **···**
>
> > Fluid notation can be nice and simple, but it also has limitations. If  
> > you  
> > want to dynamically change the ansi code, it will be less elegant:
> > 
> > &nbsp;&nbsp;background = :green  
> > &nbsp;&nbsp;foreground = :red
> > 
> > &nbsp;&nbsp;puts "Hello  
> > World".to\_ansi.send("#{background}\_background").send(foreground)
> > 
> > Where as ANSI:
> > 
> > &nbsp;&nbsp;puts "Hello World".ansi("on\_#{background}", foreground)
> > 
> > Rather than have Yet Another ANSI gem (there are at least three others  
> > besides ansi) why not contribute to existing gem?
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
