bcrowell2 has asked for the wisdom of the Perl Monks concerning the following question:
O Monks,
I'm looking for a completely bulletproof pure-perl way of finding the width of the terminal that will work on a default install of perl on Linux or BSD, i.e., with no dependencies at all except on modules that are included with every perl distribution. The following is the closest I've been able to come. I believe it will work on any Linux-based perl installation (because Term::ReadLine is standard, and is implemented as Term::ReadLine::Gnu), but on my FreeBSD box it didn't work until I added an optional library (either Term::ReadLine::Gnu or Term::ReadKey). Can anyone suggest any pure-perl method that will work by default on non-Gnu systems as well?
Other ideas I've thought of: The $COLUMNS shell variable is bash-specific, and isn't an environment variable, so it isn't exported via %ENV. Doing a shell command doesn't seem to work, because the spawned shell isn't associated with a terminal, and therefore doesn't have a width associated with it.
I couldn't find much documentation for Term::ReadLine::Perl, but I'm thinking maybe it's a pure-perl implementation that I might be able to steal a few lines of code out of that would accomplish what I want...? The source code is pretty lengthy, so I haven't been successful with casual attempts to figure this out.
TIA!
-Ben
sub columns { my $result; # http://search.cpan.org/~kjalb/TermReadKey/ReadKey.pm # Term::ReadKey is not a standard Perl module, and may not be instal +led. If the user resizes the terminal # while the program is running, this will correctly reflect the resi +zing. eval 'use Term::ReadKey; my ($wchar, $hchar, $wpixels, $hpixels) = G +etTerminalSize(); $result=$wchar'; # http://search.cpan.org/~nwclark/perl-5.8.8/lib/Term/ReadLine.pm # Term::ReadLine is a standard Perl module, but exists in different +implementations under the hood. On a Linux # system, it's implemented using Term::ReadLine::Gnu, which supports + get_screen_size(). On a default FreeBSD system, # however, the following won't work; you'd have to install the p5-Re +adLine-Gnu package to get support for this function. if (!$result) { eval 'use Term::ReadLine; $term = new Term::ReadLine("foo"); my ($ +r,$c)= Term::ReadLine::get_screen_size(); $result=$c'; } return $result; }
|
|---|