From 66bea1f346aa5c332e39c9e4a41b83ea8c085824 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B4=A2=E6=AF=85?= <944102712@qq.com> Date: Wed, 20 Dec 2023 20:59:59 +0800 Subject: [PATCH 1/2] record user command correctly --- terminado/websocket.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/terminado/websocket.py b/terminado/websocket.py index 4a988fc..9849ddb 100644 --- a/terminado/websocket.py +++ b/terminado/websocket.py @@ -65,6 +65,7 @@ def open(self, url_component: Any = None) -> None: # type:ignore[override] self.terminal.clients.append(self) self.send_json_message(["setup", {}]) self._logger.info("TermSocket.open: Opened %s", self.term_name) + self.cursor_position = 0 # Now drain the preopen buffer, if reconnect. buffered = "" preopen_buffer = self.terminal.read_buffer.copy() @@ -105,8 +106,27 @@ def on_message(self, message: str) -> None: # type:ignore[misc] if command[1] == "\r": self.log_terminal_output(f"STDIN: {self._user_command}") self._user_command = "" + self.cursor_position = 0 + elif command[1] == "\x7f": + # delete + if self.cursor_position > 0: + self._user_command = (self._user_command[:self.cursor_position - 1] + + self._user_command[self.cursor_position:]) + self.cursor_position -= 1 + elif command[1] == "\x1bOD": + # move left + if self.cursor_position > 0: + self.cursor_position -= 1 + elif command[1] == "\x1bOC": + # move right + if self.cursor_position < len(self._user_command): + self.cursor_position += 1 else: - self._user_command += command[1] + # insert + self._user_command = (self._user_command[:self.cursor_position] + + command[1] + + self._user_command[self.cursor_position:]) + self.cursor_position += 1 elif msg_type == "set_size": self.size = command[1:3] self.terminal.resize_to_smallest() From c9754f5a25572a4544633fa63c02d7994bc779ed Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 20 Dec 2023 13:02:00 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- terminado/websocket.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/terminado/websocket.py b/terminado/websocket.py index 9849ddb..34da133 100644 --- a/terminado/websocket.py +++ b/terminado/websocket.py @@ -110,8 +110,10 @@ def on_message(self, message: str) -> None: # type:ignore[misc] elif command[1] == "\x7f": # delete if self.cursor_position > 0: - self._user_command = (self._user_command[:self.cursor_position - 1] + - self._user_command[self.cursor_position:]) + self._user_command = ( + self._user_command[: self.cursor_position - 1] + + self._user_command[self.cursor_position :] + ) self.cursor_position -= 1 elif command[1] == "\x1bOD": # move left @@ -123,9 +125,11 @@ def on_message(self, message: str) -> None: # type:ignore[misc] self.cursor_position += 1 else: # insert - self._user_command = (self._user_command[:self.cursor_position] + - command[1] + - self._user_command[self.cursor_position:]) + self._user_command = ( + self._user_command[: self.cursor_position] + + command[1] + + self._user_command[self.cursor_position :] + ) self.cursor_position += 1 elif msg_type == "set_size": self.size = command[1:3]