在C中获得终端宽度?

问题描述 投票:81回答:7

我一直在寻找一种从我的C程序中获取终端宽度的方法。我一直想出的是:

#include <sys/ioctl.h>
#include <stdio.h>

int main (void)
{
    struct ttysize ts;
    ioctl(0, TIOCGSIZE, &ts);

    printf ("lines %d\n", ts.ts_lines);
    printf ("columns %d\n", ts.ts_cols);
}

但每次我尝试我得到

austin@:~$ gcc test.c -o test
test.c: In function ‘main’:
test.c:6: error: storage size of ‘ts’ isn’t known
test.c:7: error: ‘TIOCGSIZE’ undeclared (first use in this function)
test.c:7: error: (Each undeclared identifier is reported only once
test.c:7: error: for each function it appears in.)

这是最好的方法吗,还是有更好的方法?如果不是,我怎么能让它工作?

编辑:固定代码是

#include <sys/ioctl.h>
#include <stdio.h>

int main (void)
{
    struct winsize w;
    ioctl(0, TIOCGWINSZ, &w);

    printf ("lines %d\n", w.ws_row);
    printf ("columns %d\n", w.ws_col);
    return 0;
}
c linux terminal width
7个回答
112
投票

你考虑过使用getenv()吗?它允许您获取包含终端列和行的系统环境变量。

或者使用您的方法,如果您想查看内核看到的终端大小(更好的是终端大小调整),您需要使用TIOCGWINSZ,而不是TIOCGSIZE,如下所示:

struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

和完整的代码:

#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>

int main (int argc, char **argv)
{
    struct winsize w;
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

    printf ("lines %d\n", w.ws_row);
    printf ("columns %d\n", w.ws_col);
    return 0;  // make sure your main returns int
}

16
投票

这个例子有点冗长,但我相信它是检测终端尺寸最便携的方式。这也处理调整大小事件。

正如tim和rlbond所说,我正在使用ncurses。与直接读取环境变量相比,它保证了终端兼容性的极大改进。

#include <ncurses.h>
#include <string.h>
#include <signal.h>

// SIGWINCH is called when the window is resized.
void handle_winch(int sig){
  signal(SIGWINCH, SIG_IGN);

  // Reinitialize the window to update data structures.
  endwin();
  initscr();
  refresh();
  clear();

  char tmp[128];
  sprintf(tmp, "%dx%d", COLS, LINES);

  // Approximate the center
  int x = COLS / 2 - strlen(tmp) / 2;
  int y = LINES / 2 - 1;

  mvaddstr(y, x, tmp);
  refresh();

  signal(SIGWINCH, handle_winch);
}

int main(int argc, char *argv[]){
  initscr();
  // COLS/LINES are now set

  signal(SIGWINCH, handle_winch);

  while(getch() != 27){
    /* Nada */
  }

  endwin();

  return(0);
}

12
投票
#include <stdio.h>
#include <stdlib.h>
#include <termcap.h>
#include <error.h>

static char termbuf[2048];

int main(void)
{
    char *termtype = getenv("TERM");

    if (tgetent(termbuf, termtype) < 0) {
        error(EXIT_FAILURE, 0, "Could not access the termcap data base.\n");
    }

    int lines = tgetnum("li");
    int columns = tgetnum("co");
    printf("lines = %d; columns = %d.\n", lines, columns);
    return 0;
}

需要使用-ltermcap编译。使用termcap可以获得许多其他有用的信息。有关详细信息,请使用info termcap查看termcap手册。


2
投票

如果您安装了ncurses并正在使用它,则可以使用getmaxyx()查找终端的尺寸。


0
投票

假设您使用的是Linux,我认为您希望使用ncurses库。我很确定你所拥有的ttysize东西不在stdlib中。


0
投票

所以不要在这里提出答案,但是:

linux-pc:~/scratch$ echo $LINES

49

linux-pc:~/scratch$ printenv | grep LINES

linux-pc:~/scratch$

好的,我注意到如果我调整GNOME终端的大小,LINES和COLUMNS变量就是这样的。

有点似乎GNOME终端本身正在创建这些环境变量?


-1
投票

以下是已建议的环境变量事件的函数调用:

int lines = atoi(getenv("LINES"));
int columns = atoi(getenv("COLUMNS"));
© www.soinside.com 2019 - 2024. All rights reserved.