Hi. I’m making a TUI for Linux. I’ve been having an issue where I can’t parse all key presses.
For example, most control characters like up arrow, down arrow, the F1-F12 keys all start with a 0x1b or ESC byte and then have an unknown number of bytes after. It seems some have a delimiter of ~, but some simply don’t.
Anyway, I haven’t found a Zig solution that works for correctly parsing key inputs. I first tried using Io.Reader.takeByte() to get the first byte from stdin. Then I checked if that byte is ESC, and if it is, I peek a byte. This works for then getting the rest of the bytes of a control character. The issue occurs if the user presses ESC, because peekByte() waits input if there’s nothing in stdin.
I have tried to use ioctl() with FIONREAD to check if stdin was empty but it didn’t seem to work, just told me there were 0 bytes in the buffer.
pub fn is_stdin_empty() bool {
var bytes: u32 = undefined;
const ret = linux.ioctl(linux.STDIN_FILENO, linux.T.FIONREAD, @intFromPtr(&bytes));
if (ret == 0 and bytes == 0) {
return true;
} else {
return false;
}
}
So my question is, is there a Zig way to check if stdin is empty, and if not, whats the POSIX/Linux way to do this?
EDIT: (Note the double title because I’m unsure if this is an XY problem and if there is a Zig way to properly parse multibyte keys with the Io interface)
Thanks