From f2318ed30863ae88fe85e8abcfd21ec7f782be74 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Thu, 26 Nov 2020 20:04:54 +0100 Subject: [PATCH] Truncate messages if they are too large --- squirrelbattle/display/display.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/squirrelbattle/display/display.py b/squirrelbattle/display/display.py index ecfb2ec..0a06c65 100644 --- a/squirrelbattle/display/display.py +++ b/squirrelbattle/display/display.py @@ -19,8 +19,21 @@ class Display: def newpad(self, height: int, width: int) -> Union[FakePad, Any]: 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: - 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: return curses.init_pair(number, foreground, background) \