squirrel-battle/dungeonbattle/display/display.py

44 lines
1.1 KiB
Python

import curses
from typing import Any, Union
from dungeonbattle.tests.screen import FakePad
class Display:
def __init__(self, screen: Any):
self.screen = screen
def refresh(self) -> None:
raise NotImplementedError
def newpad(self, height: int, width: int) -> Union[FakePad, Any]:
return curses.newpad(height, width) if self.screen else FakePad()
def ensure_resized(self, *pads) -> bool:
"""
If the window got resized, ensure that the pads are also resized.
"""
y, x = self.screen.getmaxyx() if self.screen else (0, 0)
for pad in pads:
pad.resize(y, x)
if self.screen and curses.is_term_resized(self.rows, self.cols):
curses.resizeterm(y, x)
return True
return False
@property
def rows(self) -> int:
return curses.LINES if self.screen else 42
@property
def height(self) -> int:
return self.rows
@property
def cols(self) -> int:
return curses.COLS if self.screen else 42
@property
def width(self) -> int:
return self.cols