Help: Characters with colors

A

Amy Lee

Hello,

How to modify the color of characters when I display them?

Thank you very much~

Regards,

Amy
 
M

magloca

Amy Lee @ Wednesday 20 August 2008 15:00:
Hello,

How to modify the color of characters when I display them?

Thank you very much~

Regards,

Amy

In a terminal? Term::ANSIColor. Somewhere else? You'll have to be more
specific.

m.
 
A

Amy Lee

Amy Lee @ Wednesday 20 August 2008 15:00:


In a terminal? Term::ANSIColor. Somewhere else? You'll have to be more
specific.

m.
Yes, thank you very much.

Amy
 
A

Amy Lee

Yes, thank you very much.

Amy
Anyway, there's something I feel a little confused. I find that this
module is for 'print', when I use it on 'die' function it's still working
but will output a line like this:

at ./hwinfo.pl line 17, <KER_VER> line 1.

My codes:

use strict;
use warnings;
use Term::ANSIColor qw:)constants);
$Term::ANSIColor::AUTORESET = 1;

open KER_VER, "<", "/root/version";
while (<KER_VER>)
{
unless (/\s+2.4.\d+/)
{
die BOLD RED "Your kernel version is not 2.4.x.\n"
}
}


I hope clean this internal output by perl, how could I do that?

Thank you very much~

Amy
 
L

Lars Eighner

the lovely said:
open KER_VER, "<", "/root/version";
while (<KER_VER>)
{
unless (/\s+2.4.\d+/)
{
die BOLD RED "Your kernel version is not 2.4.x.\n"
}
}

I hope clean this internal output by perl, how could I do that?
Thank you very much~

You can write ansi codes directly, but in spite of ansi being
more or less a standard, there are serious portability problems.
Some terminals are not ansi capable and others may be quirky.

--
Lars Eighner <http://larseighner.com/> (e-mail address removed)
"I believe in God and I believe in free markets,"
-Kenneth Lay, CEO for the now defunct Enron, whose loss of some 50
billion dollars represents the largest corporate bankruptcy in the
history of the US.
 
J

Jürgen Exner

Amy Lee said:
How to modify the color of characters when I display them?

That depends _VERY_ on much how and where you display them. "perldoc -q
color"
How do I print something out in color?
may help get you started.

jue
 
S

Sherm Pendley

Amy Lee said:
How to modify the color of characters when I display them?

It depends on how you're displaying them. Are you writing a web app, a
GUI app, an app that runs in a terminal window, etc.?

sherm--
 
A

Amy Lee

That depends _VERY_ on much how and where you display them. "perldoc -q
color"
How do I print something out in color?
may help get you started.

jue
Thank you very much. Anyway, when I want to search some keywords, could I
use "perldoc -q <keyword>"?

Best Regards,

Amy
 
A

Amy Lee

You can write ansi codes directly, but in spite of ansi being
more or less a standard, there are serious portability problems.
Some terminals are not ansi capable and others may be quirky.
Thank you, but it seems that it's for 'print', not for 'die'. How do I
handle with color when I use 'die'?

Regards,

Amy
 
B

Brian Helterlilne

Amy said:
Thank you, but it seems that it's for 'print', not for 'die'. How do I
handle with color when I use 'die'?

Read the documentation for the other interface - colored(). It returns a
string with the codes embedded.

die colored( "Your kernel version is not 2.4.x.\n", 'bold red' );
 
M

magloca

Amy Lee @ Wednesday 20 August 2008 16:53:
Thank you, but it seems that it's for 'print', not for 'die'. How do I
handle with color when I use 'die'?

How about (untested):

import Term::ANSIColor;
# ...interesting stuff happens...
die colored ("Your kernel version is not 2.4.x.", "bold red");

m.
 
M

magloca

magloca @ Wednesday 20 August 2008 19:01:
How about (untested):

import Term::ANSIColor;
# ...interesting stuff happens...
die colored ("Your kernel version is not 2.4.x.", "bold red");

Hmm... That should be 'use Term::ANSIColor', of course... Contamination
from other languages. Sorry.

m.
 
T

Tad J McClellan

[ snip: asking a FAQ ]
when I want to search some keywords, could I
use "perldoc -q <keyword>"?


Have you seen the Posting Guidelines that are posted here frequently?

It covers "perldoc -q", along with about a dozen or so other tips on
how to get answers to Perl questions.
 
A

Amy Lee

Read the documentation for the other interface - colored(). It returns a
string with the codes embedded.

die colored( "Your kernel version is not 2.4.x.\n", 'bold red' );
Thank you, I have solved it.

Amy
 
L

Lars Eighner

Thank you, but it seems that it's for 'print', not for 'die'. How do I
handle with color when I use 'die'?

Exactly the same. Again I warn you, this is not at all portable and
may or may not work on your ANSI-enabled terminal and will not work
on your ANSI-ignorant terminal:

============ tear here ===============
#!/usr/bin/perl

unless ( 1 == 0 ) { die ("\e[1;41;37m Gosh numbers still work\e[0m\n") }

============ tear here ================


The point of the module is to determine the terminal capabilities
and alter the string accordingly. In a non-ANSI terminal, the string
will be printed as you see it above. The ANSI codes do not contribute
to clarity in a non-ANSI terminal. I have no idea how or whether the
module can be made to work with die, but if your script is always going
to run in an environment you are certain of, that's how you get color.


Okay, okay, you made me read the man page. Here is the real way
to do it:

============ tear here ==========================
#!/usr/bin/perl

use Term::ANSIColor;

$boldred = color('bold red');
$reset = color('reset');

unless ( 1 == 0 ) { die ("${boldred}Gosh numbers still work$reset") }

============= tear here ==========================

Evidently you cannot set attributes on the fly with die, but you
can save them as variables, for use as needed.
 
P

Peter J. Holzer

[...]

Read the documentation for the other interface - colored(). It returns a
string with the codes embedded.

die colored( "Your kernel version is not 2.4.x.\n", 'bold red' );

That has the same problem as Amys version. You may want

die colored( "Your kernel version is not 2.4.x.", 'bold red' ) . "\n";

hp
 
S

Sherm Pendley

Peter J. Holzer said:
That has the same problem as Amys version. You may want

die colored( "Your kernel version is not 2.4.x.", 'bold red' ) . "\n";

Lack of a trailing newline isn't a problem here. In fact, you get more
information (the line number of the error) if the string you pass to
die() doesn't end in a newline.

Example:

admins-macintosh:~ sherm$ perl -e 'die "Ouch!"'
Ouch! at -e line 1.
admins-macintosh:~ sherm$ perl -e 'die "Ouch!\n"'
Ouch!

sherm--
 
S

Sherm Pendley

Sherm Pendley said:
Lack of a trailing newline isn't a problem here. In fact, you get more
information (the line number of the error) if the string you pass to
die() doesn't end in a newline.

Example:

admins-macintosh:~ sherm$ perl -e 'die "Ouch!"'
Ouch! at -e line 1.
admins-macintosh:~ sherm$ perl -e 'die "Ouch!\n"'
Ouch!

On further thought, you're right - that *was* Amy's problem. The const
subs were appending the ANSI color codes *after* the newline in the
message she used, so die() was appending information she didn't
want. Brian's version would do the same thing, with the same result.

Yours works by adding the newline *after* the color codes are added to
the message, so the newline is still the last character in the string
when its passed to die().

sherm--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top