From 2f752592a18a9b43ad68b62e4631253fecbe56ab Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 21 Dec 2020 16:04:06 +0100 Subject: [PATCH] Scroll in the history with up/down arrows and page up/page down Signed-off-by: Yohann D'ANELLO --- squinnondation/squinnondation.py | 34 ++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/squinnondation/squinnondation.py b/squinnondation/squinnondation.py index 80ae272..e9deeb1 100644 --- a/squinnondation/squinnondation.py +++ b/squinnondation/squinnondation.py @@ -87,6 +87,19 @@ class Squinnondation: elif key == "KEY_RIGHT": squirrel.input_index = min(len(squirrel.input_buffer), squirrel.input_index + 1) continue + elif key == "KEY_UP": + squirrel.last_line = min(max(curses.LINES - 3, squirrel.last_line - 1), len(squirrel.history) - 1) + continue + elif key == "KEY_DOWN": + squirrel.last_line = min(len(squirrel.history) - 1, squirrel.last_line + 1) + continue + elif key == "KEY_PPAGE": + squirrel.last_line = min(max(curses.LINES - 3, squirrel.last_line - (curses.LINES - 3)), + len(squirrel.history) - 1) + continue + elif key == "KEY_NPAGE": + squirrel.last_line = min(len(squirrel.history) - 1, squirrel.last_line + (curses.LINES - 3)) + continue elif key == "KEY_HOME": squirrel.input_index = 0 continue @@ -94,7 +107,7 @@ class Squinnondation: squirrel.input_index = len(squirrel.input_buffer) continue elif len(key) > 1: - squirrel.history.append(f" *unmanaged key press: {key}*") + squirrel.add_message(f" *unmanaged key press: {key}*") continue elif key != "\n": squirrel.input_buffer = squirrel.input_buffer[:squirrel.input_index] + key \ @@ -110,7 +123,7 @@ class Squinnondation: continue msg = f"<{squirrel.nickname}> {msg}" - squirrel.history.append(msg) + squirrel.add_message(msg) for hazelnut in list(squirrel.hazelnuts.values()): pkt = Packet() @@ -427,6 +440,7 @@ class Squirrel(Hazelnut): self.input_buffer = "" self.input_index = 0 + self.last_line = -1 self.history = [] self.history_pad = curses.newpad(curses.LINES - 2, curses.COLS) @@ -436,7 +450,7 @@ class Squirrel(Hazelnut): curses.init_pair(i + 1, i, curses.COLOR_BLACK) self.hazelnuts = dict() - self.history.append(f" *Listening on {self.address}:{self.port}*") + self.add_message(f" *Listening on {self.address}:{self.port}*") def find_hazelnut(self, address: str, port: int) -> Hazelnut: """ @@ -474,6 +488,14 @@ class Squirrel(Hazelnut): """ return self.socket.recvfrom(1024) + def add_message(self, msg: str) -> None: + """ + Store a new message into the history. + """ + self.history.append(msg) + if self.last_line == len(self.history) - 2: + self.last_line += 1 + def print_markdown(self, pad: Any, y: int, x: int, msg: str, bold: bool = False, italic: bool = False, underline: bool = False, strike: bool = False) -> int: """ @@ -564,7 +586,7 @@ class Squirrel(Hazelnut): self.input_pad.resize(1, curses.COLS - 1) self.history_pad.erase() - for i, msg in enumerate(self.history[max(0, len(self.history) - curses.LINES + 2):]): + for i, msg in enumerate(self.history[max(0, self.last_line - curses.LINES + 3):self.last_line + 1]): if not re.match("<.*> .*", msg): msg = " " + msg match = re.match("<(.*)> (.*)", msg) @@ -605,8 +627,8 @@ class Worm(Thread): try: pkt, hazelnut = self.squirrel.receive_packet() except ValueError as error: - self.squirrel.history.append(" *An error occured while receiving a packet: {}*".format(error)) + self.squirrel.add_message(" *An error occured while receiving a packet: {}*".format(error)) else: - self.squirrel.history.append(pkt.body[0].data.decode('UTF-8')) + self.squirrel.add_message(pkt.body[0].data.decode('UTF-8')) self.squirrel.refresh_history() self.squirrel.refresh_input()