From 225685de63bf36761d40e578bcb9414db724006f Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Sat, 28 Mar 2026 15:48:34 +0000 Subject: [PATCH] add command history minor bump --- src/q3rcon_tui/__about__.py | 2 +- src/q3rcon_tui/history.py | 23 +++++++++++++++++++++++ src/q3rcon_tui/tui.py | 29 ++++++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/q3rcon_tui/history.py diff --git a/src/q3rcon_tui/__about__.py b/src/q3rcon_tui/__about__.py index 5e3b362..bb12a3f 100644 --- a/src/q3rcon_tui/__about__.py +++ b/src/q3rcon_tui/__about__.py @@ -1,4 +1,4 @@ # SPDX-FileCopyrightText: 2026-present onyx-and-iris # # SPDX-License-Identifier: MIT -__version__ = '0.7.2' +__version__ = '0.8.0' diff --git a/src/q3rcon_tui/history.py b/src/q3rcon_tui/history.py new file mode 100644 index 0000000..6a33598 --- /dev/null +++ b/src/q3rcon_tui/history.py @@ -0,0 +1,23 @@ +from collections import UserList + + +class CommandHistory(UserList): + """A simple list to store command history.""" + + def add(self, command: str): + """Add a command to the history if it's not empty and not a duplicate of the last.""" + command = command.strip() + if command and (not self.data or command != self.data[-1]): + self.data.append(command) + + def get_previous(self, index: int) -> str: + """Get the previous command based on the current index.""" + if 0 <= index < len(self.data): + return self.data[index] + return '' + + def get_next(self, index: int) -> str: + """Get the next command based on the current index.""" + if 0 <= index < len(self.data): + return self.data[index] + return '' diff --git a/src/q3rcon_tui/tui.py b/src/q3rcon_tui/tui.py index 1c43c72..e1ba4dc 100644 --- a/src/q3rcon_tui/tui.py +++ b/src/q3rcon_tui/tui.py @@ -4,6 +4,7 @@ from textual.containers import Grid from textual.widgets import Button, Input, RichLog from .configscreen import ConfigScreen +from .history import CommandHistory from .settings import Settings from .writable import Writable @@ -24,6 +25,8 @@ class Q3RconTUI(App): super().__init__() self._settings = Settings() self.writable = Writable(self) + self._command_history = CommandHistory() + self._history_index = None def compose(self) -> ComposeResult: yield Grid( @@ -40,9 +43,33 @@ class Q3RconTUI(App): if self.screen and isinstance(self.screen, ConfigScreen): return + command_input = self.query_one('#command', Input) match event.key: - case 'enter' if self.query_one('#command', Input).has_focus: + case 'enter' if command_input.has_focus: + value = command_input.value.strip() + if value: + self._command_history.add(value) + self._history_index = None self.query_one('#send', Button).press() + case 'up' if command_input.has_focus: + if self._command_history: + if self._history_index is None: + self._history_index = len(self._command_history) - 1 + elif self._history_index > 0: + self._history_index -= 1 + command_input.value = self._command_history.get_previous( + self._history_index + ) + case 'down' if command_input.has_focus: + if self._command_history and self._history_index is not None: + if self._history_index < len(self._command_history) - 1: + self._history_index += 1 + command_input.value = self._command_history.get_next( + self._history_index + ) + else: + self._history_index = None + command_input.value = '' case 'f2': self.query_one('#config', Button).press()