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:
Devon |
Quote:
|
Thanks for the suggestion. This will work, but looks very messy. For example -
Code:
#!/usr/bin/perl -w Code:
echo -ne "Do you wish to operate in ${GREEN}(s)${UNCOLOR}ript or ${RED}(l)${UNCOLOR}ive mode ? \n" Devon |
Quote:
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" You can probably even overload 'print' in order to do the above parsing. |
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.
|
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"); Code:
print $COLOR "$STATEMENT"; Code:
print RED "$STATEMENT"; Suggestions? Thanks for the help so far. Devon |
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 |
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 |
Quote:
Code:
print "$COLOR $STATEMENT"; Code:
print $COLOR, "$STATEMENT"; |
Quote:
|
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"; Code:
print "My lobster is ${RED}red${NO} in color."; Code:
RED='\E[31;40m' Thanks for the help. Devon |
Nice to see the easy solution was available, please don't forget to mark as SOLVED.
|
All times are GMT -5. The time now is 11:13 PM. |