From d87498bf3600dadc294c0b6058683dba54f5fb2c Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Thu, 3 Mar 2022 14:23:55 +0000 Subject: [PATCH] Use bitwise operators in LegacyWindowsTerm, fix formatting --- rich/_win32_console.py | 12 ++++-------- rich/console.py | 4 +--- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/rich/_win32_console.py b/rich/_win32_console.py index a4a76009..cf936c66 100644 --- a/rich/_win32_console.py +++ b/rich/_win32_console.py @@ -253,8 +253,7 @@ class LegacyWindowsTerm: 15, # bright white ] - def __init__(self, file: IO[str] = sys.stdout): - self.file = file + def __init__(self) -> None: handle = GetStdHandle(STDOUT) self._handle = handle default_text = GetConsoleScreenBufferInfo(handle).wAttributes @@ -262,10 +261,7 @@ class LegacyWindowsTerm: self._default_fore = default_text & 7 self._default_back = (default_text >> 4) & 7 - self._default_attrs = self._default_fore + self._default_back * 16 - - self.write = file.write - self.flush = file.flush + self._default_attrs = self._default_fore | (self._default_back << 4) @property def cursor_position(self) -> WindowsCoordinates: @@ -322,7 +318,7 @@ class LegacyWindowsTerm: assert back is not None SetConsoleTextAttribute( - self._handle, attributes=ctypes.c_ushort(fore + back * 16) + self._handle, attributes=ctypes.c_ushort(fore | (back << 4)) ) self.write_text(text) SetConsoleTextAttribute(self._handle, attributes=self._default_text) @@ -462,7 +458,7 @@ if __name__ == "__main__": console = Console() - term = LegacyWindowsTerm(console.file) + term = LegacyWindowsTerm() term.set_title("Win32 Console Examples") style = Style(color="black", bgcolor="red") diff --git a/rich/console.py b/rich/console.py index 15a78720..9275a1e7 100644 --- a/rich/console.py +++ b/rich/console.py @@ -1921,9 +1921,7 @@ class Console: from rich._win32_console import LegacyWindowsTerm from rich._windows_renderer import legacy_windows_render - legacy_windows_render( - self._buffer[:], LegacyWindowsTerm(self.file) - ) + legacy_windows_render(self._buffer[:], LegacyWindowsTerm()) output_capture_enabled = bool(self._buffer_index) if not legacy_windows_stdout or output_capture_enabled: