Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
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?

Replies are listed 'Best First'.
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?
[ambrus]: I think the minimal implementation here is just a timer and io function, plus pushing to the @REGISTRY.
[Corion]: ambrus: Yeah, at least that's what I think I'll start with. A timer and potentially the dummy IO function that never is ready
[ambrus]: That only lets you invoke the main loop from Prima->run;, not from AnyEvent->condvar- >recv; but that shouldn't matter

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2016-12-08 12:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (141 votes). Check out past polls.