LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2010, 01:53 PM   #1
DevonB
LQ Newbie
 
Registered: Dec 2009
Posts: 27

Rep: Reputation: 1
Easier way to color text in Perl like BASH?


Hello all,

I've been trying to figure out a way to more easily color text in Perl like I do on Bash on a Linux box. In bash, what I'll do is set color variables up to equal the escape sequence, then echo out with escape seqeunces to print it exactly how I want it. Typically I'll want a character or a word in a different color, not the whole line. For example
Code:
 
echo -n -e "My face is turning ${RED}red${UNCOLOR} like a lobster."
In Perl with the term::ANSIColor module, it seems to just do a line. Am I being dense? Is there a way that I can do it like I do it in BASH that's fairly easy to read after the fact? Any suggestions?

Devon
 
Old 04-08-2010, 02:03 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by DevonB View Post
Hello all,

I've been trying to figure out a way to more easily color text in Perl like I do on Bash on a Linux box. In bash, what I'll do is set color variables up to equal the escape sequence, then echo out with escape seqeunces to print it exactly how I want it. Typically I'll want a character or a word in a different color, not the whole line. For example
Code:
 
echo -n -e "My face is turning ${RED}red${UNCOLOR} like a lobster."
In Perl with the term::ANSIColor module, it seems to just do a line. Am I being dense? Is there a way that I can do it like I do it in BASH that's fairly easy to read after the fact? Any suggestions?

Devon
http://perldoc.perl.org/Term/ANSIColor.html - line numbers 17 .. 22 in the first listing. You can use 'print' without "\n" in the end.
 
Old 04-08-2010, 02:17 PM   #3
DevonB
LQ Newbie
 
Registered: Dec 2009
Posts: 27

Original Poster
Rep: Reputation: 1
Thanks for the suggestion. This will work, but looks very messy. For example -

Code:
#!/usr/bin/perl -w

    use Term::ANSIColor qw(:constants);
    $Term::ANSIColor::AUTORESET = 1;
    print "Do you wish to operate in ";
    print BOLD GREEN "(s)";
    print "cript mode or in ";
    print BOLD RED "(l)";
    print "ive mode ? \n";
That's very hard to read from a maintainability standpoint. In BASH, it be

Code:
echo -ne "Do you wish to operate in ${GREEN}(s)${UNCOLOR}ript or ${RED}(l)${UNCOLOR}ive mode ? \n"
That to me is more readable and what I'm trying to emulate, if possible.

Devon
 
Old 04-08-2010, 03:03 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by DevonB View Post
Thanks for the suggestion. This will work, but looks very messy. For example -

Code:
#!/usr/bin/perl -w

    use Term::ANSIColor qw(:constants);
    $Term::ANSIColor::AUTORESET = 1;
    print "Do you wish to operate in ";
    print BOLD GREEN "(s)";
    print "cript mode or in ";
    print BOLD RED "(l)";
    print "ive mode ? \n";
That's very hard to read from a maintainability standpoint. In BASH, it be

Code:
echo -ne "Do you wish to operate in ${GREEN}(s)${UNCOLOR}ript or ${RED}(l)${UNCOLOR}ive mode ? \n"
That to me is more readable and what I'm trying to emulate, if possible.

Devon
You can have a number of 'print' statements on in one line.

You can invent your own simplistic color management language, like

Code:
"The next hello will be red __RED__hello, but goodbye to come will be __BLUE__goodbye"
, trivially parse it and convert into what Term::ANSIColor understands.

You can probably even overload 'print' in order to do the above parsing.
 
Old 04-08-2010, 03:49 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Or you can use a subset of standard language, say, HTML (it allows coloration), any standard HTML parser and, again, convert the parser output into what Term::ANSIColor understands.
 
Old 04-09-2010, 08:38 AM   #6
DevonB
LQ Newbie
 
Registered: Dec 2009
Posts: 27

Original Poster
Rep: Reputation: 1
I went a different route and the problem is solved, but it produced a different error. Here's how I did it -

I made a subroutine that takes parameters in pairs; the color you want printed and the statement you want printed. The call of the subroutine makes it read very close to the way it did in BASH for me.

Code:
&colorprint("NO","The lobster was ","RED","red");
Here's the problem; The statement inside the subroutine of

