Skip to content

Commit 88d64f5

Browse files
tonybaloneyDinoV
authored andcommitted
Implement screen clear for Windows (python#42)
1 parent bdff535 commit 88d64f5

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

Lib/_pyrepl/windows_console.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ class CHAR_INFO(Structure):
8686
FillConsoleOutputCharacter.argtypes = [HANDLE, CHAR, DWORD, _COORD, POINTER(DWORD)]
8787
FillConsoleOutputCharacter.restype = BOOL
8888

89+
FillConsoleOutputAttribute = windll.kernel32.FillConsoleOutputAttribute
90+
FillConsoleOutputAttribute.use_last_error = True
91+
FillConsoleOutputAttribute.argtypes = [HANDLE, WORD, DWORD, _COORD, POINTER(DWORD)]
92+
FillConsoleOutputAttribute.restype = BOOL
93+
8994
ScrollConsoleScreenBuffer = windll.kernel32.ScrollConsoleScreenBufferW
9095
ScrollConsoleScreenBuffer.use_last_error = True
9196
ScrollConsoleScreenBuffer.argtypes = [HANDLE, POINTER(SMALL_RECT), POINTER(SMALL_RECT), _COORD, POINTER(CHAR_INFO)]
@@ -99,7 +104,7 @@ class CHAR_INFO(Structure):
99104
class Char(Union):
100105
_fields_ = [
101106
("UnicodeChar",WCHAR),
102-
("Char", CHAR),
107+
("Char", CHAR),
103108
]
104109

105110
class KeyEvent(ctypes.Structure):
@@ -191,7 +196,7 @@ def __init__(
191196
term: str = "",
192197
encoding: str = "",
193198
):
194-
199+
195200
SetConsoleMode(OutHandle, ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
196201
self.encoding = encoding or sys.getdefaultencoding()
197202

@@ -466,12 +471,13 @@ def move_cursor(self, x: int, y: int) -> None:
466471
trace(f'move_cursor {x} {y}')
467472

468473
if x < 0 or y < 0:
469-
raise ValueError(f"Bad cussor position {x}, {y}")
474+
raise ValueError(f"Bad cursor position {x}, {y}")
470475

471476
self.__move_relative(x, y)
472477
self.__posxy = x, y
473478

474-
def set_cursor_vis(self, visible: bool) -> None:
479+
480+
def set_cursor_vis(self, visible: bool) -> None:
475481
if visible:
476482
self.__show_cursor()
477483
else:
@@ -483,9 +489,9 @@ def getheightwidth(self) -> tuple[int, int]:
483489
info = CONSOLE_SCREEN_BUFFER_INFO()
484490
if not GetConsoleScreenBufferInfo(OutHandle, info):
485491
raise ctypes.WinError(ctypes.GetLastError())
486-
return (info.srWindow.Bottom - info.srWindow.Top + 1,
492+
return (info.srWindow.Bottom - info.srWindow.Top + 1,
487493
info.srWindow.Right - info.srWindow.Left + 1)
488-
494+
489495
def get_event(self, block: bool = True) -> Event | None:
490496
"""Return an Event instance. Returns None if |block| is false
491497
and there is no event pending, otherwise waits for the
@@ -546,6 +552,13 @@ def clear(self) -> None:
546552
size = info.dwSize.X * info.dwSize.Y
547553
if not FillConsoleOutputCharacter(OutHandle, b' ', size, _COORD(), DWORD()):
548554
raise ctypes.WinError(ctypes.GetLastError())
555+
if not FillConsoleOutputAttribute(OutHandle, 0, size, _COORD(), DWORD()):
556+
raise ctypes.WinError(ctypes.GetLastError())
557+
y = info.srWindow.Bottom - info.srWindow.Top + 1
558+
self.__move_absolute(0, y - info.dwSize.Y)
559+
self.__posxy = 0, 0
560+
self.screen = [""]
561+
549562

550563
def finish(self) -> None:
551564
"""Move the cursor to the end of the display and otherwise get

0 commit comments

Comments
 (0)