I have a tui application written in C where the simplified mainloop look something like this:
enterrawmode();
while(1) {
clearscreen(); //I clear the screen using a ANSI code.
checkforresizeterminal(); //Using ioctl
draw();
getinput();
usleep(10000);
}
When running it in some terminals, it is flickering a lot, while other programs, such as vim/neovim are updating without any flickering. How are they achieving this?
Also, when running the program, the cpu usage is high. Is there a more efficient way to check if the terminal was resized (rather than checking for it every time the screen is drawn)? When nvim is running, it is at 0% cpu usage when idle, and it goes up when I resize the terminal. How is it getting low latency input and resizing without very high cpu usage? How does it "know" when to update? Can curses do this, if so how? Is there any way to get low latency input and respond to terminal resize with low latency without high cpu usage and checking for it in a loop such as this?