Jean
Jean

Reputation: 22725

Perl sprintf formatting of colored output

How do I generate fixed width output in this statement ?

use Term::ANSIColor;
printf("%s",sprintf("[%8s]",colored(sprintf("\$%0.2f",$Price),'red')))

The %8s doesn't have any effect in this statement. Are there any color conscious format specifiers ?

Upvotes: 2

Views: 1869

Answers (1)

mob
mob

Reputation: 118645

Sure it does, it's just that the codes that change the color on your terminal also have a width, and so the string you pass to the first sprintf call is already more than 8 characters. Try it with

sprintf("[%18s]", ...

and you'll see an affect.

But the width of terminal codes is esoteric, so you're better off moving the color coding outside of the fixed-width formattng.

printf("[%s]", colored( sprintf("%8s", sprintf("\$%0.2f",$Price) ),'red') )

Upvotes: 11

Related Questions