Hi everyone!
What’s the easiest way to get the width of the terminal in Crystal ?
I didn’t have any luck finding any shards that would handle this either.
Thanks in advance!
Hi everyone!
What’s the easiest way to get the width of the terminal in Crystal ?
I didn’t have any luck finding any shards that would handle this either.
Thanks in advance!
My libremiliacr library has that. But it might be easier just to copy-paste this snippit somewhere.
lib LibC
# Bare minimum for the ioctl stuff we need
TIOCGWINSZ = 0x5413u32
struct Winsize
ws_row : UInt16
ws_col : UInt16
ws_xpixel : UInt16
ws_ypixel : UInt16
end
fun ioctl(fd : LibC::Int, what : LibC::ULong, ...) : LibC::Int
end
# Gets the width and height of the terminal.
def getWinSize : Tuple(UInt16, UInt16)
thing = LibC::Winsize.new
LibC.ioctl(STDOUT.fd, LibC::TIOCGWINSZ, pointerof(thing))
{thing.ws_row, thing.ws_col}
end
height, width = getWinSize
The Winsize struct and ioctrl function might already be in the standard library… I don’t think it was when I wrote this, and I haven’t checked since.
thanks @MistressRemilia , this works great!