Long messages are wrapped in the history to avoid crashes

Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
Yohann D'ANELLO 2020-12-21 16:04:14 +01:00
parent 7a25d24ba3
commit dd4f839fad
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
1 changed files with 16 additions and 5 deletions

View File

@ -335,7 +335,14 @@ class Squirrel(Hazelnut):
attrs |= curses.A_UNDERLINE if underline else 0 attrs |= curses.A_UNDERLINE if underline else 0
if strike: if strike:
msg = "".join(c + "\u0336" for c in msg) msg = "".join(c + "\u0336" for c in msg)
pad.addstr(y, x, msg, attrs)
remaining_lines = curses.LINES - 3 - (y + x // (curses.COLS - 1)) + 1
if remaining_lines > 0:
# Don't print the end of the line if it is too long
space_left_on_line = (curses.COLS - 2) - (x % (curses.COLS - 1))
msg = msg[:space_left_on_line + max(0, (curses.COLS - 1) * (remaining_lines - 1))]
if msg:
pad.addstr(y + x // (curses.COLS - 1), x % (curses.COLS - 1), msg, attrs)
return size return size
def refresh_history(self) -> None: def refresh_history(self) -> None:
@ -349,17 +356,21 @@ class Squirrel(Hazelnut):
self.input_pad.resize(1, curses.COLS - 1) self.input_pad.resize(1, curses.COLS - 1)
self.history_pad.erase() self.history_pad.erase()
y_offset = 0
for i, msg in enumerate(self.history[max(0, self.last_line - curses.LINES + 3):self.last_line + 1]): for i, msg in enumerate(self.history[max(0, self.last_line - curses.LINES + 3):self.last_line + 1]):
if i + y_offset > curses.LINES - 3:
break
if not re.match("<.*> .*", msg): if not re.match("<.*> .*", msg):
msg = "<unknown> " + msg msg = "<unknown> " + msg
match = re.match("<(.*)> (.*)", msg) match = re.match("<(.*)> (.*)", msg)
nickname = match.group(1) nickname = match.group(1)
msg = match.group(2) msg = match.group(2)
color_id = sum(ord(c) for c in nickname) % 6 + 1 color_id = sum(ord(c) for c in nickname) % 6 + 1
self.history_pad.addstr(i, 0, "<") true_width = self.print_markdown(self.history_pad, i + y_offset, 0, f"<**{nickname}**> {msg}")
self.history_pad.addstr(i, 1, nickname, curses.A_BOLD | curses.color_pair(color_id + 1)) self.history_pad.addstr(i + y_offset, 1, nickname, curses.A_BOLD | curses.color_pair(color_id + 1))
self.history_pad.addstr(i, 1 + len(nickname), "> ") y_offset += true_width // (curses.COLS - 1)
self.print_markdown(self.history_pad, i, 3 + len(nickname), msg)
self.history_pad.refresh(0, 0, 0, 0, curses.LINES - 2, curses.COLS) self.history_pad.refresh(0, 0, 0, 0, curses.LINES - 2, curses.COLS)
def refresh_input(self) -> None: def refresh_input(self) -> None: