我一直在寻找一种从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;
}
有帮助吗?

解决方案

您是否考虑过使用 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
}

其他提示

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

正如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);
}
#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手册。

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

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

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

linux-pc:〜/ scratch $ echo $ LINES

49

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

<代码>的Linux-PC:〜/划痕$

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

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

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

int lines = atoi(getenv("LINES"));
int columns = atoi(getenv("COLUMNS"));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top