From f5baea585ecd3288c20ba3cb9a130d939f79430f Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 21 Dec 2020 16:04:06 +0100 Subject: [PATCH] We can navigate in order to edit our message Signed-off-by: Yohann D'ANELLO --- squinnondation/squinnondation.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/squinnondation/squinnondation.py b/squinnondation/squinnondation.py index a1b4507..80ae272 100644 --- a/squinnondation/squinnondation.py +++ b/squinnondation/squinnondation.py @@ -74,19 +74,37 @@ class Squinnondation: while True: squirrel.refresh_history() squirrel.refresh_input() - key = screen.getkey(curses.LINES - 1, 3 + len(squirrel.nickname) + len(squirrel.input_buffer)) + key = screen.getkey(curses.LINES - 1, 3 + len(squirrel.nickname) + squirrel.input_index) if key == "\x7f": # backspace - squirrel.input_buffer = squirrel.input_buffer[:-1] + if squirrel.input_index: + squirrel.input_index -= 1 + squirrel.input_buffer = squirrel.input_buffer[:squirrel.input_index] \ + + squirrel.input_buffer[squirrel.input_index + 1:] + continue + elif key == "KEY_LEFT": + squirrel.input_index = max(0, squirrel.input_index - 1) + continue + elif key == "KEY_RIGHT": + squirrel.input_index = min(len(squirrel.input_buffer), squirrel.input_index + 1) + continue + elif key == "KEY_HOME": + squirrel.input_index = 0 + continue + elif key == "KEY_END": + squirrel.input_index = len(squirrel.input_buffer) continue elif len(key) > 1: squirrel.history.append(f" *unmanaged key press: {key}*") continue elif key != "\n": - squirrel.input_buffer += key + squirrel.input_buffer = squirrel.input_buffer[:squirrel.input_index] + key \ + + squirrel.input_buffer[squirrel.input_index:] + squirrel.input_index += 1 continue msg = squirrel.input_buffer squirrel.input_buffer = "" + squirrel.input_index = 0 if not msg: continue @@ -408,6 +426,7 @@ class Squirrel(Hazelnut): self.squinnondation = instance self.input_buffer = "" + self.input_index = 0 self.history = [] self.history_pad = curses.newpad(curses.LINES - 2, curses.COLS)