> the second difficulty is handling reflow. a real terminal can't resize its screen, but an emulator can. how to implement that correctly with cursor movements?
The answer to this one, incidentally, is ¯\_(ツ)_/¯.
Every terminal emulator engine I've examined handles reflow a little differently. There's no official spec, and everyone has their own idea about how to make it work "best". That being said, I've wrestled with these questions myself, and here's what I've come up with:
* Each line should have a flag on it which tracks whether it should be reflowed with the next line. By default, this flag should not be set.
* The reflow flag should only be set when the cursor leaves that line because a character was printed with the cursor in the last column ("wrapnext" state), and no special modes are set which change the wrapping columns.
* Performing any other action which changes the contents of a line should clear the reflow flag on that line.
The same logic can also be used to join wrapped lines for copy/paste. It won't work 100% of the time, but it will work for most situations.