From 71b65191022da9c3a8c32cb6328135c7186504c6 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Thu, 29 May 2025 08:42:19 +0200 Subject: [PATCH] In Terminal.write_yx, truncate at .size.x, and properly handle wide chars. --- ircplom.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/ircplom.py b/ircplom.py index 6982da2..e59af7a 100755 --- a/ircplom.py +++ b/ircplom.py @@ -77,11 +77,15 @@ class Terminal: 'Flush terminal.' print('', end='', flush=True) - def write_yx(self, yx: YX, msg: str) -> None: - 'Starting at yx, write line with msg, padded at end with spaces.' - len_padding = self.size.x - (yx.x + len(msg)) - print(self._blessed.move_yx(yx.y, yx.x), end='') - print(msg + (' ' * len_padding), end='') + def write_yx(self, offset: YX, msg: str) -> None: + 'Starting at offset, write line with msg, padded at end with spaces.' + print(self._blessed.move_yx(offset.y, offset.x), end='') + len_with_offset = offset.x + self._blessed.length(msg) + if len_with_offset > self.size.x: + print(self._blessed.truncate(msg, self.size.x - offset.x), end='') + else: + len_padding = self.size.x - len_with_offset# - 2 + print(msg + (f' ' * len_padding), end='') def get_keypresses(self) -> Iterator[str]: '''Loop through keypresses from terminal, collect what blessed ignores. @@ -313,10 +317,10 @@ class TuiLoop(Loop): if not super().process_main(event): return False if event.type_ == 'RECV': - self._log_buffer += [f'<- {event.args[0]}'] + self._log_buffer += [f'<--- {event.args[0]}'] self._draw_log() elif event.type_ == 'SEND': - self._log_buffer += [f'-> {event.args[0]}'] + self._log_buffer += [f'---> {event.args[0]}'] self._draw_log() elif event.type_ == 'INPUT_PROMPT': if event.args[0] == 'ENTER': @@ -354,7 +358,7 @@ class TuiLoop(Loop): def _draw_log(self) -> None: temp_buffer = ([''] * self._term.size.y) + self._log_buffer[:] for i, line in enumerate(temp_buffer[-self._y_separator:]): - self._term.write_yx(YX(i, 0), line[:self._term.size.x]) + self._term.write_yx(YX(i, 0), line) def _draw_prompt(self) -> None: self._term.write_yx(YX(self._y_prompt, len(INPUT_PROMPT)), @@ -368,7 +372,6 @@ class SocketRecvLoop(Loop): msg = IrcMessage.from_raw(yielded) if msg.verb == 'PING': self.broadcast('PING', msg.parameters[0]) - # DEBUG = 3 / 0 self.broadcast('RECV', str(msg)) -- 2.30.2