This commit is contained in:
2025-12-08 21:49:46 +03:00
parent 22970f7115
commit 725a1f2e40
12 changed files with 76 additions and 61 deletions
+21 -6
View File
@@ -6,7 +6,9 @@ from typing import Never
class AutoCompleter:
def __init__(self, history_filename: str | None = None, autocomplete_button: str = "tab") -> None:
def __init__(
self, history_filename: str | None = None, autocomplete_button: str = "tab"
) -> None:
"""
Public. Configures and implements auto-completion of input command
:param history_filename: the name of the file for saving the history of the autocompleter
@@ -23,12 +25,18 @@ class AutoCompleter:
:param state: the current cursor position is relative to the beginning of the line
:return: the desired candidate as str or None
"""
matches: list[str] = sorted(cmd for cmd in _get_history_items() if cmd.startswith(text))
matches: list[str] = sorted(
cmd for cmd in _get_history_items() if cmd.startswith(text)
)
if len(matches) > 1:
common_prefix = matches[0]
for match in matches[1:]:
i = 0
while i < len(common_prefix) and i < len(match) and common_prefix[i] == match[i]:
while (
i < len(common_prefix)
and i < len(match)
and common_prefix[i] == match[i]
):
i += 1
common_prefix = common_prefix[:i]
if state == 0:
@@ -72,13 +80,17 @@ class AutoCompleter:
raw_history = history_file.read()
pretty_history: list[str] = []
for line in set(raw_history.strip().split("\n")):
if _is_command_exist(line.split()[0], all_commands, ignore_command_register):
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:
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
@@ -89,4 +101,7 @@ def _get_history_items() -> list[str] | list[Never]:
Private. Returns a list of all commands entered by the user
:return: all commands entered by the user as list[str] | list[Never]
"""
return [readline.get_history_item(i) for i in range(1, readline.get_current_history_length() + 1)]
return [
readline.get_history_item(i)
for i in range(1, readline.get_current_history_length() + 1)
]
-2
View File
@@ -272,7 +272,6 @@ class BaseApp:
def _(response: Response) -> None:
self._exit_command_handler(response)
self.system_router.command_register_ignore = self._ignore_command_register
self.registered_routers.add_registered_router(self.system_router)
def _validate_routers_for_collisions(self) -> None:
@@ -519,7 +518,6 @@ class App(BaseApp):
:param router: registered router
:return: None
"""
router.command_register_ignore = self._ignore_command_register
self.registered_routers.add_registered_router(router)
def include_routers(self, *routers: Router) -> None:
-1
View File
@@ -2,7 +2,6 @@ __all__ = ["NonStandardBehaviorHandler", "EmptyCommandHandler", "Printer", "Desc
from typing import Protocol, TypeVar
T = TypeVar("T", contravariant=True) # noqa: WPS111
+4 -2
View File
@@ -1,18 +1,20 @@
__all__ = ["RegisteredRouters"]
from typing import Iterator, Optional
from typing import Iterator
from argenta.router import Router
class RegisteredRouters:
def __init__(self, registered_routers: Optional[list[Router]] = None) -> None:
def __init__(self, registered_routers: list[Router] | None = None) -> None:
"""
Private. Combines registered routers
:param registered_routers: list of the registered routers
:return: None
"""
self.registered_routers: list[Router] = registered_routers if registered_routers else []
self._matching_lower_triggers_with_routers
def add_registered_router(self, router: Router, /) -> None:
"""