## Ruby's

# ANSI

The Ruby ANSI project is a brilliant collection of ANSI escape code related libraries enabling ANSI code based colorization and stylization of console output in a variety of formats and layouts.

Included in the library are the Code module, which is the heart of the system, a Mixin module for including color methods into your own classes, a Logger, a ProgressBar, a String subclass and more. The library also includes a Terminal module which provides information about the current output device.

Click Here to Learn More

There are a number of modules and classes provided by the ANSI package. Below is a small sampling of some of these. To get an even better understanding of the available modules and classes and their capabilities, have a look at the QED Demonstrandum.

### ANSI::Code

At the heart of all the provided libraries lies the ANSI::Code module which defines ANSI codes as constants and methods. For example:

        require 'ansi/code'

        include ANSI::Code

        red + "Hello" + " " + blue + "World" + clear

          Hello World

Or in block form...

        puts red{ "Hello" } + " " + green{ "World" }

          Hello World

Rather than include `ANSI::Code`, these methods can also be called as module methods. Either as `ANSI::Code.red` or just `ANSI.red`.

The methods defined by this module are used throughout the rest of the system.

### ANSI::ProgressBar

The `ANSI::ProgressBar` class is great to convey the progress of long running process.

        require 'ansi/progressbar'

        pbar = ANSI::Progressbar.new("Test Bar", 100)

        100.times do |i|
          sleep 0.01
          pbar.inc
        end
        pbar.finish

         Test Bar:  100% |||||||||||||||||||||||||||||||||||||||||||| Time: 00:00:11

As with the rest of the ANSI classes, ANSI codes can be used to spice up the progress bar output.

### ANSI::String

The `ANSI::String` class provides a specialized String subclass ideally suited for ANSI color manipulation.

        require 'ansi/string'

        flower1 = ANSI::String.new("Roses")
        flower2 = ANSI::String.new("Violets")

        puts flower1.red + " are red and, " + flower2.blue + " are blue."

         Roses are red, and Violets are blue.

The `ANSI::String` class is mostly a String subclass with the `ANSI::Mixin` module mixed-in.

### ANSI::Columns

The `ANSI::Columns` class provides easy way to create columnar output.

        require 'ansi/columns'

        list = %w{a b c d e f g h i j k l}

        columns = ANSI::Columns.new(list)

        puts columns.to_s(4)

          a d g j
          b e h k
          c f i l

A block can be passed to the constructor to apply ANSI codes to each entry.

### ANSI::Table

The `ANSI::Table` class makes quick work of tables.

        require 'ansi/table'

        data = [
          [ 10, 20, 30 ],
          [ 20, 10, 20 ],
          [ 50, 40, 20 ]
        ]

        table = ANSI::Table.new(data)

        puts table

        +----+----+----+
        | 10 | 20 | 30 |
        | 20 | 10 | 20 |
        | 50 | 40 | 20 |
        +----+----+----+

As with the `Columns` class, a block passed to the constructor can be used to apply ANSI codes to each cell.