diff --git a/coder.py b/coder.py index 2f1b5ea4e..86db0b542 100755 --- a/coder.py +++ b/coder.py @@ -4,10 +4,6 @@ import sys import re import traceback -from prompt_toolkit import prompt -from prompt_toolkit.completion import Completer, Completion -from prompt_toolkit.history import FileHistory -from prompt_toolkit.styles import Style from rich.console import Console from rich.prompt import Confirm, Prompt @@ -17,6 +13,8 @@ from rich.text import Text from rich.markdown import Markdown from pathlib import Path + +from getinput import get_input import utils import os @@ -30,25 +28,6 @@ import prompts openai.api_key = os.getenv("OPENAI_API_KEY") -class FileContentCompleter(Completer): - def __init__(self, fnames): - self.fnames = fnames - - def get_completions(self, document, complete_event): - text = document.text_before_cursor - words = text.split() - if not words: - return - - last_word = words[-1] - for fname in self.fnames: - with open(fname, "r") as f: - content = f.read() - for word in content.split(): - if word.startswith(last_word): - yield Completion(word, start_position=-len(last_word)) - - class Coder: fnames = dict() last_modified = 0 @@ -180,7 +159,7 @@ class Coder: else: print() - inp = utils.get_input(self.history_file, self.fnames) + inp = get_input(self.history_file, self.fnames) if inp is None: return diff --git a/getinput.py b/getinput.py new file mode 100644 index 000000000..3edb2b2a6 --- /dev/null +++ b/getinput.py @@ -0,0 +1,61 @@ +from prompt_toolkit.styles import Style + +from prompt_toolkit import prompt +from prompt_toolkit.completion import Completer, Completion +from prompt_toolkit.history import FileHistory + + +class FileContentCompleter(Completer): + def __init__(self, fnames): + self.fnames = fnames + + def get_completions(self, document, complete_event): + text = document.text_before_cursor + words = text.split() + if not words: + return + + last_word = words[-1] + for fname in self.fnames: + with open(fname, "r") as f: + content = f.read() + for word in content.split(): + if word.startswith(last_word): + yield Completion(word, start_position=-len(last_word)) + + +def get_input(history_file, fnames): + inp = "" + multiline_input = False + + style = Style.from_dict({"": "green"}) + + while True: + completer_instance = FileContentCompleter(fnames) + if multiline_input: + show = ". " + else: + show = "> " + + try: + line = prompt( + show, + completer=completer_instance, + history=FileHistory(history_file), + style=style, + ) + except EOFError: + return + if line.strip() == "{" and not multiline_input: + multiline_input = True + continue + elif line.strip() == "}" and multiline_input: + break + elif multiline_input: + inp += line + "\n" + else: + inp = line + break + + print() + return inp diff --git a/utils.py b/utils.py index 46ba1f651..00529393a 100644 --- a/utils.py +++ b/utils.py @@ -107,38 +107,3 @@ def do_replace(fname, before_text, after_text): fname.write_text(new_content) return True -def get_input(history_file, fnames): - inp = "" - multiline_input = False - - style = Style.from_dict({'': 'green'}) - - while True: - completer_instance = FileContentCompleter(fnames) - if multiline_input: - show = ". " - else: - show = "> " - - try: - line = prompt( - show, - completer=completer_instance, - history=FileHistory(history_file), - style=style, - ) - except EOFError: - return - if line.strip() == "{" and not multiline_input: - multiline_input = True - continue - elif line.strip() == "}" and multiline_input: - break - elif multiline_input: - inp += line + "\n" - else: - inp = line - break - - print() - return inp