From 79e400d1eacb0f0aa14d451dbc3d4a9972311aa7 Mon Sep 17 00:00:00 2001 From: Plaza521 Date: Thu, 26 Oct 2023 01:34:51 +0300 Subject: [PATCH] Add screen.py Class to simplify the output and not think about moving cursor to start of screen (clear command works slow) --- main.py | 7 +++++++ screen.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 main.py create mode 100644 screen.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..4c7173d --- /dev/null +++ b/main.py @@ -0,0 +1,7 @@ +def main() -> None: + pass + + +if __name__ == "__main__": + main() + diff --git a/screen.py b/screen.py new file mode 100644 index 0000000..56fb7d7 --- /dev/null +++ b/screen.py @@ -0,0 +1,44 @@ +from typing import List, Any +from sys import platform + + +if platform == "win32": + from ctypes import windll, c_long, Structure + STD_OUTPUT_HANDLE = -11 + STDHANDLE = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE) + + class COORDSET(Structure): + _fields_ = [("X", c_long), ("Y", c_long)] + + def set_cursor_position(x: int, y: int) -> None: + windll.kernel32.SetConsoleCursorPosition(STDHANDLE, COORDSET(x, y)) +else: + def set_cursor_position(x: int, y: int) -> None: + print(f"\033[{x};{y}H") + + +class Screen: + def __init__( + self, + width: int, + height: int, + fill_object: Any = None + ) -> None: + self.width: int = width + self.height: int = height + self.frame: List[List[Any]] = \ + [[fill_object] * width for line in range(height)] + + def __getitem__(self, key: int) -> List[Any]: + return self.frame[key] + + def fill(self, fill_object: Any = None) -> None: + self.frame: List[List[Any]] = \ + [[fill_object] * width for line in range(height)] + + def print(self) -> None: + set_cursor_position(0, 0) + for line in self.frame: + for point in line: + print(point, end='') + print()