2

我必须改变什么才能完成这项工作?

 #!/usr/bin/perl
 use 5.012;
 use warnings;
 require "/usr/lib/perl5/vendor_perl/5.12.1/x86_64-linux-thread-multi/sys/ioctl.ph";
 my ($rows, $cols, $xpix, $ypix);

 my $winsize = "\0" x 8;
 my $TIOCGWINSZ = 0x40087468;  # should be require sys/ioctl.pl
 if (ioctl(STDOUT, $TIOCGWINSZ, $winsize)) {
     ($rows, $cols, $xpix, $ypix) = unpack('S4', $winsize);
 } else {
     say "something didn't work" and exit;
 }

tchrist 在From column to row中的回答启发。

4

5 回答 5

3

要获取行数和列数,我这样做:

#!/usr/bin/perl
use strict;
use warnings;

my $cols = 80;
my $rows = 24;
for (`stty -a`) {
    /columns ([0-9]+);/ and do { if ($1 > 0) { $cols = $1; }};
    /rows ([0-9]+);/    and do { if ($1 > 0) { $rows = $1; }};
}
print "cols=$cols\trows=$rows\n";
于 2010-11-26T15:29:24.657 回答
2

为什么不直接使用Term::Size?这使用了ioctl()方法,包裹在漂亮整洁的 XS 代码中,所有这些都可以从 Perl 中使用。

于 2012-04-21T23:24:31.503 回答
2

Term::ReadKey具有用于检索终端大小的内置方法,它支持包括 Windows 在内的一系列不同终端。考虑到CPAN 上使用此模块的模块数量之多,您可能已经安装了它。

#!/usr/bin/perl -w
use strict;
use Term::ReadKey   qw/ GetTerminalSize /;

my @winsize = &GetTerminalSize(\*STDOUT);
my ($cols, $rows, $xpix, $ypix) = @winsize;
print "cols:$cols rows:$rows xpix:$xpix ypix:$ypix\n";
于 2015-07-10T19:16:44.660 回答
1

这对我来说很好:

#!perl

use strict;
use warnings;

require 'sys/ioctl.ph';

my $winsize = ""; # Silence warning
if (ioctl(STDOUT, TIOCGWINSZ() , $winsize)) {
        my ($rows, $cols, $xpix, $ypix) = unpack 'S4', $winsize;
        print join ":", $rows, $cols, $xpix, $ypix;
        print "\n";
} else {
        warn "Something didn't work.\n";
}

require 不需要(也不应该有)完整路径;TIOCGWINSZ 已经通过加载 ioctl 标头进行了定义,并且没有迹象表明必须将目标标量初始化为正确的大小(尽管如果根本没有初始化,perl 会抛出警告,因为它似乎无法识别特殊的read-喜欢 ioctl 的性质,所以我将它设置为“”只是为了安静)。

于 2010-11-26T19:27:27.093 回答
0

你将有另一种方式要求 C 告诉你价值TIOCGWINSZ是什么。还不如让它告诉你另一个论点的大小,当你在它的时候。

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <termios.h>

int
main(argc, argv)
    int   argc;
    char *argv[];
{
    struct winsize mywinsize;
    int ttyfd;

    if ((ttyfd = open("/dev/tty", O_RDWR|O_NOCTTY)) == -1) {
        perror("open /dev/tty");
        exit(-1);
    }

    if (ioctl(ttyfd, TIOCGWINSZ, &mywinsize) == -1) {
        perror("ioctl TIOCGWINSZ");
        exit(-1);
    }

    printf("TIOCGWINSZ %#08lx\n",           TIOCGWINSZ             );
    printf("sizeof struct winsize %lu\n",   sizeof(struct winsize) ); 
    printf("rows %d\n",                     mywinsize.ws_row       );
    printf("cols %d\n",                     mywinsize.ws_col       );

    if (fclose(stdout) == EOF) {
        perror("close stdout");
        exit(-1);
    }

    exit(0);
}

也许某个善良的灵魂可能会告诉你如何Inline::C为你包装它,但与此同时,这应该就足够了。请注意,这是一个可移植的程序,因为它可以在 ̲b̲o̲t̲h̲ 种系统上运行:

☺ BSD ☺</h2>
    OpenBSD% cc getwinsz.c && a.out
    TIOCGWINSZ 0x40087468
    sizeof struct winsize 8
    rows 81
    cols 166

    Darwin% cc getwinsz.c && a.out
    TIOCGWINSZ 0x40087468
    sizeof struct winsize 8
    rows 37
    cols 126

☹ SysV ☹</h2>
   Slolaris% cc getwinsz.c && a.out
   TIOCGWINSZ 0x005468
   sizeof struct winsize 8
   rows 37
   cols 126

   Leenooxe% cc getwinsz.c && a.out
   TIOCGWINSZ 0x005413
   sizeof struct winsize 8
   rows 37
   cols 126

于 2010-11-26T16:09:45.050 回答