Manage backspace key

Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
Yohann D'ANELLO 2020-12-21 16:04:05 +01:00
parent 639ad2b3bd
commit a21b0aa652
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 30 additions and 4 deletions

View File

@ -39,7 +39,8 @@ class Squinnondation:
help="Don't replace emojis.") help="Don't replace emojis.")
self.args = parser.parse_args() self.args = parser.parse_args()
if not (1024 <= self.args.bind_port <= 65535) or not (1024 <= self.args.client_port <= 65535): if not (1024 <= self.args.bind_port <= 65535) or\
not (not self.args.client_port or 1024 <= self.args.client_port <= 65535):
raise ValueError("Ports must be between 1024 and 65535.") raise ValueError("Ports must be between 1024 and 65535.")
self.bind_address = self.args.bind_address self.bind_address = self.args.bind_address
@ -56,7 +57,9 @@ class Squinnondation:
screen = term_manager.screen screen = term_manager.screen
instance.screen = screen instance.screen = screen
screen.addstr(0, 0, "Enter your nickname: ") screen.addstr(0, 0, "Enter your nickname: ")
curses.echo()
nickname = screen.getstr().decode("UTF-8") nickname = screen.getstr().decode("UTF-8")
curses.noecho()
squirrel = Squirrel(instance, nickname) squirrel = Squirrel(instance, nickname)
squirrel.refresh_history() squirrel.refresh_history()
@ -71,7 +74,20 @@ class Squinnondation:
while True: while True:
squirrel.refresh_history() squirrel.refresh_history()
squirrel.refresh_input() squirrel.refresh_input()
msg = screen.getstr(curses.LINES - 1, 3 + len(squirrel.nickname)).decode("UTF-8") key = screen.getkey(curses.LINES - 1, 3 + len(squirrel.nickname) + len(squirrel.input_buffer))
if key == "\x7f": # backspace
squirrel.input_buffer = squirrel.input_buffer[:-1]
continue
elif len(key) > 1:
squirrel.history.append(f"<system> *unmanaged key press: {key}*")
continue
elif key != "\n":
squirrel.input_buffer += key
continue
msg = squirrel.input_buffer
squirrel.input_buffer = ""
if not msg: if not msg:
continue continue
@ -391,6 +407,8 @@ class Squirrel(Hazelnut):
self.squinnondation = instance self.squinnondation = instance
self.input_buffer = ""
self.history = [] self.history = []
self.history_pad = curses.newpad(curses.LINES - 2, curses.COLS) self.history_pad = curses.newpad(curses.LINES - 2, curses.COLS)
self.input_pad = curses.newpad(1, curses.COLS) self.input_pad = curses.newpad(1, curses.COLS)
@ -549,6 +567,7 @@ class Squirrel(Hazelnut):
self.input_pad.addstr(0, 0, "<") self.input_pad.addstr(0, 0, "<")
self.input_pad.addstr(0, 1, self.nickname, curses.A_BOLD | curses.color_pair(color_id + 1)) self.input_pad.addstr(0, 1, self.nickname, curses.A_BOLD | curses.color_pair(color_id + 1))
self.input_pad.addstr(0, 1 + len(self.nickname), "> ") self.input_pad.addstr(0, 1 + len(self.nickname), "> ")
self.input_pad.addstr(0, 3 + len(self.nickname), self.input_buffer)
self.input_pad.refresh(0, 0, curses.LINES - 1, 0, curses.LINES - 1, curses.COLS - 1) self.input_pad.refresh(0, 0, curses.LINES - 1, 0, curses.LINES - 1, curses.COLS - 1)
@ -571,3 +590,4 @@ class Worm(Thread):
else: else:
self.squirrel.history.append(pkt.body[0].data.decode('UTF-8')) self.squirrel.history.append(pkt.body[0].data.decode('UTF-8'))
self.squirrel.refresh_history() self.squirrel.refresh_history()
self.squirrel.refresh_input()

View File

@ -14,8 +14,12 @@ class TermManager: # pragma: no cover
self.screen = curses.initscr() self.screen = curses.initscr()
# convert escapes sequences to curses abstraction # convert escapes sequences to curses abstraction
self.screen.keypad(True) self.screen.keypad(True)
# stop printing typed keys to the terminal
curses.noecho()
# send keys through without having to press <enter>
curses.cbreak()
# make cursor invisible # make cursor invisible
curses.curs_set(False) # curses.curs_set(False)
# Catch mouse events # Catch mouse events
curses.mousemask(True) curses.mousemask(True)
# Enable colors # Enable colors
@ -28,5 +32,7 @@ class TermManager: # pragma: no cover
exc_traceback: TracebackType) -> None: exc_traceback: TracebackType) -> None:
# restore the terminal to its original state # restore the terminal to its original state
self.screen.keypad(False) self.screen.keypad(False)
curses.curs_set(True) curses.nocbreak()
curses.echo()
# curses.curs_set(True)
curses.endwin() curses.endwin()