Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Problem printing in colors

by hotshot (Prior)
on Apr 28, 2005 at 16:09 UTC ( [id://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?

Replies are listed 'Best First'.
Re: Problem printing in colors
by salva (Canon) 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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-24 07:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found