DylanJ
DylanJ

Reputation: 2413

ncurses terminal size

How do I find the terminal width & height of an ncurses application?

Upvotes: 25

Views: 55199

Answers (6)

Random dude
Random dude

Reputation: 9

From the ncurses documentation

9.4. The other stuff in the example

You can also see in the above examples, that I have used the variables COLS, LINES which are initialized to the screen sizes after initscr(). They can be useful in finding screen dimensions and finding the center co-ordinate of the screen as above. The function getch() as usual gets the key from keyboard and according to the key it does the corresponding work. This type of switch- case is very common in any GUI based programs.

The macros are updated automatically upon window resize, so you don't need to call refersh().

Upvotes: 0

Bora M. Alper
Bora M. Alper

Reputation: 3844

the variables COLS, LINES are initialized to the screen sizes after initscr().

Source: NCURSES Programming HOWTO

I'm not sure if they get updated on resize though.

Upvotes: 1

Thomas Dickey
Thomas Dickey

Reputation: 54455

ncurses applications normally handle SIGWINCH and use the ioctl with TIOCGWINSZ to obtain the system's notion of the screensize. That may be overridden by the environment variables LINES and COLUMNS (see use_env).

Given that, the ncurses global variables LINES and COLS are updated as a side-effect when wgetch returns KEY_RESIZE (in response to a SIGWINCH) to give the size of stdscr (the standard screen representing the whole terminal).

You can of course use getmaxx, getmaxy and getmaxyx to get one or both of the limits for the x- and y-ordinates of a window. Only the last is standard (and portable).

Further reading:

Upvotes: 17

mtvee
mtvee

Reputation: 1565

void getmaxyx(WINDOW *win, int y, int x); i believe...

also, this may help...

Getting terminal width in C?

Upvotes: 28

qknight
qknight

Reputation: 924

i'm using this code:

struct winsize size;
if (ioctl(0, TIOCGWINSZ, (char *) &size) < 0)
    printf("TIOCGWINSZ error");
printf("%d rows, %d columns\n", size.ws_row, size.ws_col);

Upvotes: 3

Conrad Meyer
Conrad Meyer

Reputation: 2886

What about using SCR_H and SCR_W?

Upvotes: 1

Related Questions