fachtnaroe.net
— because if it was easy, everyone would do it

  • Credits : John Sheehan and John Cottle

Coding is one of the highest art forms achieved by mankind; an idea - just an electrical impulse - is made real in an alternate electronic universe; the created idea however, does not have physical form, and yet can change the physical world; it cannot be touched, and yet may touch all mankind.

Sample ANSI usage

Copy and run this program; make changes to see how it does what it does. You can use ANSI codes to make your text programs more attractive.

#!/usr/bin/perl
use strict;

# Purpose:  Samples for using ANSI codes for pretty text
# Date:     2013
# Author:   Fachtna Roe
# Ref:      http://ascii-table.com/ansi-escape-sequences.php
#
# All ANSI character sequences start with ESC and "["
# I'll concatenate using the . operator
# Define constants to save later typing
use constant ESC => "\x1B";
use constant LeftSquare => "[";
# I'll put these together for easier use
use constant ANSI => ESC . LeftSquare;
# Erase display = esc[2J
use constant clrscr => ANSI . "2J";
# black foreground = esc[30m
use constant black => ANSI . "30m";
# red foreground = esc[31m
use constant red => ANSI . "31m";
# blue foreground = esc[34m
use constant blue => ANSI . "34m";
# yellow forground = esc[33m
use constant yellow => ANSI . "33m";
# color elements can be combined with ; between them
# yellow on blue is esc[33;44m
use constant yellow_on_blue => ANSI . "33;44m";
use constant blue_on_yellow => ANSI . "34;43m";
use constant white_on_black => ANSI . "37;40m";
use constant black_back => ANSI . "40m";
# cursor can be positioned also 0,0 is top-left
use constant top_left => ANSI . "0;0H";
# bold = esc[1m
use constant bold => ANSI . "1m";
# all attributes off =  esc[0m
use constant all_off => ANSI . "0m";

# start demo...
# Clear the screen
print clrscr;
print red, "Red: Hello World\n";
print blue, "Blue: Hello World\n";
print yellow, "Yellow: Hello World\n", black;
# pause for a bit
print "Press return to continue";
<STDIN>;
print yellow_on_blue, bold, clrscr, top_left;
print "Hello World\n";
print black, "Press return to continue";
<STDIN>;
print blue_on_yellow, bold, clrscr, top_left;
print "Hello World\n";
print black, "Press return to continue";
<STDIN>;
print white_on_black, bold, clrscr, top_left;
print "Hello World\n";
print black, "Press return to continue";
<STDIN>;
print red, black_back;
my (@text, @numbers, $count, $spaces);
push @text, "Some random-ish text";
push @text, "More of it";
push @text, "some more again";
push @numbers, "903.01";
push @numbers, "1.99";
push @numbers, "24.95";
print clrscr, top_left, "This text is NOT in columns\n";
$count = 0;
foreach (@numbers) {
  print "$count: ",$text[$count];
  print $numbers[$count];
  print "\n";
  $count++;
}
<STDIN>;
print "This text IS in columns\n";
$count = 0;
foreach (@numbers) {
  print "$count: ",$text[$count];
  my $spaces = 25-length($text[$count]);
  print ANSI . $spaces . "C";
  print $numbers[$count];
  print "\n";
  $count++;
}
<STDIN>;
print "This text IS in columns - plus correct spacing for numbers\n";
$count = 0;
foreach (@numbers) {
  print "$count: ",$text[$count];
  $spaces = 25-length($text[$count]);
  $spaces = $spaces + (10 - length($numbers[$count]));
  print ANSI . $spaces . "C";
  print $numbers[$count];
  print "\n";
  $count++;
}
<STDIN>;
print all_off, clrscr;
Last updated: 20150325-15:46