Skip to content

Instantly share code, notes, and snippets.

@agvxov
Created January 29, 2025 13:17
Show Gist options
  • Save agvxov/7e093a2dd7641e95ec667213d9dcf99f to your computer and use it in GitHub Desktop.
Save agvxov/7e093a2dd7641e95ec667213d9dcf99f to your computer and use it in GitHub Desktop.
#Getting the TTY size in C without ncurses
>*NIX only
>it is baffling how everyone only ever says "uh oh use ncurses or something"
{
// @BAKE gcc -o $*.out $@
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
int get_tty_size(int * width_out, int * height_out) {
struct winsize ws;
int fd = open("/dev/tty", O_WRONLY);
if (fd == -1) { return 1; }
ioctl(fd, TIOCGWINSZ, &ws);
close(fd);
*width_out = ws.ws_col;
*height_out = ws.ws_row;
return 0;
}
#include <stdio.h>
signed main(void) {
int width, height;
get_tty_size(&width, &height);
printf("Your terminal size is %dx%d\n", width, height);
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment