Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 993
Jean Avatar asked Nov 21 '25 16:11

Jean


1 Answers

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') )
like image 68
mob Avatar answered Nov 23 '25 08:11

mob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!