Merge branch 'main' into benchmarks

This commit is contained in:
2026-01-22 04:56:27 +03:00
9 changed files with 337 additions and 333 deletions
@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: Argenta \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-12-02 22:27+0300\n"
"POT-Creation-Date: 2026-01-22 04:26+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: en\n"
@@ -29,92 +29,107 @@ msgid ""
"подсказки и завершая ввод на основе истории команд, что ускоряет работу и"
" снижает вероятность опечаток."
msgstr ""
"``AutoCompleter`` is a component responsible for interactive command autocompletion. "
"It improves user experience by offering suggestions and completing input based on "
"command history, which speeds up work and reduces the likelihood of typos."
"``AutoCompleter`` is a component responsible for interactive command "
"autocompletion. It improves user experience by offering suggestions and "
"completing input based on command history, which speeds up work and "
"reduces the likelihood of typos."
#: ../../root/api/app/autocompleter.rst:11
msgid "Инициализация"
msgstr "Initialization"
#: ../../root/api/app/autocompleter.rst:18
#: ../../root/api/app/autocompleter.rst:21
msgid "Создаёт и настраивает экземпляр ``AutoCompleter``."
msgstr "Creates and configures an ``AutoCompleter`` instance."
#: ../../root/api/app/autocompleter.rst:20
#: ../../root/api/app/autocompleter.rst:23
msgid ""
"``history_filename``: Имя файла для сохранения истории команд. Если "
"указано, история будет сохраняться между сессиями. При значении ``None`` "
"история хранится только в контексте сессии."
msgstr ""
"``history_filename``: Filename for saving command history. If specified, history "
"will be saved between sessions. When set to ``None``, history is stored only within "
"the session context."
"``history_filename``: Filename for saving command history. If specified, "
"history will be saved between sessions. When set to ``None``, history is "
"stored only within the session context."
#: ../../root/api/app/autocompleter.rst:21
#: ../../root/api/app/autocompleter.rst:24
msgid ""
"``autocomplete_button``: Клавиша, активирующая автодополнение. По "
"умолчанию — **Tab**."
msgstr ""
"``autocomplete_button``: Key that activates autocompletion. Defaults to **Tab**."
"``autocomplete_button``: Key that activates autocompletion. Defaults to "
"**Tab**."
#: ../../root/api/app/autocompleter.rst:25
msgid ""
"``command_highlighting``: Если True, то в реальном времени при вводе "
"команды она будет подсвечиваться: зелёным, если такой триггер существует "
"и красный, если нет."
msgstr ""
"``command_highlighting``: If True, then in real time, when entering a "
" command, it will be highlighted: green if such a trigger exists "
"and red if not."
#: ../../root/api/app/autocompleter.rst:26
msgid ""
"``auto_suggestions``: Если True, то дополнение до раннее введённой "
"команды будет сразу отображаться светло-серым в строке ввода."
msgstr ""
"``auto_suggestions``: If True, the addition to the previously entered "
" command will immediately be displayed in light gray in the input line."
#: ../../root/api/app/autocompleter.rst:31
msgid "Назначение и возможности"
msgstr "Purpose and Features"
#: ../../root/api/app/autocompleter.rst:28
#: ../../root/api/app/autocompleter.rst:33
msgid "Основные возможности ``AutoCompleter``:"
msgstr "Main features of ``AutoCompleter``:"
#: ../../root/api/app/autocompleter.rst:30
#: ../../root/api/app/autocompleter.rst:35
msgid ""
"**Автодополнение по истории**: При нажатии клавиши автодополнения (по "
"умолчанию **Tab**) система ищет в истории команды, начинающиеся с уже "
"введённого текста."
msgstr ""
"**History-based autocompletion**: When the autocompletion key is pressed (by default **Tab**), "
"the system searches history for commands starting with the already entered text."
"**History-based autocompletion**: When the autocompletion key is pressed "
"(by default **Tab**), the system searches history for commands starting "
"with the already entered text."
#: ../../root/api/app/autocompleter.rst:32
#: ../../root/api/app/autocompleter.rst:37
msgid ""
"**Общий префикс**: Если найдено несколько команд с общим префиксом, будет"
" подставлена только общая часть. Например, для команд ``show_users`` и "
"``show_profile`` при вводе ``sho`` и нажатии **Tab** ввод дополнится до "
"``show_``."
msgstr ""
"**Common prefix**: If multiple commands with a common prefix are found, only the common "
"part will be inserted. For example, for commands ``show_users`` and ``show_profile``, "
"when entering ``sho`` and pressing **Tab**, the input will be completed to ``show_``."
"**Common prefix**: If multiple commands with a common prefix are found, "
"only the common part will be inserted. For example, for commands "
"``show_users`` and ``show_profile``, when entering ``sho`` and pressing "
"**Tab**, the input will be completed to ``show_``."
#: ../../root/api/app/autocompleter.rst:34
#: ../../root/api/app/autocompleter.rst:39
msgid ""
"**Постоянная история**: Если указан ``history_filename``, история команд "
"сохраняется в файл при выходе и загружается при следующем запуске. Это "
"делает автодополнение со временем «умнее»."
msgstr ""
"**Persistent history**: If ``history_filename`` is specified, command history is saved "
"to a file on exit and loaded on the next startup. This makes autocompletion \"smarter\" over time."
"**Persistent history**: If ``history_filename`` is specified, command "
"history is saved to a file on exit and loaded on the next startup. This "
"makes autocompletion \"smarter\" over time."
#: ../../root/api/app/autocompleter.rst:36
msgid ""
"**Очистка истории**: При сохранении ``AutoCompleter`` удаляет дубликаты и"
" несуществующие команды, поддерживая историю в актуальном состоянии."
msgstr ""
"**History cleanup**: When saving, ``AutoCompleter`` removes duplicates and non-existent "
"commands, keeping the history up to date."
#: ../../root/api/app/autocompleter.rst:38
#: ../../root/api/app/autocompleter.rst:41
msgid ""
"**Настройка клавиши**: Клавишу автодополнения можно изменить с помощью "
"параметра ``autocomplete_button``."
msgstr ""
"**Key customization**: The autocompletion key can be changed using the ``autocomplete_button`` parameter."
"**Key customization**: The autocompletion key can be changed using the "
"``autocomplete_button`` parameter."
#: ../../root/api/app/autocompleter.rst:43
#: ../../root/api/app/autocompleter.rst:46
msgid "Пример использования"
msgstr "Usage Example"
#: ../../root/api/app/autocompleter.rst:45
#: ../../root/api/app/autocompleter.rst:48
msgid "``AutoCompleter`` передаётся как аргумент при инициализации `App`."
msgstr "``AutoCompleter`` is passed as an argument when initializing `App`."
+7 -4
View File
@@ -12,13 +12,18 @@ AutoCompleter
.. code-block:: python
__init__(self, history_filename: str | None = None,
autocomplete_button: str = "tab") -> None
__init__(self,
history_filename: str | None = None,
autocomplete_button: str = "tab",
command_highlighting: bool = True,
auto_suggestions: bool = True) -> None:
Создаёт и настраивает экземпляр ``AutoCompleter``.
* ``history_filename``: Имя файла для сохранения истории команд. Если указано, история будет сохраняться между сессиями. При значении ``None`` история хранится только в контексте сессии.
* ``autocomplete_button``: Клавиша, активирующая автодополнение. По умолчанию — **Tab**.
* ``command_highlighting``: Если True, то в реальном времени при вводе команды она будет подсвечиваться: зелёным, если такой триггер существует и красный, если нет.
* ``auto_suggestions``: Если True, то дополнение до раннее введённой команды будет сразу отображаться светло-серым в строке ввода.
-----
@@ -33,8 +38,6 @@ AutoCompleter
* **Постоянная история**: Если указан ``history_filename``, история команд сохраняется в файл при выходе и загружается при следующем запуске. Это делает автодополнение со временем «умнее».
* **Очистка истории**: При сохранении ``AutoCompleter`` удаляет дубликаты и несуществующие команды, поддерживая историю в актуальном состоянии.
* **Настройка клавиши**: Клавишу автодополнения можно изменить с помощью параметра ``autocomplete_button``.
-----
+13 -5
View File
@@ -1,6 +1,14 @@
a = BrokenPipeError()
def q(f):
print(id(f))
from argenta.app import AutoCompleter
print(id(a))
q(a)
if __name__ == "__main__":
test_commands: set[str] = {"start", "qwertyu", "stop", "exit"}
hist_file: str = "history.txt"
ac: AutoCompleter = AutoCompleter(autocomplete_button='tab')
ac.initial_setup(test_commands)
while True:
inp: str = ac.prompt(">>> ").strip()
if inp == "exit":
break
+1 -1
View File
@@ -9,8 +9,8 @@ license = { text = "MIT" }
dependencies = [
"rich (>=14.0.0,<15.0.0)",
"art (>=6.4,<7.0)",
"pyreadline3>=3.5.4; sys_platform == 'win32'",
"dishka>=1.7.2",
"prompt-toolkit>=3.0.52",
]
[dependency-groups]
+120 -79
View File
@@ -1,97 +1,138 @@
__all__ = ["AutoCompleter"]
import os
import readline
from typing import Never
import sys
from typing import Callable, Iterable
from prompt_toolkit import PromptSession, HTML
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.completion import Completer, Completion, CompleteEvent
from prompt_toolkit.document import Document
from prompt_toolkit.formatted_text import StyleAndTextTuples
from prompt_toolkit.history import History, ThreadedHistory, FileHistory, InMemoryHistory
from prompt_toolkit.key_binding import KeyBindings, KeyPressEvent
from prompt_toolkit.lexers import Lexer
from prompt_toolkit.styles import Style
class CommandLexer(Lexer):
def __init__(self, valid_commands: set[str]) -> None:
self.valid_commands: set[str] = valid_commands
def lex_document(self, document: Document) -> Callable[[int], StyleAndTextTuples]:
def get_line_tokens(lineno: int) -> StyleAndTextTuples:
if lineno >= len(document.lines):
return []
line_text: str = document.lines[lineno]
if not line_text.strip():
return [("", line_text)]
first_word: str = line_text.split()[0] if line_text.split() else ""
if first_word in self.valid_commands:
return [("class:valid", line_text)]
else:
return [("class:invalid", line_text)]
return get_line_tokens
class HistoryCompleter(Completer):
def __init__(self, history_container: History, static_commands: set[str]) -> None:
self.history_container: History = history_container
self.static_commands: set[str] = static_commands
def get_completions(self, document: Document, complete_event: CompleteEvent) -> Iterable[Completion]:
text: str = document.text_before_cursor
history_items: set[str] = set(self.history_container.load_history_strings())
all_candidates: set[str] = history_items.union(self.static_commands)
matches: list[str] = sorted(cmd for cmd in all_candidates if cmd.startswith(text))
if not matches:
return
for match in matches:
yield Completion(
match,
start_position=-len(text),
display=match
)
@staticmethod
def _find_common_prefix(matches: list[str]) -> str:
if not matches:
return ""
common: str = matches[0]
for match in matches[1:]:
i: int = 0
while i < len(common) and i < len(match) and common[i] == match[i]:
i += 1
common = common[:i]
return common
class AutoCompleter:
def __init__(
self, history_filename: str | None = None, autocomplete_button: str = "tab"
self,
history_filename: str | None = None,
autocomplete_button: str = "tab",
command_highlighting: bool = True,
auto_suggestions: bool = True,
) -> 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
:param autocomplete_button: the button for auto-completion
:return: None
"""
self.history_filename: str | None = history_filename
self.autocomplete_button: str = autocomplete_button
def _complete(self, text: str, state: int) -> str | None:
"""
Private. Auto-completion function
:param text: part of the command being entered
: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)
)
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]
):
i += 1
common_prefix = common_prefix[:i]
if state == 0:
readline.insert_text(common_prefix[len(text) :])
readline.redisplay()
return None
elif len(matches) == 1:
return matches[0] if state == 0 else None
else:
return None
self.command_highlighting: bool = command_highlighting
self.auto_suggestions: bool = auto_suggestions
self._session: PromptSession[str] | None = None
self._fallback_mode: bool = False
def initial_setup(self, all_commands: set[str]) -> None:
"""
Private. Initial setup function
:param all_commands: Registered commands for adding them to the autocomplete history
:return: None
"""
if self.history_filename:
if os.path.exists(self.history_filename):
readline.read_history_file(self.history_filename)
if not sys.stdin.isatty():
self._session = None
self._fallback_mode = True
return
kb = KeyBindings()
def _(event: KeyPressEvent) -> None:
buff = event.app.current_buffer
if buff.complete_state:
buff.complete_next()
else:
for line in all_commands:
readline.add_history(line)
completions = list(buff.completer.get_completions(buff.document, CompleteEvent()))
if len(completions) == 1:
buff.apply_completion(completions[0])
else:
buff.start_completion(select_first=False)
if not self.history_filename:
for line in all_commands:
readline.add_history(line)
kb.add(self.autocomplete_button)(_)
readline.set_completer(self._complete)
readline.set_completer_delims(readline.get_completer_delims().replace(" ", ""))
readline.parse_and_bind(f"{self.autocomplete_button}: complete")
history: InMemoryHistory | ThreadedHistory
def exit_setup(self, all_commands: set[str]) -> None:
"""
Private. Exit setup function
:return: None
"""
if self.history_filename:
readline.write_history_file(self.history_filename)
with open(self.history_filename, "r") as history_file:
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:
pretty_history.append(line)
with open(self.history_filename, "w") as history_file:
_ = history_file.write("\n".join(pretty_history))
history = ThreadedHistory(FileHistory(self.history_filename))
else:
history = InMemoryHistory()
style = Style.from_dict({'valid': '#00ff00', 'invalid': '#ff0000'})
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)
]
self._session = PromptSession(
history=history,
completer=HistoryCompleter(history, all_commands),
complete_while_typing=False,
key_bindings=kb,
auto_suggest=AutoSuggestFromHistory() if self.auto_suggestions else None,
style=style if self.command_highlighting else style,
lexer=CommandLexer(all_commands) if self.command_highlighting else None,
)
def prompt(self, prompt_text: str | HTML = ">>> ") -> str:
if self._fallback_mode:
return input(prompt_text if isinstance(prompt_text, str) else ">>> ")
if self._session is None:
raise RuntimeError("Call initial_setup() before using prompt()")
return self._session.prompt(
HTML(prompt_text) if isinstance(prompt_text, str) else prompt_text
)
+6 -6
View File
@@ -6,6 +6,7 @@ from contextlib import redirect_stdout
from typing import Callable, Never, TypeAlias
from art import text2art
from prompt_toolkit import HTML
from rich.console import Console
from rich.markup import escape
@@ -37,7 +38,7 @@ class BaseApp:
def __init__(
self,
*,
prompt: str,
prompt: str | HTML,
initial_message: str,
farewell_message: str,
exit_command: Command,
@@ -48,7 +49,7 @@ class BaseApp:
autocompleter: AutoCompleter,
print_func: Printer,
) -> None:
self._prompt: str = prompt
self._prompt: str | HTML = prompt
self._print_func: Printer = print_func
self._exit_command: Command = exit_command
self._dividing_line: StaticDividingLine | DynamicDividingLine = dividing_line
@@ -307,7 +308,7 @@ class BaseApp:
Private. Sets up default app view
:return: None
"""
self._prompt = f"[italic dim bold]{self._prompt}[/italic dim bold]"
self._prompt = f"<gray><b>{self._prompt}</b></gray>"
self._initial_message = (
"\n" + f"[bold red]{text2art(self._initial_message, font='tarty1')}" + "\n"
)
@@ -404,7 +405,7 @@ class App(BaseApp):
def __init__(
self,
*,
prompt: str = "What do you want to do?\n\n",
prompt: str | HTML = ">>> ",
initial_message: str = "Argenta\n",
farewell_message: str = "\nSee you\n",
exit_command: Command = DEFAULT_EXIT_COMMAND,
@@ -455,7 +456,7 @@ class App(BaseApp):
if self._repeat_command_groups_printing:
self._print_command_group_description()
raw_command: str = Console().input(self._prompt)
raw_command: str = self._autocompleter.prompt(self._prompt)
try:
input_command: InputCommand = InputCommand.parse(raw_command=raw_command)
@@ -468,7 +469,6 @@ class App(BaseApp):
if self._is_exit_command(input_command):
self.system_router.finds_appropriate_handler(input_command)
self._autocompleter.exit_setup(self.registered_routers.get_triggers())
return
if self._is_unknown_command(input_command):
+1 -1
View File
@@ -361,7 +361,7 @@ def test_set_exit_command_handler_stores_handler() -> None:
def test_setup_default_view_formats_prompt() -> None:
app = App(prompt='>>')
assert app._prompt == '[italic dim bold]>>[/italic dim bold]'
assert app._prompt == '<gray><b>>></b></gray>'
def test_setup_default_view_sets_default_unknown_command_handler() -> None:
+122 -152
View File
@@ -1,192 +1,162 @@
import os
from typing import Any
import pytest
from pyfakefs.fake_filesystem import FakeFilesystem
from pytest_mock import MockerFixture
from prompt_toolkit.document import Document
from prompt_toolkit.history import InMemoryHistory
from argenta.app.autocompleter.entity import (
AutoCompleter,
_get_history_items
CommandLexer,
HistoryCompleter
)
HISTORY_FILE: str = "test_history.txt"
COMMANDS: set[str] = {"start", "stop", "status"}
# ============================================================================
# Fixtures
# ============================================================================
@pytest.fixture
def mock_readline(mocker: MockerFixture) -> Any:
_history: list[str] = []
def add_history(item: str) -> None:
_history.append(item)
def get_history_item(index: int) -> str | None:
if 1 <= index <= len(_history):
return _history[index - 1]
return None
def get_current_history_length() -> int:
return len(_history)
def clear_history() -> None:
_history.clear()
mock: Any = mocker.MagicMock() # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
mocker.patch('argenta.app.autocompleter.entity.readline', mock) # pyright: ignore[reportUnknownArgumentType]
mock.reset_mock() # pyright: ignore[reportUnknownMemberType]
clear_history()
mock.add_history.side_effect = add_history # pyright: ignore[reportUnknownMemberType]
mock.get_history_item.side_effect = get_history_item # pyright: ignore[reportUnknownMemberType]
mock.get_current_history_length.side_effect = get_current_history_length # pyright: ignore[reportUnknownMemberType]
mock.get_completer_delims.return_value = " " # pyright: ignore[reportUnknownMemberType]
return mock # pyright: ignore[reportReturnType, reportUnknownVariableType]
# ============================================================================
# Tests for AutoCompleter initialization
# ============================================================================
def test_autocompleter_initializes_with_history_file_and_button() -> None:
completer: AutoCompleter = AutoCompleter(history_filename=HISTORY_FILE, autocomplete_button="tab")
assert completer.history_filename == HISTORY_FILE
def test_autocompleter_initializes_with_default_params() -> None:
completer = AutoCompleter()
assert completer.history_filename is None
assert completer.autocomplete_button == "tab"
assert completer.command_highlighting is True
assert completer.auto_suggestions is True
# ============================================================================
# Tests for initial setup
# ============================================================================
def test_autocompleter_initializes_with_custom_params() -> None:
completer = AutoCompleter(
history_filename="test.txt",
autocomplete_button="c-space",
command_highlighting=False,
auto_suggestions=False
)
assert completer.history_filename == "test.txt"
assert completer.autocomplete_button == "c-space"
assert completer.command_highlighting is False
assert completer.auto_suggestions is False
def test_initial_setup_creates_history_when_file_does_not_exist(fs: FakeFilesystem, mock_readline: Any) -> None:
if os.path.exists(HISTORY_FILE):
os.remove(HISTORY_FILE)
completer: AutoCompleter = AutoCompleter(history_filename=HISTORY_FILE)
completer.initial_setup(COMMANDS)
mock_readline.read_history_file.assert_not_called()
assert mock_readline.add_history.call_count == len(COMMANDS)
mock_readline.set_completer.assert_called_with(completer._complete)
mock_readline.parse_and_bind.assert_called_with("tab: complete")
def test_prompt_raises_error_without_initial_setup() -> None:
completer = AutoCompleter()
with pytest.raises(RuntimeError, match="Call initial_setup"):
completer.prompt(">>> ")
def test_initial_setup_reads_existing_history_file(fs: FakeFilesystem, mock_readline: Any) -> None:
fs.create_file(HISTORY_FILE, contents="previous_command\n") # pyright: ignore[reportUnknownMemberType]
completer: AutoCompleter = AutoCompleter(history_filename=HISTORY_FILE)
completer.initial_setup(COMMANDS)
mock_readline.read_history_file.assert_called_once_with(HISTORY_FILE)
mock_readline.add_history.assert_not_called()
mock_readline.set_completer.assert_called_once()
mock_readline.parse_and_bind.assert_called_once()
def test_command_lexer_highlights_valid_command() -> None:
lexer = CommandLexer({"start", "stop"})
doc = Document("start server")
tokens = lexer.lex_document(doc)(0)
assert tokens == [("class:valid", "start server")]
def test_initial_setup_works_without_history_filename(mock_readline: Any) -> None:
completer: AutoCompleter = AutoCompleter(history_filename=None)
completer.initial_setup(COMMANDS)
mock_readline.read_history_file.assert_not_called()
assert mock_readline.add_history.call_count == len(COMMANDS)
def test_command_lexer_highlights_invalid_command() -> None:
lexer = CommandLexer({"start", "stop"})
doc = Document("invalid command")
tokens = lexer.lex_document(doc)(0)
assert tokens == [("class:invalid", "invalid command")]
# ============================================================================
# Tests for exit setup and history filtering
# ============================================================================
def test_command_lexer_handles_empty_line() -> None:
lexer = CommandLexer({"start", "stop"})
doc = Document("")
tokens = lexer.lex_document(doc)(0)
assert tokens == [("", "")]
def test_exit_setup_writes_and_filters_duplicate_commands(fs: FakeFilesystem, mock_readline: Any) -> None:
mock_readline.add_history.side_effect = None
mock_readline.add_history("start server")
mock_readline.add_history("stop client")
mock_readline.add_history("invalid command")
mock_readline.add_history("start server")
raw_history_content: str = "\n".join(["start server", "stop client", "invalid command", "start server"])
fs.create_file(HISTORY_FILE, contents=raw_history_content) # pyright: ignore[reportUnknownMemberType]
completer: AutoCompleter = AutoCompleter(history_filename=HISTORY_FILE)
completer.exit_setup(all_commands={"start", "stop"})
mock_readline.write_history_file.assert_called_once_with(HISTORY_FILE)
with open(HISTORY_FILE) as f:
content: str = f.read()
lines: list[str] = sorted(content.strip().split("\n"))
assert lines == ["start server", "stop client"]
def test_command_lexer_handles_whitespace_only() -> None:
lexer = CommandLexer({"start", "stop"})
doc = Document(" ")
tokens = lexer.lex_document(doc)(0)
assert tokens == [("", " ")]
def test_exit_setup_skips_writing_when_no_history_filename(mock_readline: Any) -> None:
completer: AutoCompleter = AutoCompleter(history_filename=None)
completer.exit_setup(all_commands=COMMANDS)
mock_readline.write_history_file.assert_not_called()
def test_history_completer_returns_matching_commands() -> None:
history = InMemoryHistory()
history.append_string("start server")
history.append_string("stop server")
completer = HistoryCompleter(history, {"status"})
doc = Document("sta")
completions = list(completer.get_completions(doc, None))
completion_texts = [c.text for c in completions]
assert "start server" in completion_texts
assert "status" in completion_texts
assert "stop server" not in completion_texts
# ============================================================================
# Tests for autocomplete functionality
# ============================================================================
def test_history_completer_returns_all_when_empty_input() -> None:
history = InMemoryHistory()
history.append_string("start")
history.append_string("stop")
completer = HistoryCompleter(history, {"status"})
doc = Document("")
completions = list(completer.get_completions(doc, None))
completion_texts = [c.text for c in completions]
assert len(completion_texts) == 3
assert "start" in completion_texts
assert "stop" in completion_texts
assert "status" in completion_texts
def test_complete_returns_none_when_no_matches_found(mock_readline: Any) -> None:
cmd: str
for cmd in ["start", "stop"]:
mock_readline.add_history(cmd)
completer: AutoCompleter = AutoCompleter()
assert completer._complete("run", 0) is None
assert completer._complete("run", 1) is None
def test_history_completer_returns_empty_when_no_matches() -> None:
history = InMemoryHistory()
history.append_string("start")
completer = HistoryCompleter(history, {"stop"})
doc = Document("xyz")
completions = list(completer.get_completions(doc, None))
assert len(completions) == 0
def test_complete_returns_single_match(mock_readline: Any) -> None:
mock_readline.add_history("start server")
mock_readline.add_history("stop server")
completer: AutoCompleter = AutoCompleter()
assert completer._complete("start", 0) == "start server"
assert completer._complete("start", 1) is None
def test_history_completer_deduplicates_commands() -> None:
history = InMemoryHistory()
history.append_string("start")
history.append_string("start")
completer = HistoryCompleter(history, {"start"})
doc = Document("sta")
completions = list(completer.get_completions(doc, None))
assert len(completions) == 1
def test_complete_inserts_common_prefix_for_multiple_matches(mock_readline: Any) -> None:
mock_readline.add_history("status client")
mock_readline.add_history("status server")
mock_readline.add_history("stop")
completer: AutoCompleter = AutoCompleter()
result: str | None = completer._complete("stat", 0)
assert result is None
mock_readline.insert_text.assert_called_once_with("us ")
mock_readline.redisplay.assert_called_once()
mock_readline.reset_mock()
result_state_1: str | None = completer._complete("stat", 1)
assert result_state_1 is None
mock_readline.insert_text.assert_not_called()
def test_history_completer_sorts_results() -> None:
history = InMemoryHistory()
history.append_string("stop")
history.append_string("start")
history.append_string("status")
completer = HistoryCompleter(history, set())
doc = Document("st")
completions = list(completer.get_completions(doc, None))
completion_texts = [c.text for c in completions]
assert completion_texts == ["start", "status", "stop"]
# ============================================================================
# Tests for helper functions
# ============================================================================
def test_find_common_prefix_with_multiple_matches() -> None:
matches = ["start server", "start client", "start process"]
prefix = HistoryCompleter._find_common_prefix(matches)
assert prefix == "start "
def test_get_history_items_returns_empty_list_initially(mock_readline: Any) -> None:
assert _get_history_items() == []
def test_find_common_prefix_with_no_common() -> None:
matches = ["start", "stop", "status"]
prefix = HistoryCompleter._find_common_prefix(matches)
assert prefix == "st"
def test_get_history_items_returns_all_added_items(mock_readline: Any) -> None:
mock_readline.add_history("first item")
mock_readline.add_history("second item")
def test_find_common_prefix_with_single_match() -> None:
matches = ["start"]
prefix = HistoryCompleter._find_common_prefix(matches)
assert prefix == "start"
assert _get_history_items() == ["first item", "second item"]
def test_find_common_prefix_with_empty_list() -> None:
matches: list[str] = []
prefix = HistoryCompleter._find_common_prefix(matches)
assert prefix == ""
Generated
+17 -50
View File
@@ -44,7 +44,7 @@ source = { editable = "." }
dependencies = [
{ name = "art" },
{ name = "dishka" },
{ name = "pyreadline3", marker = "sys_platform == 'win32'" },
{ name = "prompt-toolkit" },
{ name = "rich" },
]
@@ -61,10 +61,6 @@ linters = [
{ name = "ruff" },
{ name = "wemake-python-styleguide" },
]
metrics = [
{ name = "psutil" },
{ name = "py-cpuinfo" },
]
tests = [
{ name = "pyfakefs" },
{ name = "pytest" },
@@ -79,7 +75,7 @@ typecheckers = [
requires-dist = [
{ name = "art", specifier = ">=6.4,<7.0" },
{ name = "dishka", specifier = ">=1.7.2" },
{ name = "pyreadline3", marker = "sys_platform == 'win32'", specifier = ">=3.5.4" },
{ name = "prompt-toolkit", specifier = ">=3.0.52" },
{ name = "rich", specifier = ">=14.0.0,<15.0.0" },
]
@@ -96,10 +92,6 @@ linters = [
{ name = "ruff", specifier = ">=0.12.12" },
{ name = "wemake-python-styleguide", specifier = ">=0.17.0" },
]
metrics = [
{ name = "psutil", specifier = ">=7.2.1" },
{ name = "py-cpuinfo", specifier = ">=9.0.0" },
]
tests = [
{ name = "pyfakefs", specifier = ">=5.5.0" },
{ name = "pytest", specifier = ">=8.3.2" },
@@ -598,40 +590,15 @@ wheels = [
]
[[package]]
name = "psutil"
version = "7.2.1"
name = "prompt-toolkit"
version = "3.0.52"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/73/cb/09e5184fb5fc0358d110fc3ca7f6b1d033800734d34cac10f4136cfac10e/psutil-7.2.1.tar.gz", hash = "sha256:f7583aec590485b43ca601dd9cea0dcd65bd7bb21d30ef4ddbf4ea6b5ed1bdd3", size = 490253, upload-time = "2025-12-29T08:26:00.169Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/77/8e/f0c242053a368c2aa89584ecd1b054a18683f13d6e5a318fc9ec36582c94/psutil-7.2.1-cp313-cp313t-macosx_10_13_x86_64.whl", hash = "sha256:ba9f33bb525b14c3ea563b2fd521a84d2fa214ec59e3e6a2858f78d0844dd60d", size = 129624, upload-time = "2025-12-29T08:26:04.255Z" },
{ url = "https://files.pythonhosted.org/packages/26/97/a58a4968f8990617decee234258a2b4fc7cd9e35668387646c1963e69f26/psutil-7.2.1-cp313-cp313t-macosx_11_0_arm64.whl", hash = "sha256:81442dac7abfc2f4f4385ea9e12ddf5a796721c0f6133260687fec5c3780fa49", size = 130132, upload-time = "2025-12-29T08:26:06.228Z" },
{ url = "https://files.pythonhosted.org/packages/db/6d/ed44901e830739af5f72a85fa7ec5ff1edea7f81bfbf4875e409007149bd/psutil-7.2.1-cp313-cp313t-manylinux2010_x86_64.manylinux_2_12_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:ea46c0d060491051d39f0d2cff4f98d5c72b288289f57a21556cc7d504db37fc", size = 180612, upload-time = "2025-12-29T08:26:08.276Z" },
{ url = "https://files.pythonhosted.org/packages/c7/65/b628f8459bca4efbfae50d4bf3feaab803de9a160b9d5f3bd9295a33f0c2/psutil-7.2.1-cp313-cp313t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:35630d5af80d5d0d49cfc4d64c1c13838baf6717a13effb35869a5919b854cdf", size = 183201, upload-time = "2025-12-29T08:26:10.622Z" },
{ url = "https://files.pythonhosted.org/packages/fb/23/851cadc9764edcc18f0effe7d0bf69f727d4cf2442deb4a9f78d4e4f30f2/psutil-7.2.1-cp313-cp313t-win_amd64.whl", hash = "sha256:923f8653416604e356073e6e0bccbe7c09990acef442def2f5640dd0faa9689f", size = 139081, upload-time = "2025-12-29T08:26:12.483Z" },
{ url = "https://files.pythonhosted.org/packages/59/82/d63e8494ec5758029f31c6cb06d7d161175d8281e91d011a4a441c8a43b5/psutil-7.2.1-cp313-cp313t-win_arm64.whl", hash = "sha256:cfbe6b40ca48019a51827f20d830887b3107a74a79b01ceb8cc8de4ccb17b672", size = 134767, upload-time = "2025-12-29T08:26:14.528Z" },
{ url = "https://files.pythonhosted.org/packages/05/c2/5fb764bd61e40e1fe756a44bd4c21827228394c17414ade348e28f83cd79/psutil-7.2.1-cp314-cp314t-macosx_10_15_x86_64.whl", hash = "sha256:494c513ccc53225ae23eec7fe6e1482f1b8a44674241b54561f755a898650679", size = 129716, upload-time = "2025-12-29T08:26:16.017Z" },
{ url = "https://files.pythonhosted.org/packages/c9/d2/935039c20e06f615d9ca6ca0ab756cf8408a19d298ffaa08666bc18dc805/psutil-7.2.1-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:3fce5f92c22b00cdefd1645aa58ab4877a01679e901555067b1bd77039aa589f", size = 130133, upload-time = "2025-12-29T08:26:18.009Z" },
{ url = "https://files.pythonhosted.org/packages/77/69/19f1eb0e01d24c2b3eacbc2f78d3b5add8a89bf0bb69465bc8d563cc33de/psutil-7.2.1-cp314-cp314t-manylinux2010_x86_64.manylinux_2_12_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:93f3f7b0bb07711b49626e7940d6fe52aa9940ad86e8f7e74842e73189712129", size = 181518, upload-time = "2025-12-29T08:26:20.241Z" },
{ url = "https://files.pythonhosted.org/packages/e1/6d/7e18b1b4fa13ad370787626c95887b027656ad4829c156bb6569d02f3262/psutil-7.2.1-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:d34d2ca888208eea2b5c68186841336a7f5e0b990edec929be909353a202768a", size = 184348, upload-time = "2025-12-29T08:26:22.215Z" },
{ url = "https://files.pythonhosted.org/packages/98/60/1672114392dd879586d60dd97896325df47d9a130ac7401318005aab28ec/psutil-7.2.1-cp314-cp314t-win_amd64.whl", hash = "sha256:2ceae842a78d1603753561132d5ad1b2f8a7979cb0c283f5b52fb4e6e14b1a79", size = 140400, upload-time = "2025-12-29T08:26:23.993Z" },
{ url = "https://files.pythonhosted.org/packages/fb/7b/d0e9d4513c46e46897b46bcfc410d51fc65735837ea57a25170f298326e6/psutil-7.2.1-cp314-cp314t-win_arm64.whl", hash = "sha256:08a2f175e48a898c8eb8eace45ce01777f4785bc744c90aa2cc7f2fa5462a266", size = 135430, upload-time = "2025-12-29T08:26:25.999Z" },
{ url = "https://files.pythonhosted.org/packages/c5/cf/5180eb8c8bdf6a503c6919f1da28328bd1e6b3b1b5b9d5b01ae64f019616/psutil-7.2.1-cp36-abi3-macosx_10_9_x86_64.whl", hash = "sha256:b2e953fcfaedcfbc952b44744f22d16575d3aa78eb4f51ae74165b4e96e55f42", size = 128137, upload-time = "2025-12-29T08:26:27.759Z" },
{ url = "https://files.pythonhosted.org/packages/c5/2c/78e4a789306a92ade5000da4f5de3255202c534acdadc3aac7b5458fadef/psutil-7.2.1-cp36-abi3-macosx_11_0_arm64.whl", hash = "sha256:05cc68dbb8c174828624062e73078e7e35406f4ca2d0866c272c2410d8ef06d1", size = 128947, upload-time = "2025-12-29T08:26:29.548Z" },
{ url = "https://files.pythonhosted.org/packages/29/f8/40e01c350ad9a2b3cb4e6adbcc8a83b17ee50dd5792102b6142385937db5/psutil-7.2.1-cp36-abi3-manylinux2010_x86_64.manylinux_2_12_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:5e38404ca2bb30ed7267a46c02f06ff842e92da3bb8c5bfdadbd35a5722314d8", size = 154694, upload-time = "2025-12-29T08:26:32.147Z" },
{ url = "https://files.pythonhosted.org/packages/06/e4/b751cdf839c011a9714a783f120e6a86b7494eb70044d7d81a25a5cd295f/psutil-7.2.1-cp36-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:ab2b98c9fc19f13f59628d94df5cc4cc4844bc572467d113a8b517d634e362c6", size = 156136, upload-time = "2025-12-29T08:26:34.079Z" },
{ url = "https://files.pythonhosted.org/packages/44/ad/bbf6595a8134ee1e94a4487af3f132cef7fce43aef4a93b49912a48c3af7/psutil-7.2.1-cp36-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:f78baafb38436d5a128f837fab2d92c276dfb48af01a240b861ae02b2413ada8", size = 148108, upload-time = "2025-12-29T08:26:36.225Z" },
{ url = "https://files.pythonhosted.org/packages/1c/15/dd6fd869753ce82ff64dcbc18356093471a5a5adf4f77ed1f805d473d859/psutil-7.2.1-cp36-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:99a4cd17a5fdd1f3d014396502daa70b5ec21bf4ffe38393e152f8e449757d67", size = 147402, upload-time = "2025-12-29T08:26:39.21Z" },
{ url = "https://files.pythonhosted.org/packages/34/68/d9317542e3f2b180c4306e3f45d3c922d7e86d8ce39f941bb9e2e9d8599e/psutil-7.2.1-cp37-abi3-win_amd64.whl", hash = "sha256:b1b0671619343aa71c20ff9767eced0483e4fc9e1f489d50923738caf6a03c17", size = 136938, upload-time = "2025-12-29T08:26:41.036Z" },
{ url = "https://files.pythonhosted.org/packages/3e/73/2ce007f4198c80fcf2cb24c169884f833fe93fbc03d55d302627b094ee91/psutil-7.2.1-cp37-abi3-win_arm64.whl", hash = "sha256:0d67c1822c355aa6f7314d92018fb4268a76668a536f133599b91edd48759442", size = 133836, upload-time = "2025-12-29T08:26:43.086Z" },
dependencies = [
{ name = "wcwidth" },
]
[[package]]
name = "py-cpuinfo"
version = "9.0.0"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/37/a8/d832f7293ebb21690860d2e01d8115e5ff6f2ae8bbdc953f0eb0fa4bd2c7/py-cpuinfo-9.0.0.tar.gz", hash = "sha256:3cdbbf3fac90dc6f118bfd64384f309edeadd902d7c8fb17f02ffa1fc3f49690", size = 104716, upload-time = "2022-10-25T20:38:06.303Z" }
sdist = { url = "https://files.pythonhosted.org/packages/a1/96/06e01a7b38dce6fe1db213e061a4602dd6032a8a97ef6c1a862537732421/prompt_toolkit-3.0.52.tar.gz", hash = "sha256:28cde192929c8e7321de85de1ddbe736f1375148b02f2e17edd840042b1be855", size = 434198, upload-time = "2025-08-27T15:24:02.057Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/e0/a9/023730ba63db1e494a271cb018dcd361bd2c917ba7004c3e49d5daf795a2/py_cpuinfo-9.0.0-py3-none-any.whl", hash = "sha256:859625bc251f64e21f077d099d4162689c762b5d6a4c3c97553d56241c9674d5", size = 22335, upload-time = "2022-10-25T20:38:27.636Z" },
{ url = "https://files.pythonhosted.org/packages/84/03/0d3ce49e2505ae70cf43bc5bb3033955d2fc9f932163e84dc0779cc47f48/prompt_toolkit-3.0.52-py3-none-any.whl", hash = "sha256:9aac639a3bbd33284347de5ad8d68ecc044b91a762dc39b7c21095fcd6a19955", size = 391431, upload-time = "2025-08-27T15:23:59.498Z" },
]
[[package]]
@@ -696,15 +663,6 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/e6/3d/8888e7ca0c6b093b52aa5c6693b0022e66d5958adcc685ed7a6a8ae615e8/pygments_styles-0.2.0-py3-none-any.whl", hash = "sha256:40fb7f1d34ce2b2792aecabc8d3877ca364eb04bb3b7f7747cfc9a7f0569bae9", size = 34200, upload-time = "2025-09-26T08:39:02.262Z" },
]
[[package]]
name = "pyreadline3"
version = "3.5.4"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/0f/49/4cea918a08f02817aabae639e3d0ac046fef9f9180518a3ad394e22da148/pyreadline3-3.5.4.tar.gz", hash = "sha256:8d57d53039a1c75adba8e50dd3d992b28143480816187ea5efbd5c78e6c885b7", size = 99839, upload-time = "2024-09-19T02:40:10.062Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/5a/dc/491b7661614ab97483abf2056be1deee4dc2490ecbf7bff9ab5cdbac86e1/pyreadline3-3.5.4-py3-none-any.whl", hash = "sha256:eaf8e6cc3c49bcccf145fc6067ba8643d1df34d604a1ec0eccbf7a18e6d3fae6", size = 83178, upload-time = "2024-09-19T02:40:08.598Z" },
]
[[package]]
name = "pytest"
version = "8.4.2"
@@ -1068,6 +1026,15 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/e3/bd/fa9bb053192491b3867ba07d2343d9f2252e00811567d30ae8d0f78136fe/watchfiles-1.1.1-cp314-cp314t-musllinux_1_1_x86_64.whl", hash = "sha256:a916a2932da8f8ab582f242c065f5c81bed3462849ca79ee357dd9551b0e9b01", size = 622112, upload-time = "2025-10-14T15:05:50.941Z" },
]
[[package]]
name = "wcwidth"
version = "0.3.0"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/38/75/2144b65e4fba12a2d9868e9a3f99db7fa0760670d064603634bef9ff1709/wcwidth-0.3.0.tar.gz", hash = "sha256:af1a2fb0b83ef4a7fc0682a4c95ca2576e14d0280bca2a9e67b7dc9f2733e123", size = 172238, upload-time = "2026-01-21T17:44:09.508Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/18/0e/a5f0257ab47492b7afb5fb60347d14ba19445e2773fc8352d4be6bd2f6f8/wcwidth-0.3.0-py3-none-any.whl", hash = "sha256:073a1acb250e4add96cfd5ef84e0036605cd6e0d0782c8c15c80e42202348458", size = 85520, upload-time = "2026-01-21T17:44:08.002Z" },
]
[[package]]
name = "websockets"
version = "15.0.1"