|
|
| Problems? Is your data what you think it is? | |
| PerlMonks |
How to determine terminal size when piped data to STDIN?by mushu (Initiate) |
| on Nov 14, 2003 at 12:27 UTC ( [id://307114]=perlquestion: print w/replies, xml ) | Need Help?? |
This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.mushu has asked for the wisdom of the Perl Monks concerning the following question:
Help! I need a _reliable_ way to determine the current terminal window size when my Perl program is being piped data to it. It is being run under HP-UX 11.11 under both hpterm and xterm terminals (separately, of course) :) At issue is the fact that when I run my program with no parameters or piped-in data, I can grab the output from `stty -a` and parse it for the "rows=" and "columns=" values. HOWEVER when you are piping data to a program like this: cat passwd | ./myprog then STDIN is no longer "real", meaning that your Perl program no longer knows anything about the terminal, since its whole world is now a stream of data which has no "boundaries". If you don't believe me, try this: #!/usr/local/bin/perl -w my $stuff=`stty -a 2>/dev/null`; print "\$stuff=$stuff\n"; Now run this by itself, then run it again but cat some data to it: cat ./myprog | ./myprog and you will see that $stuff is empty when having something piped to it. Note #1: must use only standard (default) Perl installation, so no CPAN modules (such as Term::ReadKey) allowed! Note #2: curses is unavailable in this installation (don't ask...) Note #3: I don't consider environment variables to be reliable due to them being so volatile, so $LINES and $COLUMNS are out of the picture. Now, after all of these are tossed, I'm thinking that there must be a POSIX escape code to read the current cursor position, so I can set the cursor position to (200,200) and read it and if it wrapped then the window isn't that big so drop the column width down and do it again, ad nauseum. However, I cannot find it after days of searching. Also, there might be a simple POSIX escape sequence that simply reads the current cursor position, but I was unable to find that either! I sure hope someone with lots of low-level terminal experience can help me!
Back to
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||||||||||||||||