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

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

Thread Next
From:
Paul "LeoNerd" Evans
Date:
July 1, 2021 16:46
Subject:
Pre-RFC: Width-aware (s)printf flag
Message ID:
20210701174610.493e5742@shy.leonerd.org.uk
Consider

  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

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About