如何让终端窗口宽度是多少? [英] How to get terminal window width?

查看:746
本文介绍了如何让终端窗口宽度是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的嵌入式系统上工作 C 的Linux 。用户可以通过SSH或控制台串行电缆连接到该设备。他能够通过腻子或万亿期限做到这一点。我的问题是,他接通后,我怎么能知道他的窗口的宽度是多少?我曾尝试不同的方法,如果我模拟一个Linux的PC我的系统他们的工作,但他们的设备上工作:


  1. 的ioctl()

     结构使用winsize WS;
    的ioctl(...,TIOCGWINSZ,&安培; WS);

    此方法适用于PC,但它总是在设备上返回0。


  2. tgetnum()

      setupterm((的char *)0,1,为(int *)0);
    CGR_INT列= tgetnum(CO);

    此方法适用于电脑,但它总是返回设备上的80x24。


  3. getmaxyx()

      CGR_INT xdim;
    CGR_INT ydim;
    initscr的();
    刷新();
    getmaxyx(stdscr上,ydim,xdim);

    此方法适用于PC,但它总是在设备上返回0



解决方案

我用这个code打印一堆等号作为分隔符。适用于大多数盒/条款我试过。不知道你说的是什么设备。值得一试我猜; - )

 的#include< termios.h>
#包括LT&; SYS / ioctl.h>
#包括LT&;&stdio.h中GT;
INT主(INT ARGC,CHAR *的argv [])
{
   结构使用winsize WS;
   的ioctl(0,TIOCGWINSZ,&安培; WS);   INT I = 0;
   为(;我小于10 * ws.ws_col ++ⅰ)的printf(=);
   的printf(\\ n);
   返回0;
}

I am working on embedded system using C and linux. User can connect to the device via SSH or a console serial cable. He can do this through PuTTY or Tera Term. My question is, after he is connected, how can I know his window's width? I have tried different approaches, they work if I simulate my system on a linux pc, but none of them work on the device:

  1. ioctl()

    struct winsize ws;
    ioctl(..., TIOCGWINSZ, &ws); 
    

    This method works on pc, but it always returns 0 on the device.

  2. tgetnum()

    setupterm((char *)0, 1, (int *)0);
    CGR_INT columns = tgetnum("co");
    

    This method works on pc, but it always returns 80x24 on the device.

  3. getmaxyx()

    CGR_INT xdim;
    CGR_INT ydim;
    initscr();
    refresh();
    getmaxyx(stdscr, ydim, xdim);
    

    This method works on pc, but it always returns 0 on the device

解决方案

I use this code to print a bunch of equals signs as a divider. Works on most boxes/terms I've tried. Not sure what devices you are talking about. Worth a try I guess ;-)

#include <termios.h>
#include <sys/ioctl.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
   struct winsize ws;
   ioctl(0, TIOCGWINSZ, &ws);

   int i=0;
   for(;i<10*ws.ws_col;++i) printf("=");
   printf("\n");
   return 0;
}

这篇关于如何让终端窗口宽度是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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