Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: using colors with print()

by thunders (Priest)
on Jun 19, 2004 at 22:08 UTC ( #368213=note: print w/ replies, xml ) Need Help??


in reply to Re: using colors with print()
in thread using colors with print()

very nice, here's the same program in perl

use constant RESET => 0; use constant BRIGHT => 1; use constant DIM => 2; use constant UNDERLINE => 3; use constant BLINK => 4; use constant REVERSE => 7; use constant HIDDEN => 8; use constant BLACK => 0; use constant RED => 1; use constant GREEN => 2; use constant YELLOW => 3; use constant BLUE => 4; use constant MAGENTA => 5; use constant CYAN => 6; use constant WHITE => 7; sub textcolor($$$); print textcolor( RESET, RED, BLACK ), "In color\n", textcolor( RESET, WHITE, BLACK ); sub textcolor($$$) { my ( $attr, $fg, $bg ) = @_; #Command is the control command to the terminal my $command = sprintf( "%c[%d;%d;%dm", 0x1B, $attr, $fg + 30, $bg ++ 40 ); return sprintf( "%s", $command ); }


Comment on Re^2: using colors with print()
Download Code
Re^3: using colors with print()
by pbeckingham (Parson) on Jun 19, 2004 at 23:56 UTC

    I believe there is a need to print the newline character after the reset, not before, or in other words, do not let color attributes span unnecessary lines, and those last lines dealing with $command are redundant.

    sub textcolor ($$$); print textcolor (RESET, RED, BLACK), 'In color', textcolor (RESET, WHITE, BLACK), "\n"; sub textcolor ($$$) { my ($attr, $fg, $bg) = @_; sprintf "\e[%d;%d;%dm", $attr, $fg + 30, $bg + 40; }

      Actually the ANSI escapes span lines so newline position is really immaterial (except from a neatness point of view). In fact AFAIK they remain active until reset, and this status outlives the life of your program as you are effectively manipulating defaults on your term.

       
      $ cat col.pl
      #!/usr/bin/perl
      
      # default black background
      use constant BLUE => "\e[0;34;40m";
      use constant RED  => "\e[0;31;40m";
      use constant DEFAULT  => "\e[0;37;40m";
      
      print "The flag is:
      ", RED, "
      red
      ", DEFAULT, "
      white &
      ", BLUE,"
      blue.
      
      ANSI color escapes
      will span", DEFAULT, " at least with
      putty as the terminal emulation.
      ", BLUE;
      
      $ ./col.pl
      The flag is:
      
      red
      
      white &
      
      blue.
      
      ANSI color escapes
      will span at least with
      putty as the terminal emulation.
      $
      $ echo Oops forgot to reset blue mode
      Oops forgot to reset blue mode
      $ perl -e 'print "\e[0;37;40m"';
      $ echo Fixed....
      Fixed....
      
      

      cheers

      tachyon

        Oh, you're absolutely right, the sequences do span lines, and indeed should, but I had problems on Solaris 8/xterm when I relied on this. Perhaps I shouldn't have even mentioned this.

        I will mention though, that the longer those sequences remain active, the greater the chance of the userr hitting Crtl-C and having their terminal in need of an \e[0m.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://368213]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2015-07-04 02:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls