mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 18:15:28 +03:00
work on autocomplete
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
__all__ = ["Autocompleter"]
|
||||||
|
|
||||||
|
|
||||||
|
from argenta.app.autocompleter.models import Autocompleter
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
import readline
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Autocompleter:
|
||||||
|
def __init__(self, history_filename: str = './completer.hist', autocomplete_button: str = 'tab'):
|
||||||
|
self.history_filename = history_filename
|
||||||
|
self.autocomplete_button = autocomplete_button
|
||||||
|
self.matches = []
|
||||||
|
|
||||||
|
def complete(self, text, state):
|
||||||
|
if state == 0:
|
||||||
|
history_values = self.get_history_items()
|
||||||
|
if text:
|
||||||
|
self.matches = sorted(h for h in history_values if h and h.startswith(text))
|
||||||
|
else:
|
||||||
|
self.matches = []
|
||||||
|
try:
|
||||||
|
response = self.matches[state]
|
||||||
|
except IndexError:
|
||||||
|
response = None
|
||||||
|
return response
|
||||||
|
|
||||||
|
def initial_setup(self):
|
||||||
|
if os.path.exists(self.history_filename):
|
||||||
|
readline.read_history_file(self.history_filename)
|
||||||
|
readline.set_completer(self.complete)
|
||||||
|
readline.parse_and_bind(f'{self.autocomplete_button}: complete')
|
||||||
|
|
||||||
|
def write_command_to_history(self):
|
||||||
|
readline.write_history_file(self.history_filename)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_history_items():
|
||||||
|
return [readline.get_history_item(i) for i in range(1, readline.get_current_history_length() + 1)]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def inputting():
|
||||||
|
autocompleter = Autocompleter()
|
||||||
|
autocompleter.initial_setup()
|
||||||
|
print(f'Максимальная длина файла истории: {readline.get_history_length()}')
|
||||||
|
print(f'История запуска:{autocompleter.get_history_items()}')
|
||||||
|
while True:
|
||||||
|
line = input('\n!("stop" to quit) Ввод текста: => ')
|
||||||
|
if line == 'stop':
|
||||||
|
print(f'Конец записи истории: {autocompleter.get_history_items()}')
|
||||||
|
autocompleter.write_command_to_history()
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
inputting()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -8,6 +8,7 @@ import re
|
|||||||
from argenta.command.models import Command, InputCommand
|
from argenta.command.models import Command, InputCommand
|
||||||
from argenta.router import Router
|
from argenta.router import Router
|
||||||
from argenta.router.defaults import system_router
|
from argenta.router.defaults import system_router
|
||||||
|
from argenta.app.autocompleter import Autocompleter
|
||||||
from argenta.app.dividing_line.models import StaticDividingLine, DynamicDividingLine
|
from argenta.app.dividing_line.models import StaticDividingLine, DynamicDividingLine
|
||||||
from argenta.command.exceptions import (UnprocessedInputFlagException,
|
from argenta.command.exceptions import (UnprocessedInputFlagException,
|
||||||
RepeatedInputFlagsException,
|
RepeatedInputFlagsException,
|
||||||
@@ -33,6 +34,7 @@ class AppInit:
|
|||||||
dividing_line: StaticDividingLine | DynamicDividingLine = StaticDividingLine(),
|
dividing_line: StaticDividingLine | DynamicDividingLine = StaticDividingLine(),
|
||||||
repeat_command_groups: bool = True,
|
repeat_command_groups: bool = True,
|
||||||
full_override_system_messages: bool = False,
|
full_override_system_messages: bool = False,
|
||||||
|
autocompleter: Autocompleter = Autocompleter(),
|
||||||
print_func: Callable[[str], None] = Console().print) -> None:
|
print_func: Callable[[str], None] = Console().print) -> None:
|
||||||
self._prompt = prompt
|
self._prompt = prompt
|
||||||
self._print_func = print_func
|
self._print_func = print_func
|
||||||
|
|||||||
+55
-11
@@ -1,14 +1,58 @@
|
|||||||
from contextlib import redirect_stdout
|
import readline
|
||||||
import io
|
import os
|
||||||
import string
|
|
||||||
|
HISTORY_FILENAME = 'completer.hist'
|
||||||
|
|
||||||
|
|
||||||
|
def get_history_items():
|
||||||
|
return [readline.get_history_item(i) for i in range(1, readline.get_current_history_length() + 1)]
|
||||||
|
|
||||||
|
|
||||||
|
class HistoryCompleter:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.matches = []
|
||||||
|
return
|
||||||
|
|
||||||
|
def complete(self, text, state):
|
||||||
|
response = None
|
||||||
|
if state == 0:
|
||||||
|
history_values = get_history_items()
|
||||||
|
if text:
|
||||||
|
self.matches = sorted(h
|
||||||
|
for h in history_values
|
||||||
|
if h and h.startswith(text))
|
||||||
|
else:
|
||||||
|
self.matches = []
|
||||||
|
try:
|
||||||
|
response = self.matches[state]
|
||||||
|
except IndexError:
|
||||||
|
response = None
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
def inputing():
|
||||||
|
if os.path.exists(HISTORY_FILENAME):
|
||||||
|
readline.read_history_file(HISTORY_FILENAME)
|
||||||
|
print(f'Максимальная длина файла истории: {readline.get_history_length()}')
|
||||||
|
print(f'История запуска:{get_history_items()}')
|
||||||
|
try:
|
||||||
while True:
|
while True:
|
||||||
with redirect_stdout(io.StringIO()) as f:
|
line = input('!("stop" to quit) Ввод текста: => ')
|
||||||
a = input()
|
if line == 'stop':
|
||||||
print(a)
|
break
|
||||||
res = f.getvalue()
|
if line:
|
||||||
res = ''.join([x for x in res if x in string.printable])
|
print(f'Добавление "{line}" в файл истории.')
|
||||||
print('-'*len(res))
|
finally:
|
||||||
print(res.strip('\n'))
|
print(f'Конец записи истории: {get_history_items()}')
|
||||||
print('-'*len(res))
|
readline.write_history_file(HISTORY_FILENAME)
|
||||||
|
|
||||||
|
|
||||||
|
# Регистрация класса 'HistoryCompleter'
|
||||||
|
readline.set_completer(HistoryCompleter().complete)
|
||||||
|
|
||||||
|
# Регистрация клавиши `tab` для автодополнения
|
||||||
|
readline.parse_and_bind('tab: complete')
|
||||||
|
|
||||||
|
# Запрос текста
|
||||||
|
inputing()
|
||||||
Reference in New Issue
Block a user