Scroll in the history with up/down arrows and page up/page down

Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
Yohann D'ANELLO 2020-12-21 16:04:06 +01:00
parent f5baea585e
commit 2f752592a1
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
1 changed files with 28 additions and 6 deletions

View File

@ -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"<system> *unmanaged key press: {key}*")
squirrel.add_message(f"<system> *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"<system> *Listening on {self.address}:{self.port}*")
self.add_message(f"<system> *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 = "<unknown> " + 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("<system> *An error occured while receiving a packet: {}*".format(error))
self.squirrel.add_message("<system> *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()