Thanks for the post, it's just what I was looking for to start a tui app.
Sorry for my English because I'm using a translator.
I've been testing using a 2 byte buffer to try to work around the escape sequence problem and it seems to work. Here is the code if you want to see it.
Greetings.
while (true) {
var buffer: [2]u8 = undefined;
var n = try tty.read(&buffer);
if (buffer[0] == 'q') {
try os.tcsetattr(tty.handle, .FLUSH, original);
return;
} else if (buffer[0] == '\x1B') {
if (n == 1) {
debug.print("input: escape\r\n", .{});
} else if (n == 2) {
debug.print("input: Alt-{c}\r\n", .{buffer[1]});
}
} else if (buffer[0] == '\n' or buffer[0] == '\r') {
debug.print("input: return\r\n", .{});
} else {
debug.print("input: {c}\r\n", .{buffer[0]});
}
}
Yes, technically using a larger buffer instead of two reads is the better approach and I have now updated the article to mention that.
However, it is also a lot more involved. First, your buffer needs to be larger than 2, because some escape sequences are pretty long. Even if you don't want to handle them, they will mess with your input code if your buffer is too small. I use a 32 bit buffer in my code right now. Also, while rare, it is legal behaviour for the terminal so send you two or more input events at once, meaning your buffer may contain more than a single key-press. So you need to write a dedicated parser that can iterate over your input buffer, which is considerably more complex than the simple method I described in this article and something most sane people probably don't want to engage in (I am apparently not sane, as this is exactly the approach my TUI library uses now).
For further actions, you may consider blocking this person and/or reporting abuse
Thanks for the post, it's just what I was looking for to start a tui app.
Sorry for my English because I'm using a translator.
I've been testing using a 2 byte buffer to try to work around the escape sequence problem and it seems to work. Here is the code if you want to see it.
Greetings.
Yes, technically using a larger buffer instead of two reads is the better approach and I have now updated the article to mention that.
However, it is also a lot more involved. First, your buffer needs to be larger than 2, because some escape sequences are pretty long. Even if you don't want to handle them, they will mess with your input code if your buffer is too small. I use a 32 bit buffer in my code right now. Also, while rare, it is legal behaviour for the terminal so send you two or more input events at once, meaning your buffer may contain more than a single key-press. So you need to write a dedicated parser that can iterate over your input buffer, which is considerably more complex than the simple method I described in this article and something most sane people probably don't want to engage in (I am apparently not sane, as this is exactly the approach my TUI library uses now).