Truncate messages if they are too large

This commit is contained in:
Yohann D'ANELLO 2020-11-26 20:04:54 +01:00
parent 8b187ec2b3
commit f2318ed308
1 changed files with 14 additions and 1 deletions

View File

@ -19,8 +19,21 @@ class Display:
def newpad(self, height: int, width: int) -> Union[FakePad, Any]: def newpad(self, height: int, width: int) -> Union[FakePad, Any]:
return curses.newpad(height, width) if self.screen else FakePad() return curses.newpad(height, width) if self.screen else FakePad()
def truncate(self, msg: str, height: int, width: int) -> str:
lines = msg.split("\n")
lines = lines[:height]
lines = [line[:width] for line in lines]
return "\n".join(lines)
def addstr(self, pad: Any, y: int, x: int, msg: str, *options) -> None: def addstr(self, pad: Any, y: int, x: int, msg: str, *options) -> None:
return pad.addstr(y, x, msg, *options) """
Display a message onto the pad.
If the message is too large, it is truncated vertically and horizontally
"""
height, width = pad.getmaxyx()
msg = self.truncate(msg, height - y, width - x)
if msg.replace("\n", ""):
return pad.addstr(y, x, msg, *options)
def init_pair(self, number: int, foreground: int, background: int) -> None: def init_pair(self, number: int, foreground: int, background: int) -> None:
return curses.init_pair(number, foreground, background) \ return curses.init_pair(number, foreground, background) \