The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Term::ANSIColor::Print - Create and/or print strings with ANSI color markup.

SYNOPSIS

Basic usage

  use Term::ANSIColor::Print;

  my $print = Term::ANSIColor::Print->new();

  $print->green('go!');

  $print->red('stop!');

  my @words = qw( one two three );
  $print->yellow(\@words);
  $print->yellow(@words);

Color aliases

  use Term::ANSIColor::Print;

  my $print = Term::ANSIColor::Print->new(
      alias => {
          good => 'white_on_green',
          bad  => 'yellow_on_red',
      },
  );

  $print->good('happy new year!');

  $print->bad('there has been a disturbance in the force');

  # omit traling line break with trailing _

  $print->normal_( "performing activity ... " );

  if ( perform_activity() ) {

      $print->good('OK!');
  }
  else {

      $print->bad('disapointment');
  }

Checkers

  use Term::ANSIColor::Print;

  my $string = Term::ANSIColor::Print->new(
      output => 'return',
      eol    => '',
  );

  my $red   = $string->on_red('  ');
  my $white = $string->on_white('  ');

  my $red_pad = Term::ANSIColor::Print->new(
      pad    => $red,
      output => 'return',
      eol    => '',
  );

  my $white_pad = Term::ANSIColor::Print->new(
      pad    => $white,
      output => 'return',
      eol    => '',
  );

  my $checkers_a = $red_pad->normal( map { $white } ( 0 .. 5 ) );
  my $checkers_b = $white_pad->normal( map { $red } ( 0 .. 5 ) );

  my $checker_board = Term::ANSIColor::Print->new(
      pad => "\n$checkers_a\n",
  );

  $checker_board->normal( map { $checkers_b } ( 0 .. 4 ) );

HTML approximation of the output

                     
                     
                     
                     
                     
                     
                     
                     
                     

Old Glory

  use Term::ANSIColor::Print;

  my $string = Term::ANSIColor::Print->new(
      output => 'return',
      eol    => '',
  );

  my $star = $string->bold_white_on_blue('*');
  my $blue = $string->on_blue(' ');

  my $blue_pad = Term::ANSIColor::Print->new(
      output => 'return',
      pad    => $blue,
      eol    => '',
  );
  my $star_pad = Term::ANSIColor::Print->new(
      output => 'return',
      pad    => $star,
      eol    => '',
  );

  my $starbar_a = $blue_pad->normal( map { $star } ( 0 .. 5 ) );
  my $starbar_b = $star_pad->normal( map { $blue } ( 0 .. 5 ) );

  my $redbar   = $string->on_red( ' ' x 15 );
  my $whitebar = $string->on_white( ' ' x 15 );

  my $full_redbar   = $string->on_red( ' ' x 26 );
  my $full_whitebar = $string->on_white( ' ' x 26 );

  my $old_glory = Term::ANSIColor::Print->new(
      pad    => "\n",
  );

  $old_glory->normal(
      $starbar_a . $redbar,
      $starbar_b . $whitebar,
      $starbar_a . $redbar,
      $starbar_b . $whitebar,
      $starbar_a . $redbar,
      $full_whitebar,
      $full_redbar,
      $full_whitebar,
      $full_redbar,
  );

HTML approximation of the output

*   *   *   *   *   *                                          
  *   *   *   *   *                                            
*   *   *   *   *   *                                          
  *   *   *   *   *                                            
*   *   *   *   *   *                                          
                                                               
                                                               
                                                               
                                                               

Global Warming?

  use LWP::Simple qw( get );
  use Term::ANSIColor::Print;

  my $string = Term::ANSIColor::Print->new();

  my $max_temp_regex = qr{
      <td [^>]* > \s*
          <span>Max \s Temperature</span> \s*
      </td> \s*
      <td> \s*
          (?: <span [^>]* > \s* <span [^>]* >( \d+ )</span> | - )
  }xms;

  my ($month,$day) = (8,31);

  for my $year ( reverse 1927 .. 2009 ) {

      my $url = 'http://www.wunderground.com/history/airport/'
          . 'KSAN' . '/'
          . $year  . '/'
          . $month . '/'
          . $day   . '/'
          . 'DailyHistory.html?req_city=NA&req_state=NA&req_statename=NA';

      my $html = get( $url );

      if ( $html =~ $max_temp_regex ) {

          my $temp = $1 || 0;
          my $size = $temp - 65;

          my $bar = $string->black_on_yellow( '-' x $size . '> ' );

          printf "%02d/%02d/%02d : %03d°F %s\n", $month, $day, $year, $temp, $bar;
      }
  }

HTML approximation of output

08/31/2009 : 082°F -----------------> 
08/31/2008 : 077°F ------------> 
08/31/2007 : 084°F -------------------> 
08/31/2006 : 077°F ------------> 
08/31/2005 : 072°F -------> 
08/31/2004 : 077°F ------------> 
08/31/2003 : 075°F ----------> 
08/31/2002 : 081°F ----------------> 
08/31/2001 : 072°F -------> 
08/31/2000 : 075°F ----------> 
08/31/1999 : 069°F ----> 
... 

DESCRIPTION

This is a natural language way of indicating how you want your strings to look.

METHODS

Methods are all dynamic. The methods you invoke are underscore separated keywords which you take from this lexicon:

  • black

  • blink + slow or rapid

  • blue

  • bold

  • conceal

  • cyan

  • dark

  • double

  • faint

  • green

  • grey

  • intensity

  • italic

  • light_black

  • magenta

  • negative

  • no + underline or blink

  • normal + foreground or background

  • on - prefixes background spec

  • positive

  • red

  • reset

  • reveal

  • underline

  • white

  • yellow

LIMITATIONS

Not all combinations or codes may be supported on your Terminal application.

This is alpha code and is likely to have bugs. I'm happy to hear about them.

AUTHOR

Dylan Doxey, <dylan.doxey@gmail.com>

COPYRIGHT AND LICENSE

Copyright (C) 2009 by Dylan Doxey

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 597:

Non-ASCII character seen before =encoding in '%03d°F'. Assuming UTF-8