Sha256: 7f8c98f5f9feee9959d1fb7d3fb0c57162492d72c49a302e3ee44b440b6bc17c

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

= ANSI::Code

Require the library.

  require 'ansi/code'

ANSI::Code can be used as a functions module.

  str = ANSI::Code.red + "Hello" + ANSI::Code.blue + "World"
  str.assert == "\e[31mHello\e[34mWorld"

If a block is supplied to each method then yielded value will
be wrapped in the ANSI code and clear code. 

  str = ANSI::Code.red{ "Hello" } + ANSI::Code.blue{ "World" }
  str.assert == "\e[31mHello\e[0m\e[34mWorld\e[0m"

More conveniently the ANSI::Code module extends ANSI itself.

  str = ANSI.red + "Hello" + ANSI.blue + "World"
  str.assert == "\e[31mHello\e[34mWorld"

  str = ANSI.red{ "Hello" } + ANSI.blue{ "World" }
  str.assert == "\e[31mHello\e[0m\e[34mWorld\e[0m"

In the appropriate context the ANSI::Code module can also be
included, making its methods directly accessible.

  include ANSI::Code

  str = red + "Hello" + blue + "World"
  str.assert == "\e[31mHello\e[34mWorld"

  str = red{ "Hello" } + blue{ "World" }
  str.assert == "\e[31mHello\e[0m\e[34mWorld\e[0m"

Along with the single font colors, the library include background colors.

  str = on_red + "Hello"
  str.assert == "\e[41mHello"

As well as combined color methods.

  str = white_on_red + "Hello"
  str.assert == "\e[37m\e[41mHello"

In addition the library offers an extension to String class
called #ansi, which allows some of the ANSI::Code methods
to be called in a more object-oriented fashion.

  str = "Hello".ansi(:red) + "World".ansi(:blue)
  str.assert == "\e[31mHello\e[0m\e[34mWorld\e[0m"

The ANSI::Code module supports most standard ANSI codes, though
not all platforms support every code, so YMMV.

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ansi-1.2.5 qed/01_ansicode.rdoc
ansi-1.2.4 qed/01_ansicode.rdoc