Francesco wrote:
> Do you happen to know of a command-line tool that can read text lines
> from stdin and write them centered to stdout?

 This isn't strictly about this bug, but I can help you with that.
perl supports UTF-8 (but not enabled by default).  I converted your
awk program perl, with a2p, and added use ':encoding(utf8)'.  And
added some useful comments and improvements...

#!/usr/bin/perl -w
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
 if 0; #    if $running_under_some_shell;
                        # this emulates #! processing on NIH machines.
                        # (remove #! line above if indigestible)

# -wCDS can't be put on the #! line because perl5.10 complains that it's "too 
late" for them there
# Even though Linux puts the args on perl's cmdline -wCDS, other OSes might 
not, 
# so perl parses its options out of the #! line itself.  Even Linux only 
supports a single arg on #! lines...

# these two lines make perl treat any open()ed streams as UTF-8, and also 
stdin/out/err as UTF-8, like -CDS.
use open ':encoding(utf8)';
use open ':std'; # set STD* based on IN and OUT settings.  shortcut for 
binmode(...)
#Note: perl's UTF-8 doesn't depend on LANG or locales at all.  In fact,
#perl's use 'locale' is actually incompatible with it's UTF-8 support.

my $columns = 80;
# $columns = something with ioctl(stdout, TIOCGWINSZ)
$columns = $ENV{COLUMNS} if exists $ENV{COLUMNS};

while (<>) {
    chomp;      # strip record separator
    $w = int(($columns + length($_)) / 2);
    printf "%${w}s\n", $_;
}

Share and enjoy.


[EMAIL PROTECTED]:/net/llama/home/peter$ ./center.pl  < hellos  # Perl 5.10.0 
on Ubuntu Intrepid
                                  hello world
                                  hèllo wörld
                                                                    
llama]~$ ./center.pl hellos  # Perl 5.8.8 on Debian Etch
                                  hello world
                                  hèllo wörld

LANG=en_CA.utf8 gawk '{ w = int((80 + length())/2); printf "%" w "s\n", $0; }' 
hellos
                                  hello world
                                 hèllo wörld

 So using a2p and perl as an awk interpreter would give a UTF-8 awk,
although a2p doesn't claim to be a fully correct implementation of awk.

 BTW, this bug was cloned as #404980 for mawk.  I'm not sure mawk has
UTF-8 support at all.  It's regexes are just as fast whether you use
LANG=C 

-- 
#define X(x,y) x##y
Peter Cordes ;  e-mail: X([EMAIL PROTECTED] , des.ca)

"The gods confound the man who first found out how to distinguish the hours!
 Confound him, too, who in this place set up a sundial, to cut and hack
 my day so wretchedly into small pieces!" -- Plautus, 200 BC




--
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to