ANSI - A simple ANSI hash and routine.


  use ANSI;
  print ansi(qw(bold flash underline red));
  print ("Some text.\n");
  print ansi('normal');
  use ANSI '%ANSI';
  print "$ANSI{'color'}";


ANSI is a very simple module designed to provide raw ANSI codes by name so you don't have to remember them yourself.

You can use the hash %ANSI, or use the function ansi(). The advantage of the functin is that it can be given multiple arguments, and it returns a string of all the related elements of %ANSI concatenated together.

The color codes are as follows:

 Variable:        ANSI #:  What it does:
 $ANSI{'normal'}       0   Restores default color.
 $ANSI{'bright'}       1   Brighter colors.
 $ANSI{'bold'}         1   Same as $ANSI{'bright'}.
 $ANSI{'underline'}    4   Underlined text.
 $ANSI{'flash'}        5   Flashing text.
 $ANSI{'blink'}        5   Same as $ANSI{'flash'}.
 $ANSI{'reverse'}      7   Reverse video.
 $ANSI{'nounderline'} 24   No-underline.
 $ANSI{'noflash'}     25   No-flash.
 $ANSI{'noreverse'}   27   No-reverse.
 $ANSI{'black'}       30   Black foreground.
 $ANSI{'red'}         31   Red foreground.
 $ANSI{'green'}       32   Green foreground.
 $ANSI{'yellow'}      33   Yellow (or brown) foreground.
 $ANSI{'blue'}        34   Blue foreground.
 $ANSI{'magenta'}     35   Purple foreground.
 $ANSI{'cyan'}        36   Cyan foreground.
 $ANSI{'white'}       37   White (or gray) foreground.
 $ANSI{'BLACK'}       40   Black background.
 $ANSI{'RED'}         41   Red background.
 $ANSI{'GREEN'}       42   Green background.
 $ANSI{'YELLOW'}      43   Yellow (or brown) background.
 $ANSI{'BLUE'}        44   Blue background.
 $ANSI{'MAGENTA'}     45   Purple background.
 $ANSI{'CYAN'}        46   Cyan background.
 $ANSI{'WHITE'}       47   White (or gray) background.

You can get translations of the numbers (without the raw escapes) to color names or vice-versa by exporting the %COLOR_NUMBERS hash.

In addition, there's some cursor handling codes:

 $ANSI{'clear'}       Clear the screen, put cursor at the top left.
 $ANSI{'cleareol'}    Clear from the cursor position to the end of the line.
 $ANSI{'cleartoeol'}  Same as $ANSI{'cleareol'}
 $ANSI{'save'}        Save che cursor position.
 $ANSI{'restore'}     Restore the cursor to the saved position.
 ansi(up => N)        Move the cursor up N lines.         (*)
 ansi(down => N)      Move the cursor down N lines.       (*)
 ansi(right => N)     Move the cursor right N columns.    (*)
 ansi(left => N)      Move the cursor left N columns.     (*)
 ansi(forward => N)   Same as ansi(right => N)            (*)
 ansi(backward => N)  Same as ansi(left => N)             (*)
 ansi(move => I, J)   Move the cursor to row I, column J. (*)

(*) These codes aren't useful when used directly from the %ANSI hash (they're a reference). They can be used by the ansi() function with subsequent arguments. e.g.:

 ansi(move => 5, 15);  # Return the code to move to row 5, column 15.


The following will give a simple demo of ANSI colors.

 use lib qw(/location/of/this/file);
 use ANSI;
 foreach $color (qw/normal bright underline flash/)
   print ansi($color), "$color", ansi('normal');
   print "\n";
 foreach $color (qw/black red green yellow blue magenta cyan white/)
   print ansi($color), "normal $color", ansi('normal'), ", ";
   print ansi('bright', $color), "bright $color", ansi(normal), ", ";
   print ansi ( ("\U$color" eq "BLACK" ? 'white' : 'black'), "\U$color");
   print "reverse $color", ansi('normal'), "\n";


Add the following ANSI escapes:

 22 normal (how is this different from 0?)


Christian J. Robinson <>


Copyright (C) 1999, 2002, 2003 Christian J. Robinson <>

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.