#get terminal size

1 messages · Page 1 of 1 (latest)

dim perch
#

I need to get the terminal size in amount of rows and columns, I searched but haven't found a proper way.

still crown
#

the TIOCGWINSZ ioctl on posix, GetConsoleScreenBufferInfo on windows

dim perch
#

the only stuff i found about it are manual pages on C specifically

mental obsidian
#

The way I've been doing it for linux and macos is as such, where file is std.io.getStdOut():

var winsize: std.posix.system.winsize = undefined;
const errno = std.posix.system.ioctl(file.handle, posix.T.IOCGWINSZ, @intFromPtr(&winsize));
// switch on errno to verify .SUCCESS...
const width = winsize.ws_col;
const height = winsize.ws_row;

posix.system.ioctl is a C function (generally). The docs will get you a good way through anything you need to do, and anything that is C can be found in the C manpages

edit: link symbols swapped. random internet person's answer is similar to mine