Discussion on: Want to create a TUI application? - The Basics of "Uncooked" Terminal IO

lhp profile image
Leon Henrik Plickat Author

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).