This commit is contained in:
2025-10-22 13:40:26 +03:00
parent f38da15bdb
commit 4f5481fa70
8 changed files with 386 additions and 120 deletions
+14 -3
View File
@@ -7,7 +7,8 @@ from typing import Never
class AutoCompleter:
def __init__(
self, history_filename: str | None = None, autocomplete_button: str = "tab"
self, history_filename: str | None = None,
autocomplete_button: str = "tab"
) -> None:
"""
Public. Configures and implements auto-completion of input command
@@ -60,12 +61,16 @@ class AutoCompleter:
else:
for line in all_commands:
readline.add_history(line)
if not self.history_filename:
for line in all_commands:
readline.add_history(line)
readline.set_completer(self._complete)
readline.set_completer_delims(readline.get_completer_delims().replace(" ", ""))
readline.parse_and_bind(f"{self.autocomplete_button}: complete")
def exit_setup(self, all_commands: list[str]) -> None:
def exit_setup(self, all_commands: list[str], ignore_command_register: bool) -> None:
"""
Private. Exit setup function
:return: None
@@ -76,10 +81,16 @@ class AutoCompleter:
raw_history = history_file.read()
pretty_history: list[str] = []
for line in set(raw_history.strip().split("\n")):
if line.split()[0] in all_commands:
if _is_command_exist(line.split()[0], all_commands, ignore_command_register):
pretty_history.append(line)
with open(self.history_filename, "w") as history_file:
_ = history_file.write("\n".join(pretty_history))
def _is_command_exist(command: str, existing_commands: list[str], ignore_command_register: bool) -> bool:
if ignore_command_register:
return command.lower() in existing_commands
return command in existing_commands
def _get_history_items() -> list[str] | list[Never]:
"""
+2 -1
View File
@@ -465,7 +465,8 @@ class App(BaseApp):
if self._is_exit_command(input_command):
system_router.finds_appropriate_handler(input_command)
self._autocompleter.exit_setup(
list(self._current_matching_triggers_with_routers.keys())
list(self._current_matching_triggers_with_routers.keys()),
self._ignore_command_register
)
return