ANSI v1.4.0 released

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.

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.

Alternatively you can use isna which has simpler approach.

https://github.com/ktlacaelel/isna

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

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:

background = :green
foreground = :red

puts “Hello
World”.to_ansi.send("#{background}_background").send(foreground)

Where as ANSI:

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?

On Sunday, December 4, 2011 12:44:55 AM UTC-5, Marc H. 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.

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 Sun, Dec 4, 2011 at 1:00 PM, Kazuyoshi T. [email protected]
wrote:

Alternatively you can use isna which has simpler approach.

https://github.com/ktlacaelel/isna

puts “Hello World”.to_ansi.red
puts “Hello World”.to_ansi.underline.blue
[…]

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

https://github.com/gsinclair/col

require 'col'

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

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

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

puts Col("Hello ", "world!").fmt [:bold], [:cyan, :italic, 

:on_white]
puts Col("Hello ", “world!”).fmt “_b,ciow”

puts Col("Hello ", "world!").fmt [:blue, :on_yellow], [:on_green]
puts Col("Hello ", "world!").fmt "b_oy,__og"

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

puts Col.inline(
  "Hello ",   [:red, :bold, :on_white],
  "world!",   :b_oy
)

Gavin

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

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 directory, a file 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.

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!

require ‘rubygems’
require ‘isna’

class String

  def ansi background, foreground
    to_ansi.send("#{background}_background").send(foreground).to_s
  end

end

puts “Hello World”.ansi(:red, :green)

You can see more cool examples on how to extend it here!
https://github.com/ktlacaelel/isna

Thomas S. 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:

background = :green
foreground = :red

puts “Hello
World”.to_ansi.send("#{background}_background").send(foreground)

Where as ANSI:

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?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs