diff --git a/Lib/_pyrepl/windows_console.py b/Lib/_pyrepl/windows_console.py index c897b16dd0e68d..528bec192f720a 100644 --- a/Lib/_pyrepl/windows_console.py +++ b/Lib/_pyrepl/windows_console.py @@ -86,6 +86,11 @@ class CHAR_INFO(Structure): FillConsoleOutputCharacter.argtypes = [HANDLE, CHAR, DWORD, _COORD, POINTER(DWORD)] FillConsoleOutputCharacter.restype = BOOL +FillConsoleOutputAttribute = windll.kernel32.FillConsoleOutputAttribute +FillConsoleOutputAttribute.use_last_error = True +FillConsoleOutputAttribute.argtypes = [HANDLE, WORD, DWORD, _COORD, POINTER(DWORD)] +FillConsoleOutputAttribute.restype = BOOL + ScrollConsoleScreenBuffer = windll.kernel32.ScrollConsoleScreenBufferW ScrollConsoleScreenBuffer.use_last_error = True ScrollConsoleScreenBuffer.argtypes = [HANDLE, POINTER(SMALL_RECT), POINTER(SMALL_RECT), _COORD, POINTER(CHAR_INFO)] @@ -99,7 +104,7 @@ class CHAR_INFO(Structure): class Char(Union): _fields_ = [ ("UnicodeChar",WCHAR), - ("Char", CHAR), + ("Char", CHAR), ] class KeyEvent(ctypes.Structure): @@ -191,7 +196,7 @@ def __init__( term: str = "", encoding: str = "", ): - + SetConsoleMode(OutHandle, ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING) self.encoding = encoding or sys.getdefaultencoding() @@ -487,14 +492,14 @@ def move_cursor(self, x: int, y: int) -> None: trace(f'move to {x} {y}') if x < 0 or y < 0: - raise ValueError(f"Bad cussor position {x}, {y}") + raise ValueError(f"Bad cursor position {x}, {y}") self.__move(x, y) self.__posxy = x, y self.flushoutput() - - def set_cursor_vis(self, visible: bool) -> None: + + def set_cursor_vis(self, visible: bool) -> None: if visible: self.__show_cursor() else: @@ -506,9 +511,9 @@ def getheightwidth(self) -> tuple[int, int]: info = CONSOLE_SCREEN_BUFFER_INFO() if not GetConsoleScreenBufferInfo(OutHandle, info): raise ctypes.WinError(ctypes.GetLastError()) - return (info.srWindow.Bottom - info.srWindow.Top + 1, + return (info.srWindow.Bottom - info.srWindow.Top + 1, info.srWindow.Right - info.srWindow.Left + 1) - + def get_event(self, block: bool = True) -> Event | None: """Return an Event instance. Returns None if |block| is false and there is no event pending, otherwise waits for the @@ -527,7 +532,7 @@ def get_event(self, block: bool = True) -> Event | None: key = chr(rec.Event.KeyEvent.uChar.Char[0]) # self.push_char(key) if rec.Event.KeyEvent.uChar.Char == b'\r': - return Event(evt="key", data="\n", raw="\n") + return Event(evt="key", data="\n", raw="\n") trace('virtual key code', rec.Event.KeyEvent.wVirtualKeyCode, rec.Event.KeyEvent.uChar.Char) if rec.Event.KeyEvent.wVirtualKeyCode == 8: return Event(evt="key", data="backspace", raw=rec.Event.KeyEvent.uChar.Char) @@ -536,7 +541,7 @@ def get_event(self, block: bool = True) -> Event | None: if rec.Event.KeyEvent.uChar.Char == b'\x00': code = VK_MAP.get(rec.Event.KeyEvent.wVirtualKeyCode) if code: - return Event(evt="key", data=code, raw=rec.Event.KeyEvent.uChar.Char) + return Event(evt="key", data=code, raw=rec.Event.KeyEvent.uChar.Char) continue #print(key, rec.Event.KeyEvent.wVirtualKeyCode) return Event(evt="key", data=key, raw=rec.Event.KeyEvent.uChar.Char) @@ -557,6 +562,13 @@ def clear(self) -> None: size = info.dwSize.X * info.dwSize.Y if not FillConsoleOutputCharacter(OutHandle, b' ', size, _COORD(), DWORD()): raise ctypes.WinError(ctypes.GetLastError()) + if not FillConsoleOutputAttribute(OutHandle, 0, size, _COORD(), DWORD()): + raise ctypes.WinError(ctypes.GetLastError()) + y = info.srWindow.Bottom - info.srWindow.Top + 1 + self.__move_absolute(0, y - info.dwSize.Y) + self.__posxy = 0, 0 + self.screen = [""] + def finish(self) -> None: """Move the cursor to the end of the display and otherwise get