On Sun, Dec 20, 2020 at 8:06 PM Eryk Sun <eryksun@gmail.com> wrote:
The entire buffer can be scrolled out, like
the CMD shell's CLS command, or one can just scroll the buffer enough
to clear the visible window.

The Windows console model is a bit different from POSIX terminals. Instead of having a screen and a scrollback buffer, you have a buffer and a window/view into it.

If you want clear() to just erase the screen and preserve "scrollback", the obvious thing to do is to clear the window/view. The problem is that if the user happens to be looking at "scrollback" when clear() is called, whatever they're looking at will be erased, not whatever was written since the last clear().

I wonder if the best approach wouldn't be to set the top-of-screen line on the first call of clear() (perhaps to one more than the current cursor line) and then use that line on all subsequent calls regardless of the current window or cursor position. That way scrollback would be safe both from accidental erasure and from pollution by programs that use clear() as a poor man's curses by erasing and redrawing everything whenever anything changes.

I don't think there would need to be any interface to clear the first-line cache, since the whole thing is just a cheap hack anyway.

I think it'd be a good idea to include some sort of proper Windows console interface in the standard library. It's only fair since other platforms already have curses. Then os.clear() could be implemented on top of it, and if you didn't like its behavior you could import the real library and do what you want.