Code:
print $COLOR "$STATEMENT";
Makes it thinks $COLOR is a filehandle, and gives an error stating that RED is not a file handle. However -

Code:
print RED "$STATEMENT";
works fine, and prints as expected. I did an ugly work around of lots of elsif's searching for all the possible color names, but I'd prefer for it to evaluate $COLOR and consider it a color selection instead of a file handle. My guess is this is a presidence problem of how it evaluates the statement from right to left, but I have no idea how to fix it? I'm way too new to perl still.

Suggestions? Thanks for the help so far.

Devon
 
Old 04-09-2010, 09:29 AM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Why not just create your own print function which translates embedded color tags to escape sequences, using regular expression substitutions, and ultimately prints the converted string?
Untested:
Code:
#! /bin/perl -w
use strict;
sub colorPrint($);

    colorPrint( "This is \${red}red\${unred}\n" );
    colorPrint( "This is \${green}green\${ungreen}\n" );
    colorPrint( "This is \${blue}blue\${unblue}\n" );
    exit 0;

sub colorPrint($){

# Escape sequences not real, just invented for demonstration
my %colorTags = (
    '${red}'   => "esc[#",
    '${unred}' => "esc]#",
    '${green}' => "esc[\$",
    '${ungreen}'=> "esc]\$",
    '${blue}'  => "esc[%",
    '${unblue}'=> "esc]%"
);

my $stringToPrint = shift;
    foreach my $color ( keys %colorTags ){
        my $escapeSeq = $colorTags{ $color };
        $color = quotemeta( $color );
        $stringToPrint =~ s/$color/$escapeSeq/g;
    }
    print $stringToPrint;
}
--- rod.
 
Old 04-09-2010, 11:55 AM   #8
DevonB
LQ Newbie
 
Registered: Dec 2009
Posts: 27

Original Poster
Rep: Reputation: 1
Thank you for the excellent suggestion, rod. I had tried something similiar to your suggestion, yet I could not figure out the escape sequences to put in place to make the colors work. All's it did was print the 'garbage' characters to the screen. With the TERM:ANSIcolors module there, it kind of lead me to believe that it didn't exist, and that printf didn't have the ability to interpret escape seqeunces like BASH does with a %b or echo does inline with -e to process escape codes. If I could get just "print $COLOR "$STATEMENT" to process $COLOR as a color name and not see it as a file handle, I'd be happy.

Devon
 
Old 04-09-2010, 12:22 PM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by DevonB View Post
Code:
print $COLOR "$STATEMENT";
Makes it thinks $COLOR is a filehandle, and gives an error stating that RED is not a file handle.
How about
Code:
print "$COLOR $STATEMENT";
or
Code:
print $COLOR, "$STATEMENT";
--- rod.

Last edited by theNbomr; 04-09-2010 at 12:23 PM.
 
Old 04-09-2010, 01:04 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by theNbomr View Post
How about
Code:
print "$COLOR $STATEMENT";
or
Code:
print $COLOR, "$STATEMENT";
--- rod.
You don't need quotes in the last example.
 
Old 04-09-2010, 01:52 PM   #11
DevonB
LQ Newbie
 
Registered: Dec 2009
Posts: 27

Original Poster
Rep: Reputation: 1
I got it gentlemen. I should have just looked at the TERM:ANSIColor module in the first place. The fomulation of the escape code I had earlier was my problem. I now have -


Code:
$RED="\e[31m";
$GREEN="\e[32m";
etc. and doing a

Code:
print "My lobster is ${RED}red${NO} in color.";
works as I want it to. BASH is different. It uses -

Code:
RED='\E[31;40m'
GREEN='\E[32;40m'
Just a slight variation.

Thanks for the help.

Devon
 
Old 04-10-2010, 12:48 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Nice to see the easy solution was available, please don't forget to mark as SOLVED.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
print text color using perl john83reuben Programming 4 12-04-2009 12:11 AM
How change text color using linux in text mode only runlevel 3? Xavius Linux - General 7 05-07-2009 02:19 AM
bash script to create text in a file or replace value of text if already exists knightto Linux - Newbie 5 09-10-2008 11:13 PM
How can I change text color in BASH evansd321 Linux - General 1 09-10-2008 04:00 AM
how change text (and background) color within the bash shell? Xavius Linux - Newbie 4 03-29-2004 02:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration