Last active
March 7, 2023 17:27
-
-
Save dakkar/38703016cb05dcb40e6d80a8c5c5a525 to your computer and use it in GitHub Desktop.
winsize ioctl in Raku
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v6.d; | |
use NativeCall; | |
class winsize is repr('CStruct') { | |
has uint16 $.rows; | |
has uint16 $.cols; | |
has uint16 $.xpixels; | |
has uint16 $.ypixels; | |
method gist() { | |
return "rows={self.rows} cols={self.cols} {self.xpixels}x{self.ypixels}" | |
} | |
} | |
# copied from my `/usr/include/asm-generic/ioctls.h` | |
constant TIOCGWINSZ = 0x5413; | |
sub term-size(--> winsize) { | |
sub ioctl(int32 $fd, int32 $cmd, winsize $winsize) is native {*} | |
my winsize $winsize .= new; | |
ioctl(0,TIOCGWINSZ,$winsize); | |
return $winsize; | |
} | |
say term-size(); | |
react { | |
whenever signal(SIGWINCH) { | |
say term-size(); | |
} | |
whenever signal(SIGINT) { | |
exit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment