Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle terminal resizing? #20

Closed
singingwolfboy opened this issue Apr 26, 2013 · 4 comments
Closed

How to handle terminal resizing? #20

singingwolfboy opened this issue Apr 26, 2013 · 4 comments

Comments

@singingwolfboy
Copy link

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.

@nsf
Copy link
Owner

nsf commented Apr 26, 2013

There are two ways to handle it.

  1. Termbox has special resize event. You get them when terminal is being resized. http://godoc.org/github.com/nsf/termbox-go#Event - this structure will have Type set to EventResize, plus valid Width and Height fields.
  2. You can just check the size everytime you redraw things. The internal buffer is being resized possibly in two cases: when you call the 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.

@lukechampine
Copy link

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?

@nsf
Copy link
Owner

nsf commented Sep 17, 2014

Updated the Size() docs, is it better now?

@lukechampine
Copy link

Yes, that looks clearer to me. Thanks!

@nsf nsf closed this as completed Dec 13, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants