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; }


In reply to bulletproof way of finding size of terminal? by bcrowell2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.