develooper Front page | perl.perl5.porters | Postings from July 2021

Pre-RFC: Width-aware (s)printf flag

Thread Next
Paul "LeoNerd" Evans
July 1, 2021 16:46
Pre-RFC: Width-aware (s)printf flag
Message ID:

  printf "%-40s : %s\n", $_->@* for @rows;

The intention is to print a nice neat table on the terminal.

This works fine in ASCII but gets all confused if any ->[0] element
contains Unicode text. While Perl will count in Unicode codepoints,
this won't help if there are combining chars (because combining chars
count as codepoints but do not consume terminal columns), or if there
are any emoji or other double-width characters (because these single
graphemes count as two columns).

I propose a new printf flag, perhaps `|`, to tell (s)printf to count
these strings by terminal width instead. Thus

  printf "%-|40s : %s\n", $_->@* for @rows;

would now print a neat table even in the presence of Weird Unicode.

If no objections I'll write up an RFC.

Paul "LeoNerd" Evans      |  |

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About