Comment by titzer
6 years ago
Oh man, they left out the part I was hoping to find out more about!
> it's good form to have a function called resize() or similar that you run on program start and later when the terminal window is resized. while there is a horrible way to do this with ANSI escapes, it's better to just bite the bullet and learn how to use ioctls and termios.
I wanted to know about the horrible ANSI escape sequences to send that can query the window size. Afaict one has to read from the terminal to get the answer--but these are control sequences that could be inserted into the normal stream--when, where, how?
You can look at the programmer's guide for the VT100[1], which does a good job of explaining the different ANSI escape codes.
To put it simply, you can move the cursor down and to the right. For example, you'd move the cursor 999 lines down, then 999 columns to the right, and then query the cursor's position[2]. You can then read the output through STDOUT and parse it.
I'd recommend anyone who's interested in playing around with ANSI and learning how to write a pure terminal-based program to look into the Kilo text editor[3]. That's where I learnt the above instructions.
[1] https://vt100.net/docs/vt100-ug/chapter3.html
[2] https://vt100.net/docs/vt100-ug/chapter3.html#CPR
[3] https://viewsourcecode.org/snaptoken/kilo/
Horrible escape sequences: man 4 console_codes
Horrible console ioctls: man 4 console_ioctl