Professional Web Applications Themes

Get Terminal Width and Height, Pure Perl - PERL Beginners

Okay, I have a terminal program I need to get the width and height for. I have a very good reason needing to do it with a base 5.8 Perl install, if at all possible. Unfortunately, that rules out the super easy Term::ReadKey CPAN module. (Side note: I do have Term::ReadKey installed and am very aware of how wonderful it is. In fact, why isn't this a standard module??? I just can't count on it being on the box this program will be run on. I'll fight that fight if I have to, but first I thought I would check ...

  1. #1

    Default Get Terminal Width and Height, Pure Perl

    Okay, I have a terminal program I need to get the width and height for.
    I have a very good reason needing to do it with a base 5.8 Perl
    install, if at all possible. Unfortunately, that rules out the super
    easy Term::ReadKey CPAN module.

    (Side note: I do have Term::ReadKey installed and am very aware of how
    wonderful it is. In fact, why isn't this a standard module??? I just
    can't count on it being on the box this program will be run on. I'll
    fight that fight if I have to, but first I thought I would check for
    another option.)

    So my question is, is there a "Pure Perl" way to fetch the terminal
    columns and rows?

    Thanks.

    James

    James Edward Gray II Guest

  2. #2

    Default RE: Get Terminal Width and Height, Pure Perl

    James Edward Gray II <jamesgrayproductions.net> wrote:
    :
    : So my question is, is there a "Pure Perl" way to fetch
    : the terminal columns and rows?


    I took a look at the source of Term::ReadKey and it
    seemed to be pure perl. Are you sure it's not? If it is,
    you could use it as a guide to write your own sub. Or,
    if you're allowed to install multiple files, you could
    include this module in your package.


    HTH,

    Charles K. Clarkson
    --
    Head Bottle Washer,
    Clarkson Energy Homes, Inc.
    Mobile Home Specialists
    254 968-8328

    Charles K. Clarkson Guest

  3. #3

    Default Re: Get Terminal Width and Height, Pure Perl

    On Dec 15, 2003, at 10:51 AM, Charles K. Clarkson wrote:
    > James Edward Gray II <jamesgrayproductions.net> wrote:
    > :
    > : So my question is, is there a "Pure Perl" way to fetch
    > : the terminal columns and rows?
    >
    >
    > I took a look at the source of Term::ReadKey and it
    > seemed to be pure perl. Are you sure it's not?
    Haven't got a clue, to tell the truth! <laughs>

    I assumed it was not, for two reasons. One, the POD says,
    "Term::ReadKey is a compiled perl module..." and that ugly compiled
    word in there scared me. And two, I can't see where the function calls
    at the beginning of GetTerminalSize() are coming from
    (termsizeoptions(), GetTermSizeVIO(), GetTermSizeGWINSZ(),
    GetTermSizeGSIZE(), GetTermSizeWin32()).

    I also just had the thought to go check the CPAN's listing. It said
    'Rdcf?' under the DSLIP column. I'm pretty sure that middle 'c' in
    there means it's written in C and Perl. I know how dangerous
    assumptions are though, especially mine, and I won't be surprised if
    I'm wrong.
    > If it is, you could use it as a guide to write your own sub. Or,
    > if you're allowed to install multiple files, you could
    > include this module in your package.
    I did try to look at the module, but as you can see, I can't tell what
    it's doing.

    Finally, I can only use the include it trick if it is indeed pure Perl,
    right?

    Thanks for all the good ideas. I did try them.

    James

    James Edward Gray II Guest

  4. #4

    Default RE: Get Terminal Width and Height, Pure Perl

    James Edward Gray II wrote:
    > On Dec 15, 2003, at 10:51 AM, Charles K. Clarkson wrote:
    >
    > > James Edward Gray II <jamesgrayproductions.net> wrote:
    > > >
    > > > So my question is, is there a "Pure Perl" way to fetch
    > > > the terminal columns and rows?
    > >
    > >
    > > I took a look at the source of Term::ReadKey and it
    > > seemed to be pure perl. Are you sure it's not?
    >
    > Haven't got a clue, to tell the truth! <laughs>
    >
    > I assumed it was not, for two reasons. One, the POD says,
    > "Term::ReadKey is a compiled perl module..." and that ugly compiled
    > word in there scared me. And two, I can't see where the
    > function calls
    > at the beginning of GetTerminalSize() are coming from
    > (termsizeoptions(), GetTermSizeVIO(), GetTermSizeGWINSZ(),
    > GetTermSizeGSIZE(), GetTermSizeWin32()).
    >
    > I also just had the thought to go check the CPAN's listing. It said
    > 'Rdcf?' under the DSLIP column. I'm pretty sure that middle 'c' in
    > there means it's written in C and Perl. I know how dangerous
    > assumptions are though, especially mine, and I won't be surprised if
    > I'm wrong.
    You're right, it's not a pure perl module. The "bootstrap" statement around
    line 240 gives it away as well.
    >
    > > If it is, you could use it as a guide to write your own sub. Or,
    > > if you're allowed to install multiple files, you could
    > > include this module in your package.
    >
    > I did try to look at the module, but as you can see, I can't
    > tell what
    > it's doing.
    It's trying to make the process platform-independent, which is rather
    tricky.

    You might be able to get away with something as simple as querying the LINES
    and COLUMNS environment variables. Or use the output from 'tput lines' and
    'tput columns'. It all depends on your platform...
    Bob Showalter Guest

  5. #5

    Default RE: Get Terminal Width and Height, Pure Perl

    >> You might be able to get away with something as simple as querying
    the
    >> LINES and COLUMNS environment variables. Or use the output from 'tput
    >> lines' and 'tput columns'. It all depends on your platform...
    Actually, its tput cols

    Steven Kreuzer
    Linux Systems Administrator
    Etagon, Inc
    W: 646.728.0656
    F: 646.728.0607
    E: [email]steven_kreuzeretagon.com[/email]
    Steven Kreuzer Guest

  6. #6

    Default Re: Get Terminal Width and Height, Pure Perl

    In article <8A7DF9E1-2F1C-11D8-B912-000A95BA45F8grayproductions.net>,
    James Edward Gray II <jamesgrayproductions.net> wrote:
    > Okay, I have a terminal program I need to get the width and height for.
    > I have a very good reason needing to do it with a base 5.8 Perl
    > install, if at all possible. Unfortunately, that rules out the super
    > easy Term::ReadKey CPAN module.
    >
    > (Side note: I do have Term::ReadKey installed and am very aware of how
    > wonderful it is. In fact, why isn't this a standard module??? I just
    > can't count on it being on the box this program will be run on. I'll
    > fight that fight if I have to, but first I thought I would check for
    > another option.)
    >
    > So my question is, is there a "Pure Perl" way to fetch the terminal
    > columns and rows?
    Would you be adverse to parsing output from a unix command?

    use strict;
    my ($rows, $cols);
    $data = `stty -a`;
    if ($data =~ /rows (\d+)\; columns (\d+)/) {
    ($rows, $cols) = ($1, $2);
    } else {
    print "No match.\n";
    }

    Of course, this is NOT pure Perl, but stty exists on most systems
    so this should be *fairly* reliable. At the very least, it may
    hold you until you can find a better solution.

    - C
    Clifton Wood Guest

  7. #7

    Thumbs up Re: Get Terminal Width and Height, Pure Perl

    Code:
    use Term::ReadKey qw(GetTerminalSize);
    
    sub get_term_cols {
        my $cols = ((GetTerminalSize())[0]); # cols = 0; lines = 1
        $cols and return $cols;
    
        # alternative:
        -f "/bin/tput" or die("/bin/tput: command not found..");
        open( my $OH, '-|', "/bin/tput cols" ) or die($!);
        chomp(($cols) = <$OH>);
        close($OH) or die($!);
        return $cols
    }
    print "Columns on your terminal: ".get_term_cols()."\n";
    Unregistered Guest

Similar Threads

  1. how to get stream width and height?
    By dsdsdsdsd in forum Macromedia Flash Flashcom
    Replies: 0
    Last Post: August 1st, 04:40 AM
  2. ActionScripting Width, Height, X, and Y ?
    By simsjr in forum Macromedia Flash Actionscript
    Replies: 3
    Last Post: March 4th, 04:37 PM
  3. Stage width &amp; height
    By migwia in forum Macromedia Flash Actionscript
    Replies: 1
    Last Post: February 11th, 11:45 AM
  4. Can't control height and width
    By Round Rectangle H & Width webforumsuser@macromedia.com in forum Macromedia Fireworks
    Replies: 3
    Last Post: October 9th, 09:31 PM
  5. GetTextExtent (get RTF string's width/height) function in Win32 Perl?
    By Russ Johnson in forum PERL Miscellaneous
    Replies: 0
    Last Post: August 18th, 04:57 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139