fANSI: make your terminal a little fancy

Library and modules to make using ANSI escape sequences easier and more intuitive to use.

Installation

Either directly: (not yet possible)

[sudo] gem install fansi

or from the git repository:

<clone this repo>
bundle install
rake install

Usage

Can be used as is, or included into Object. Most of the methods take blocks but not arguments. The block is expected to return a string.

require 'fansi'
include FANSI # so you can use bold{} instead of FANSI.bold{}

FANSI.red { "a piece of red string" }
 # => "\e[31ma piece of red string\e[39m"

FANSI.bold { "a daring length of twine" }
 # => "\e[1ma daring length of twine\e22m"

FANSI.underline { FANSI.magenta { "sheer frivolity" } + "!" }
 # => "\e[4m\e[35msheer frivolity\e[39m!\e[24m"

FANSI.on_cyan { FANSI.cyan { "incognito" } }
 # => "\e[46m\e[36mincognito\e[39m\e[49m"

Try this out in irb

require 'fansi'
include FANSI

def trick
  puts "mwuhahahahaahahahaha :)"
  print up
  20.times {
    sleep 0.05
    print ?.
  }
  print up + left(28)
  10.times {
    sleep 0.05
    print ?*
  }
  print down
  puts right(12) + ?P
end

trick

This one too

require 'fansi'
include FANSI

def termite
  17.times { puts ?=*40 }
  [
    up + right(18),
    up,
    left + up,
    left + up,
    left(2) + up,
    left(2) + up,
    left(3) + up,
    left(3) + up,
    left(2) + up,
    left + up,
    up,
    right + up(2),
    right + up,
    right(2) + up,
    right(2) + up,
    right(2),
    right,
    right(2) + down,
    right(2) + down,
    right + down,
    right + down,

    right + up,
    right + up,
    right(2) + up,
    right(2) + up,
    right,
    right(2),
    right(2) + down,
    right(2) + down,
    right + down,
    right + down(2),
    down,
    left + down,
    left(2) + down,
    left(3) + down,
    left(3) + down,
    left(2) + down,
    left(2) + down,
    left + down,
    left + down,

    down + right(18)
  ].each { |whim|
    sleep 0.05
    print left + whim + ?-
  }
  :xox
end

termite

The full range of colours (each with their own on_ counterpart):

black, red, green, yellow, blue, magenta, cyan, white

Note that the actual appearance of the colours depends on the palate settings of your terminal. Decorative bits:

bold, underline, blink, negative

And to change something you've already said:

left(n), right(n), up(n), down(n), insert, cursorless

And of course, you can halt the hilarity with a simple

FANSI.disable_fansi

which makes the termite very depressed.

Requires

fANSI needs all this stuff. Most of these are gems that will install themselves automatically when you install the fansi gem.

Acknowledgement

Thanks to these sources used during development

License

Copyright © 2011 Carl Suster

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.