add command history

minor bump
This commit is contained in:
2026-03-28 15:48:34 +00:00
parent 5178b0066b
commit 225685de63
3 changed files with 52 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2026-present onyx-and-iris <code@onyxandiris.online> # SPDX-FileCopyrightText: 2026-present onyx-and-iris <code@onyxandiris.online>
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
__version__ = '0.7.2' __version__ = '0.8.0'

23
src/q3rcon_tui/history.py Normal file
View File

@@ -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 ''

View File

@@ -4,6 +4,7 @@ from textual.containers import Grid
from textual.widgets import Button, Input, RichLog from textual.widgets import Button, Input, RichLog
from .configscreen import ConfigScreen from .configscreen import ConfigScreen
from .history import CommandHistory
from .settings import Settings from .settings import Settings
from .writable import Writable from .writable import Writable
@@ -24,6 +25,8 @@ class Q3RconTUI(App):
super().__init__() super().__init__()
self._settings = Settings() self._settings = Settings()
self.writable = Writable(self) self.writable = Writable(self)
self._command_history = CommandHistory()
self._history_index = None
def compose(self) -> ComposeResult: def compose(self) -> ComposeResult:
yield Grid( yield Grid(
@@ -40,9 +43,33 @@ class Q3RconTUI(App):
if self.screen and isinstance(self.screen, ConfigScreen): if self.screen and isinstance(self.screen, ConfigScreen):
return return
command_input = self.query_one('#command', Input)
match event.key: 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() 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': case 'f2':
self.query_one('#config', Button).press() self.query_one('#config', Button).press()