On 10/13/20, Mike Miller <python-id...@mgmiller.net> wrote:
>
> The legacy Windows console has another limitation in that I don't believe it
> has a single API call to clear the whole thing.  One must iterate over the 
> whole
> buffer and write spaces to each cell, or some similar craziness.

No, it's not really similar craziness -- at least not from the client
program's perspective. The implementation in the console host itself
is probably something like that.

CMD's CLS is implemented with three API calls:
GetConsoleScreenBufferInfo to get the screen-buffer dimensions and
default text attributes, ScrollConsoleScreenBufferW to shift the
buffer out, and SetConsoleCursorPosition to move the cursor to (0,0).

https://docs.microsoft.com/en-us/windows/console/scrollconsolescreenbuffer

The following debugger session is while stepped into CMD's eCls()
function that implements the CLS command. This is just before it calls
ScrollConsoleScreenBufferW, with parameters 1-4 in registers rcx, rdx,
r8, and r9, and parameter 5 on the stack.

lpScrollRectangle (rdx): the entire screen buffer (sized 125 x 9001)
is to be scrolled.

    0:000> ?? ((SMALL_RECT *)@rdx)
    struct _SMALL_RECT * 0x000000e9`be8ff8b8
       +0x000 Left             : 0n0
       +0x002 Top              : 0n0
       +0x004 Right            : 0n125
       +0x006 Bottom           : 0n9001

dwDestinationOrigin (r9): the target row is -9001, so the contents of
the entire buffer are shifted out.

    0:000> ?? (short)(@r9 >> 16)
    short 0n-9001

lpFill (rsp / stack): use a space with the default attributes (in my
case background color 0 and foreground color 7, in the current
16-color palette).

    0:000> ?? ((CHAR_INFO **)@rsp)[4]->Char.UnicodeChar
    wchar_t 0x20 ' '
    0:000> ?? ((CHAR_INFO **)@rsp)[4]->Attributes
    unsigned short 7
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/SYMJ4HM6ZUBA3HMS5QXIDVSMQDRECHFP/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to