From: Christian Heller Date: Sat, 7 Jun 2025 18:52:55 +0000 (+0200) Subject: Group disconnect, reconnect commands into dedicated new Window subclass. X-Git-Url: https://plomlompom.com/repos/%22https:/validator.w3.org/process_efforts?a=commitdiff_plain;p=ircplom Group disconnect, reconnect commands into dedicated new Window subclass. --- diff --git a/ircplom.py b/ircplom.py index 5b18324..b1605bf 100755 --- a/ircplom.py +++ b/ircplom.py @@ -29,6 +29,10 @@ KEYBINDINGS = { '[91, 49, 59, 51, 68]': ('window', 'left'), '[91, 49, 59, 51, 67]': ('window', 'right'), } +CMD_SHORTCUTS = { + 'disconnect': 'window.disconnect', + 'reconnect': 'window.reconnect' +} IRCSPEC_LINE_SEPARATOR = b'\r\n' IRCSPEC_TAG_ESCAPES = ((r'\:', ';'), @@ -166,7 +170,7 @@ class IrcConnection: def _start_connecting(self) -> None: - def connect(self): + def connect(self) -> None: self._socket = socket() self._broadcast('CONN_ALERT', f'Connecting to {self._hostname} …') @@ -627,6 +631,28 @@ class Window(Widget): self.prompt.draw() +class ConnectionWindow(Window): + 'Window with attributes and methods for dealing with an IrcConnection.' + + def __init__(self, + broadcast: Callable[[str, Any], None], + conn_idx: int, + *args, **kwargs + ) -> None: + self._broadcast = broadcast + self._conn_idx = conn_idx + super().__init__(*args, **kwargs) + + def cmd__disconnect(self, quit_msg: str = 'ircplom says bye') -> None: + 'Send QUIT command to server.' + self._broadcast('SEND', + (self._conn_idx, IrcMessage('QUIT', [quit_msg]))) + + def cmd__reconnect(self) -> None: + 'Attempt reconnection.' + self._broadcast('INIT_RECONNECTION', (self._conn_idx,)) + + class TuiLoop(Loop): 'Loop for drawing/updating TUI.' @@ -639,6 +665,7 @@ class TuiLoop(Loop): self.put(Event('SET_SCREEN')) def _cmd_name_to_cmd(self, cmd_name: str) -> Optional[Callable]: + cmd_name = CMD_SHORTCUTS.get(cmd_name, cmd_name) cmd_parent = self while True: cmd_name_toks = cmd_name.split('.', maxsplit=1) @@ -662,7 +689,8 @@ class TuiLoop(Loop): window.set_geometry() self.window.draw() elif event.type_ == 'CONNECTION_WINDOW': - conn_win = Window(len(self._windows), self._term) + conn_win = ConnectionWindow(self.broadcast, event.payload[0], + len(self._windows), self._term) self._windows += [conn_win] self._conn_windows += [conn_win] self._switch_window(conn_win.idx) @@ -705,14 +733,6 @@ class TuiLoop(Loop): self._window_idx = idx self.window.draw() - def cmd__reconnect(self) -> Optional[str]: - 'Send INIT_RECONNECTION to server if in connection window.' - if self.window not in self._conn_windows: - return 'can only reconnect from inside connection window.' - conn_idx = self._conn_windows.index(self.window) - self.broadcast('INIT_RECONNECTION', (conn_idx,)) - return None - def cmd__connect(self, hostname: str, username: str, @@ -723,16 +743,6 @@ class TuiLoop(Loop): self.broadcast('INIT_CONNECTION', (hostname, (username, nickname, realname))) - def cmd__disconnect(self, - quit_msg: str = 'ircplom says bye' - ) -> Optional[str]: - 'Send QUIT command to server if in connection window.' - if self.window not in self._conn_windows: - return 'what to disconnect from? (not in connection window!)' - self.broadcast('SEND', (self._conn_windows.index(self.window), - IrcMessage('QUIT', [quit_msg]))) - return None - def cmd__prompt_enter(self) -> None: 'Get prompt content from .window.prompt.enter, parse to & run command.' to_parse = self.window.prompt.enter()