final work on autocomplete

This commit is contained in:
2025-04-09 23:32:21 +03:00
parent 592d128ef6
commit 8b496aa782
10 changed files with 100 additions and 124 deletions
+2 -2
View File
@@ -1,4 +1,4 @@
__all__ = ["Autocompleter"]
__all__ = ["AutoCompleter"]
from argenta.app.autocompleter.models import Autocompleter
from argenta.app.autocompleter.entity import AutoCompleter
+45
View File
@@ -0,0 +1,45 @@
import readline
import os
class AutoCompleter:
def __init__(self, history_filename: str = False, autocomplete_button: str = 'tab'):
self.history_filename = history_filename
self.autocomplete_button = autocomplete_button
self.matches = []
def complete(self, text, state):
matches = sorted(cmd for cmd in self.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
def initial_setup(self):
if self.history_filename:
if os.path.exists(self.history_filename):
readline.read_history_file(self.history_filename)
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):
if self.history_filename:
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)]
-58
View File
@@ -1,58 +0,0 @@
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()