Help: Characters with colors

Discussion in 'Perl Misc' started by Amy Lee, Aug 20, 2008.

  1. Amy Lee

    Amy Lee Guest

    Hello,

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

    Thank you very much~

    Regards,

    Amy
     
    Amy Lee, Aug 20, 2008
    #1
    1. Advertising

  2. Amy Lee

    magloca Guest

    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.
     
    magloca, Aug 20, 2008
    #2
    1. Advertising

  3. Amy Lee

    Amy Lee Guest

    On Wed, 20 Aug 2008 15:16:36 +0200, magloca wrote:

    > 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.

    Yes, thank you very much.

    Amy
     
    Amy Lee, Aug 20, 2008
    #3
  4. Amy Lee

    Amy Lee Guest

    On Wed, 20 Aug 2008 21:49:44 +0800, Amy Lee wrote:

    > On Wed, 20 Aug 2008 15:16:36 +0200, magloca wrote:
    >
    >> 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.

    > 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
     
    Amy Lee, Aug 20, 2008
    #4
  5. Amy Lee

    Lars Eighner Guest

    In our last episode, <>, the lovely
    and talented Amy Lee broadcast on comp.lang.perl.misc:

    > 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/>
    "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.
     
    Lars Eighner, Aug 20, 2008
    #5
  6. Amy Lee <> wrote:
    >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
     
    Jürgen Exner, Aug 20, 2008
    #6
  7. Amy Lee <> writes:

    > 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--

    --
    My blog: http://shermspace.blogspot.com
    Cocoa programming in Perl: http://camelbones.sourceforge.net
     
    Sherm Pendley, Aug 20, 2008
    #7
  8. Amy Lee

    Amy Lee Guest

    On Wed, 20 Aug 2008 14:40:47 +0000, Jürgen Exner wrote:

    > Amy Lee <> wrote:
    >>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

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

    Best Regards,

    Amy
     
    Amy Lee, Aug 20, 2008
    #8
  9. Amy Lee

    Amy Lee Guest

    On Wed, 20 Aug 2008 14:40:19 +0000, Lars Eighner wrote:

    > In our last episode, <>, the lovely
    > and talented Amy Lee broadcast on comp.lang.perl.misc:
    >
    >> 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.

    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
     
    Amy Lee, Aug 20, 2008
    #9
  10. Amy Lee wrote:
    > On Wed, 20 Aug 2008 14:40:19 +0000, Lars Eighner wrote:
    >
    >> In our last episode, <>, the lovely
    >> and talented Amy Lee broadcast on comp.lang.perl.misc:
    >>
    >>> 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.

    > 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' );

    --
    -brian
     
    Brian Helterlilne, Aug 20, 2008
    #10
  11. Amy Lee

    magloca Guest

    Amy Lee @ Wednesday 20 August 2008 16:53:

    > On Wed, 20 Aug 2008 14:40:19 +0000, Lars Eighner wrote:
    >
    >> In our last episode, <>, the
    >> lovely and talented Amy Lee broadcast on comp.lang.perl.misc:
    >>
    >>> 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.

    > 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.
     
    magloca, Aug 20, 2008
    #11
  12. Amy Lee

    magloca Guest

    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.
     
    magloca, Aug 20, 2008
    #12
  13. Amy Lee <> wrote:

    [ 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.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Aug 21, 2008
    #13
  14. Amy Lee

    Amy Lee Guest

    On Wed, 20 Aug 2008 09:47:46 -0700, Brian Helterlilne wrote:

    > Amy Lee wrote:
    >> On Wed, 20 Aug 2008 14:40:19 +0000, Lars Eighner wrote:
    >>
    >>> In our last episode, <>, the lovely
    >>> and talented Amy Lee broadcast on comp.lang.perl.misc:
    >>>
    >>>> 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.

    >> 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' );

    Thank you, I have solved it.

    Amy
     
    Amy Lee, Aug 21, 2008
    #14
  15. Amy Lee

    Lars Eighner Guest

    In our last episode, <>, the lovely
    and talented Amy Lee broadcast on comp.lang.perl.misc:

    > On Wed, 20 Aug 2008 14:40:19 +0000, Lars Eighner wrote:


    >> In our last episode, <>, the lovely
    >> and talented Amy Lee broadcast on comp.lang.perl.misc:
    >>
    >>> 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.


    > 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.

    --
    Lars Eighner <http://larseighner.com/>
    War on Terrorism: The Difference Between Us and Them
    "We should invade their countries, kill their leaders and
    convert them to Christianity." -- Ann Coulter
     
    Lars Eighner, Aug 21, 2008
    #15
  16. On 2008-08-20 16:47, Brian Helterlilne <> wrote:
    > Amy Lee wrote:
    >> On Wed, 20 Aug 2008 14:40:19 +0000, Lars Eighner wrote:
    >>> In our last episode, <>, the lovely
    >>> and talented Amy Lee broadcast on comp.lang.perl.misc:
    >>>
    >>>> 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"
    >>>> }
    >>>> }

    [...]
    >
    > 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
     
    Peter J. Holzer, Aug 24, 2008
    #16
  17. "Peter J. Holzer" <> writes:

    > On 2008-08-20 16:47, Brian Helterlilne <> wrote:
    >>
    >> 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";


    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--

    --
    My blog: http://shermspace.blogspot.com
    Cocoa programming in Perl: http://camelbones.sourceforge.net
     
    Sherm Pendley, Aug 24, 2008
    #17
  18. Sherm Pendley <> writes:

    > "Peter J. Holzer" <> writes:
    >
    >> On 2008-08-20 16:47, Brian Helterlilne <> wrote:
    >>>
    >>> 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";

    >
    > 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--

    --
    My blog: http://shermspace.blogspot.com
    Cocoa programming in Perl: http://camelbones.sourceforge.net
     
    Sherm Pendley, Aug 24, 2008
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    818
  2. Fat Sam

    Help with Wild West Colors

    Fat Sam, Nov 12, 2004, in forum: HTML
    Replies:
    4
    Views:
    2,896
    nice.guy.nige
    Nov 14, 2004
  3. omegaman
    Replies:
    1
    Views:
    592
    omegaman
    Sep 21, 2004
  4. AAaron123

    TreeNode colors come from anchor colors

    AAaron123, Aug 7, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    561
    darrel
    Aug 7, 2008
  5. ZelluX
    Replies:
    3
    Views:
    1,958
    Peter Otten
    Dec 1, 2008
Loading...

Share This Page