I am hacking on a terminal editor and am trying to update my ui struct’s internal state when the terminal window changes its size. I understand that one can subscribe to a posix channel to get notified when the window changes, like so:
The inner struct function cannot access outer local variables. It can access global variables.
When handlers are called, the normal execution of your program is interrupted in some unknown state.
Your best strategy is to do almost nothing in the handler; just notify the program about the change e.g. by raising a flag. Then your main program loop can handle the resize.
I see, so basically maintain a global variable of type bool that I set to true on sigwinch, and then set it back to false once the state has been handled? Thanks a lot!
If you are developing with a specific terminal in mind, I’ve written a new spec and a few popular terminals have adopted it. It allows you to subscribe to window size changes in the escape sequence stream instead of needing the global signal handler.
It’s likely not in any released versions, but the development branches of foot, ghostty, kitty, and iterm2 all support it.
I am daily driving WezTerm, so I would need to support the sigwinch way for a while. But I will definitely keep an eye out for this. I can see you made an issue that I subscribed to.