http://qs321.pair.com?node_id=452373

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.