Get size of terminal window (rows/columns)

2019-03-08 11:40发布

Is there any reliable way of getting the number of columns/rows of the current output terminal window?

I want to retrieve these numbers in a C/C++ program.

I'm looking for a GNU/Linux solution primarily, but also need a Windows solution.

4条回答
一夜七次
2楼-- · 2019-03-08 11:54

On GNU/Linux using libtermcap (https://www.gnu.org/software/termutils/manual/termcap-1.3/html_mono/termcap.html) create demo.c:

#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <term.h>

static char term_buffer[2048];

void
init_terminal_data (void)
{

  char *termtype = getenv ("TERM");
  int success;

  if (termtype == NULL)
    fprintf (stderr, "Specify a terminal type with `setenv TERM <yourtype>'.\n");

  success = tgetent (term_buffer, termtype);
  if (success < 0)
    fprintf (stderr, "Could not access the termcap data base.\n");
  if (success == 0)
    fprintf (stderr, "Terminal type `%s' is not defined.\n", termtype);
}

int
main ()
{
  init_terminal_data ();
  printf ("Got: Lines: %d, Columns: %d\n", tgetnum ("li"), tgetnum ("co"));
  return 0;
}

Then compile with gcc -o demo.x demo.c -ltermcap and run to give:

$ ./demo.x
Got: Lines: 24, Columns: 80

I doubt this helps much on Windows though, I don't know that platform.

(Some of this code is copied straight from the termcap documentation.)

查看更多
爷、活的狠高调
3楼-- · 2019-03-08 12:03

Linux/unix: Use ioctl with the standard output file number STDOUT_FILENO and TIOCGWINSZ.

struct winsize size;
ioctl(STDOUT_FILENO,TIOCGWINSZ,&size);
/* size.ws_row is the number of rows, size.ws_col is the number of columns. */


Also, while I didn't touch windows in the last 5 years, GetConsoleScreenBufferInfo should help you get the current console size.

查看更多
Ridiculous、
4楼-- · 2019-03-08 12:11

On Windows, use the following code to print the size of the console window (borrowed from here):

#include <windows.h>

int main(int argc, char *argv[]) 
{
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    int columns, rows;

    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
    columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
    rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;

    printf("columns: %d\n", columns);
    printf("rows: %d\n", rows);
    return 0;
}

On Linux, use the following instead (borrowed from here):

#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
}
查看更多
SAY GOODBYE
5楼-- · 2019-03-08 12:13

To expand @herohuyongtao answer for Windows. The .srWindow property gives the answer to the size of the console window, i.e. visible rows and cols. This doesn't say what is the actual available screen buffer width and height, which could be larger if window contains scroll bars. If this is the case, use .dwSize:

CONSOLE_SCREEN_BUFFER_INFO sbInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &sbInfo);
int availableColumns = sbInfo.dwSize.X;
int availableRows = sbInfo.dwSize.Y;
查看更多
登录 后发表回答