Truncate messages if they are too large
This commit is contained in:
parent
8b187ec2b3
commit
f2318ed308
|
@ -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) \
|
||||||
|
|
Loading…
Reference in New Issue