Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

"Exporting" Terminal Size to Piped Command?

by PetaMem (Priest)
on Jul 22, 2008 at 20:31 UTC ( [id://699419]=perlquestion: print w/replies, xml ) Need Help??

PetaMem has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,

I've been using Term::Size - and while hunting the problem described herein also Term::Size::Perl for detecting the size of the terminal my perl script is running in. I'm only interested in the chars (columns, rows). Everything woks fine, when you start the script in a shell: Both columns and rows are displayed correctly by both aforementioned modules.

Unfortunately, both modules fail if you have your script started from within another script through a pipe:

if (open my $cmdh, "$rcmd |") { ...
$rcmd being the name of the script that shall detect the terminal size. Unfortunately in this case, both modules return empty values for columns and rows. I'm using
my ($col, $row) = Term::Size::chars *STDOUT{IO};
to get the terminal dimensions.

Now that's of course totally unsatisfying and horrible and makes me whine. :-) Is there any way, how to pass terminal size elegantly to the piped command. (By elegantly I mean not the obvious way of putting these data on the command line - it should be transparent for the user).

edit:
Perlmonks rocks :-)
From the answers below, I've done a combined version

my ($col, $row); if (open my $tty, '<', '/dev/tty') { ($col, $row) = Term::Size::chars (defined $tty ? $tty: *STDIN{IO}) +; close $tty; }
Which seems to work pretty well in all cases where the old (STDOUT) worked before PLUS in those where it didn't. Also, this is able to cope with the </dev/null redirection.

Bye
 PetaMem
    All Perl:   MT, NLP, NLU

Replies are listed 'Best First'.
Re: "Exporting" Terminal Size to Piped Command?
by almut (Canon) on Jul 22, 2008 at 21:38 UTC

    Works for me if I use STDIN or STDERR instead of STDOUT. Those handles are still connected to the terminal the parent script is started from1, while STDOUT (in the child) is connected to the pipe (for which the used TIOCGWINSZ ioctl call is invalid).

    ___

    1 unless you've reopened them to somewhere else in the Perl code, or redirected them to/from a file in the calling shell, of course...

    Update:

    _____ rcmd.pl _____

    #!/usr/bin/perl use Term::Size; my ($col, $row) = Term::Size::chars *STDIN{IO}; print "$col x $row\n";

    _____ 699419.pl _____

    #!/usr/bin/perl my $rcmd = "./rcmd.pl"; if (open my $cmdh, "$rcmd |") { print <$cmdh>; }

    _____

    $ ./699419.pl 120 x 50 $ ./699419.pl </dev/null # doesn't work x
Re: "Exporting" Terminal Size to Piped Command?
by dave_the_m (Monsignor) on Jul 22, 2008 at 20:39 UTC
    Try using /dev/tty (untested):
    open my $tty, '<', '/dev/tty'; my ($col, $row) = Term::Size::chars (defined $tty ? $tty: *STDOUT{IO}) +;

    Dave.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://699419]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-20 13:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found