I'm working on a TUI (Text User Interface) library for C++, and I have a function for detecting the size of the console window. Is it possible for me to detect a window resize? I would prefer if it would work with any terminal emulator, but if it matters I am using Ubuntu Budgie 20.10, and my terminal emulator is Tilix.
2 Answers
You can set up a signal handler for the SIGWINCH signal (you can use signal() or sigaction()), and you will get this signal whenever the terminal size changes. Note that, as with all signal handlers, any code in the signal handler must be signal-safe.
If you are using the curses library, you will get a KEY_RESIZE key, and then check COLS and LINES.
2 Comments
Da Mammoth
Thanks, I'll look into it. I would also like to point out, that I am not using curses/ncurses, I'm using ASCII escape codes to control it.
Luigi Minardi
Not sure how useful this is for the OP thread but after listening to
SIGWINCH signal you can get the size using ioctl with TIOCGWINSZ as the op argument and a winsize struct as a 3rd argument.