diff --git a/services/tui_service.py b/services/tui_service.py index 23508eb..3035b74 100644 --- a/services/tui_service.py +++ b/services/tui_service.py @@ -9,6 +9,8 @@ from textual.app import App from textual.containers import Container, ScrollableContainer, Horizontal from textual.widgets import Static, Input, Header, Footer, LoadingIndicator from textual.reactive import reactive +from textual.suggester import Suggester +from textual.suggestion import Suggestion from typing import List, Dict import asyncio from datetime import datetime @@ -493,6 +495,37 @@ class PluginStatusPanel(Static): pass +class CommandSuggester(Suggester): + """命令补全 — 根据已注册命令提供 Tab 补全建议""" + + def __init__(self, command_service=None): + self._cmd_svc = command_service + + def set_command_service(self, svc): + self._cmd_svc = svc + + async def get_suggestion(self, value: str) -> Suggestion | None: + """根据当前输入返回匹配的命令建议""" + if not value or not self._cmd_svc: + return None + value_lower = value.lower().strip() + commands = getattr(self._cmd_svc, "commands", {}) + # 优先前缀匹配 + candidates = sorted( + [n for n in commands if n.startswith(value_lower)], + key=len, + ) + if not candidates: + candidates = sorted( + [n for n in commands if value_lower in n], + key=len, + ) + if candidates: + cmd = candidates[0] + return Suggestion(cmd[len(value_lower):]) + return None + + class TUIFramework(App): """TUI框架应用""" def __init__(self, config, log_service, command_service, plugin_service=None): @@ -623,7 +656,12 @@ class TUIFramework(App): self.message_display, id="message-area" ) - self.command_input = Input(placeholder="输入指令...", id="command-input") + self.command_suggester = CommandSuggester(self.command_service) + self.command_input = Input( + placeholder="输入指令... Tab 补全", + suggester=self.command_suggester, + id="command-input", + ) yield Container( self.command_input, id="input-area"