Add screen.py
Class to simplify the output and not think about moving cursor to start of screen (clear command works slow)dev
parent
d11ac62e1c
commit
79e400d1ea
|
@ -0,0 +1,7 @@
|
|||
def main() -> None:
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
@ -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()
|
Loading…
Reference in New Issue