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

Hi! I maybe completely missed something here, but neither PerlMonks nor Google gives me hints on how to accomplish the following:

I'd like to get the width of console window (the one which was used to start my perl script) so that I know how long my output lines are allowed to be at maximum without introducing "forced" linebreaks by the console itself.
Say, the console window has a width of 100 characters, then I would like to know that so I can output lines no longer than, for example, 99 characters to prevent those unintended line-wrapping...
I thought there must be something in %ENV, but no go, I didn't find anything. Any ideas? I don't get any further here.

Thanks in advance, Michael

  • Comment on How to get the width of a console window?

Replies are listed 'Best First'.
Re: How to get the width of a console window?
by pbeckingham (Parson) on Jun 07, 2004 at 13:39 UTC

    I'm using the following on Solaris:

    use Term::ANSIScreen qw/:screen/; my ($width, $height) = GetTerminalSize ();

Re: How to get the width of a console window?
by halley (Prior) on Jun 07, 2004 at 13:41 UTC
    Any method you choose will be dependent on the terminal you're using. The Perl Cookbook, Recipe 4.18, "words program" offers a simplistic scrap of code for Linux. It would not work on Windows. It asks for extended information on the given filehandle, which the terminal program itself generally provides.
    # not portable -- linux only sub getwinsize { my $winsize = "\0" x 8; my $TIOCGWINSZ = 0x40087468; if (ioctl(STDOUT, $TIOCGWINSZ, $winsize)) { ($rows, $cols, $xpixel, $ypixel) = unpack('S4', $winsize); } else { $cols = 80; } }

    You might do some research into termcap, which is the usual way of figuring out terminal capabilities and escape code sequences supported on a given Unix-style terminal. There are some CPAN modules which interface with the termcap facility.

    --
    [ e d @ h a l l e y . c c ]

Re: How to get the width of a console window?
by zentara (Cardinal) on Jun 07, 2004 at 15:25 UTC
    If you are in X, you can get it from xwininfo. Also if in X, the X11::Protocol module may help.

    Here are a couple of other snippets which may( or may not :-) ) work for you :

    #!/usr/bin/perl use Term::ReadKey; ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize(); print "$wchar\t$hchar\t$wpixels\t$hpixels\n";
    #!/usr/bin/perl @x = qx(stty -a); @y = split(/;/, $x[0]); print "screen size is $y[1] $y[2]\n"; ########################################### #Also, you can specify 'size' to avoid parsing; ($rows, $cols) = split ' ', qx"stty size </dev/tty 2>/dev/null"; print "screen size is $rows x $cols\n";

    I'm not really a human, but I play one on earth. flash japh
      I was just thinking that the information should be available from stty, and was trying to figure out how to parse the output from stty, having forgotten about stty size.

      Good on you, zentara++

Re: How to get the width of a console window?
by meetraz (Hermit) on Jun 07, 2004 at 19:37 UTC
    For completeness, here's how to do it on Windows:

    use strict; use Win32::Console; my $CONSOLE = Win32::Console->new(); my ($width, $height) = $CONSOLE->Size(); # $CONSOLE->Size(80, 25); # force a console size print "Console size is $width x $height\n";

    Note that if the user resizes the window after the call to new(), the Size() method will not return the new size. If your application needs to handle resizing, it should create the Win32::Console object each time it needs to read the size.

Re: How to get the width of a console window?
by ambrus (Abbot) on Jun 07, 2004 at 20:03 UTC

    I'd use the TIOCGWINSZ ioctl too.

    A bit portable but slower is this:

    ($lines,$cols) = `stty size`=~/(\d+)\s+(\d+)/?($1,$2):(80,25);

    Whichever you use, somewhat nicer would be to first check @ENV{qw{COLUMNS LINES}}, which are usually set to the screen size. Note that bash sets this variable automatically to correct values if the checkwinsize shopt is set.

    You might also try to read the default values of the screen size from the termcap database and the TERM variable, if the other tries fail, before failing back to some hard-coded defaults. This might even help, as xterms are usually 24 high, while th elinux console is usually 25. (Update: oops, you wanted only the width, so this is stupid.) In today's world, however, when there are no more real terminals or consoles with fixes size, this loses from its significance, that's whi telnet and ssh tries to transfer the window size along with the TERMinal type.

    Note that your application might also want to recalculate the screen size if it gets a SIGWINCH. The kernel or xterm or telnet etc automatically notifies the process with this signal when the console size changes.

Re: How to get the width of a console window?
by Zaxo (Archbishop) on Jun 07, 2004 at 23:50 UTC

    There is something in %ENV, $ENV{'COLUMNS'}, if you export COLUMNS in your environment.

    After Compline,
    Zaxo