I've never written text-based UIs before, but I thought I'd give termbox a try since it seems pretty simple. I'm starting to get the hang of it, but one thing that's confusing me is how to handle terminal window resize events. Does termbox support this case? Ideally, I'd like to be able to define a function that runs whenever the user resizes the terminal, and the function would take care of determining the new terminal size and re-drawing cells as necessary. I don't see any documentation about this, and I don't know how easy or hard it would be.
There are two ways to handle it.
Type set to EventResize, plus valid Width and Height fields.Clear function or Flush. Before clearing and before flushing it will resize the internal back buffer. So, checking the results of Size function after calling these is the way to catch the resize.Which one to choose really depends on logic. In some cases you'll have a complete redraw on every event you get, then just check the size every time and it'll work. Or if your app does careful event processing and doesn't redraw itself every time, you can wait for the resize event.
Yes it's not very well documented, because I'm still not sure about the right semantics of this behaviour. Perhaps I should document it better, or maybe create a tutorial of some sort explaining various aspects of termbox. Will leave that issue open as a reminder.
To add my personal experience: I was calling termbox.Size() immediately after receiving the EventResize, so the return values were wrong. It was easy enough to change this to event.Width, event.Height, but I had to hunt around for a bit to discover the problem. Ideally, resize() would automatically update the values returned by termbox.Size(), but it looks like that would require some refactoring. At the very least, could you add a comment to termbox.Size() that warns users of this behavior?
Updated the Size() docs, is it better now?
Yes, that looks clearer to me. Thanks!