如何使用 ioctl 获取终端的宽度和高度? [英] How do I get width and height of my terminal with ioctl?

查看:38
本文介绍了如何使用 ioctl 获取终端的宽度和高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要改变什么才能使这项工作发挥作用?

What do I have to change to make this work?

 #!/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;
 }

受到基督在从列到行中的回答的启发.

Inspired by tchrist's answer in From column to row.

推荐答案

这对我来说很好用:

#!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-like ioctl 的性质,所以我将它设置为 "" 只是为了安静).

The require doesn't need (and shouldn't have) the full path; TIOCGWINSZ is already defined by loading the ioctl header, and there's no sign that the destination scalar has to be initialized to the right size (although if it's not initialized at all, perl throws a warning because it doesn't seem to recognize the special read-like nature of ioctl, so I set it to "" just to quiet that).

这篇关于如何使用 ioctl 获取终端的宽度和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