Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Problem printing in colors

by hotshot (Prior)
on Apr 28, 2005 at 16:09 UTC ( #452373=perlquestion: print w/ replies, xml ) Need Help??
hotshot has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I'm trying to use colors in my application (perl 5.8.0 on Linux) and have some difficulties combining it with printf(). Example:
# when I perform: printf "\n\t%-25s| %-25s\n", 'Filename', 'Comment'; # I get on STDOUT Filename Comment # please note the spacin +g # when I perform printf "\n\t%-25s| %-25s\n", RED . 'Filename' . RESET, UNDERLINE . 'Co +mment' . RESET; # the same spacing as above # I get on STDOUT Filename Comment # each underlined, but the spacing + is changed somehow
Why adding the underline constant messes with my spacing?

Comment on Problem printing in colors
Download Code
Re: Problem printing in colors
by salva (Abbot) on Apr 28, 2005 at 16:15 UTC
    because perl knows nothing about ANSI escape sequences, it just see them as normal characters and count them inside %-25s formats.

    Insert them with additional formaters, i.e:

    printf "\n\t%s%-25s%s| %s%-25s%s\n", RED, 'Filename', RESET, UNDERLINE +, 'Comment', RESET;
Re: Problem printing in colors
by blazar (Canon) on Apr 28, 2005 at 16:35 UTC
    Why adding the underline constant messes with my spacing?
    Because the "underline constant" is an ANSI sequence, which in turn is nothing but a string and perl -or better: sprintf()- takes its lenght into account, but then its length "disappears" when it is interpreted by the terminal.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2014-12-28 05:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (178 votes), past polls