roughed in switch to prompt toolkit for _ask funcs

This commit is contained in:
Paul Gauthier 2023-05-12 08:15:32 -07:00
parent 9a4dfae07a
commit 0325fc40ac
3 changed files with 40 additions and 41 deletions

View file

@ -16,7 +16,7 @@ import git
import openai import openai
# from aider.dump import dump # from aider.dump import dump
from aider.getinput import get_input from aider import getinput
from aider import utils from aider import utils
from aider import prompts from aider import prompts
from aider.commands import Commands from aider.commands import Commands
@ -65,7 +65,7 @@ class Coder:
else: else:
self.root = os.getcwd() self.root = os.getcwd()
self.console.print(f"[bright_black]Common root directory: {self.root}") self.console.print(f"Common root directory: {self.root}")
def set_repo(self, cmd_line_fnames): def set_repo(self, cmd_line_fnames):
if not cmd_line_fnames: if not cmd_line_fnames:
@ -76,7 +76,7 @@ class Coder:
repo_paths = [] repo_paths = []
for fname in abs_fnames: for fname in abs_fnames:
if not fname.exists(): if not fname.exists():
self.console.print(f"[bright_black]Creating {fname}") self.console.print(f"Creating {fname}")
fname.parent.mkdir(parents=True, exist_ok=True) fname.parent.mkdir(parents=True, exist_ok=True)
fname.touch() fname.touch()
try: try:
@ -87,7 +87,7 @@ class Coder:
if fname.is_dir(): if fname.is_dir():
continue continue
self.console.print(f"[bright_black]Loading {fname}") self.console.print(f"Loading {fname}")
fname = fname.resolve() fname = fname.resolve()
self.abs_fnames.add(str(fname)) self.abs_fnames.add(str(fname))
@ -114,19 +114,17 @@ class Coder:
new_files.append(relative_fname) new_files.append(relative_fname)
if new_files: if new_files:
self.console.print(f"[bright_black]Files not tracked in {repo.git_dir}:") self.console.print(f"Files not tracked in {repo.git_dir}:")
for fn in new_files: for fn in new_files:
self.console.print(f"[bright_black] {fn}") self.console.print(f" {fn}")
if self.confirm_ask("[bright_black]Add them?", default="y"): if getinput.confirm_ask("Add them?", default="y"):
for relative_fname in new_files: for relative_fname in new_files:
repo.git.add(relative_fname) repo.git.add(relative_fname)
self.console.print(f"[bright_black]Added {relative_fname} to the git repo") self.console.print(f"Added {relative_fname} to the git repo")
show_files = ", ".join(new_files) show_files = ", ".join(new_files)
commit_message = f"Initial commit: Added new files to the git repo: {show_files}" commit_message = f"Initial commit: Added new files to the git repo: {show_files}"
repo.git.commit("-m", commit_message, "--no-verify") repo.git.commit("-m", commit_message, "--no-verify")
self.console.print( self.console.print(f"Committed new files with message: {commit_message}")
f"[bright_black]Committed new files with message: {commit_message}"
)
else: else:
self.console.print("[red]Skipped adding new files to the git repo.") self.console.print("[red]Skipped adding new files to the git repo.")
return return
@ -205,7 +203,7 @@ class Coder:
else: else:
print() print()
inp = get_input(self.history_file, self.abs_fnames, self.commands) inp = getinput.get_input(self.history_file, self.abs_fnames, self.commands)
self.num_control_c = 0 self.num_control_c = 0
@ -321,9 +319,9 @@ class Coder:
return return
for rel_fname in mentioned_rel_fnames: for rel_fname in mentioned_rel_fnames:
self.console.print(f"[bright_black]{rel_fname}") self.console.print(f"{rel_fname}")
if not self.confirm_ask("[bright_black]Add {path} to git?", default="y"): if not getinput.confirm_ask("Add {path} to git?", default="y"):
return return
for rel_fname in mentioned_rel_fnames: for rel_fname in mentioned_rel_fnames:
@ -400,10 +398,12 @@ class Coder:
if full_path not in self.abs_fnames: if full_path not in self.abs_fnames:
if not Path(full_path).exists(): if not Path(full_path).exists():
question = f"[bright_black]Allow creation of new file {path}?" # noqa: E501 question = f"Allow creation of new file {path}?" # noqa: E501
else: else:
question = f"[bright_black]Allow edits to {path} which was not previously provided?" # noqa: E501 question = (
if not self.confirm_ask(question, default="y"): f"Allow edits to {path} which was not previously provided?" # noqa: E501
)
if not getinput.confirm_ask(question, default="y"):
self.console.print(f"[red]Skipping edit to {path}") self.console.print(f"[red]Skipping edit to {path}")
continue continue
@ -411,15 +411,15 @@ class Coder:
Path(full_path).touch() Path(full_path).touch()
self.abs_fnames.add(full_path) self.abs_fnames.add(full_path)
if self.repo and self.confirm_ask( if self.repo and getinput.confirm_ask(
f"[bright_black]Add {path} to git?", f"Add {path} to git?",
default="y", default="y",
): ):
self.repo.git.add(full_path) self.repo.git.add(full_path)
edited.add(path) edited.add(path)
if utils.do_replace(full_path, original, updated): if utils.do_replace(full_path, original, updated):
self.console.print(f"[bright_black]Applied edit to {path}") self.console.print(f"Applied edit to {path}")
else: else:
self.console.print(f"[red]Failed to apply edit to {path}") self.console.print(f"[red]Failed to apply edit to {path}")
@ -512,14 +512,14 @@ class Coder:
if ask: if ask:
if which == "repo_files": if which == "repo_files":
self.console.print("[bright_black]Git repo has uncommitted changes.\n") self.console.print("Git repo has uncommitted changes.\n")
else: else:
self.console.print("[bright_black]Files have uncommitted changes.\n") self.console.print("Files have uncommitted changes.\n")
self.console.print(f"[bright_black]Suggested commit message:\n{commit_message}\n") self.console.print(f"Suggested commit message:\n{commit_message}\n")
res = self.prompt_ask( res = getinput.prompt_ask(
Text("Commit before the chat proceeds? [y/n/commit message]", style="bright_black"), "Commit before the chat proceeds [y/n/commit message]?",
default="y", default=commit_message,
).strip() ).strip()
self.last_asked_for_commit_time = self.get_last_modified() self.last_asked_for_commit_time = self.get_last_modified()
@ -536,7 +536,7 @@ class Coder:
full_commit_message = commit_message + "\n\n" + context full_commit_message = commit_message + "\n\n" + context
repo.git.commit("-m", full_commit_message, "--no-verify") repo.git.commit("-m", full_commit_message, "--no-verify")
commit_hash = repo.head.commit.hexsha[:7] commit_hash = repo.head.commit.hexsha[:7]
self.console.print(f"[bright_black]{commit_hash} {commit_message}") self.console.print(f"{commit_hash} {commit_message}")
return commit_hash, commit_message return commit_hash, commit_message

View file

@ -77,7 +77,7 @@ class Commands:
return return
commit_message = args.strip() commit_message = args.strip()
self.coder.commit(message = commit_message, which="repo_files") self.coder.commit(message=commit_message, which="repo_files")
def cmd_undo(self, args): def cmd_undo(self, args):
"Undo the last git commit if it was done by aider" "Undo the last git commit if it was done by aider"
@ -94,7 +94,7 @@ class Commands:
return return
self.coder.repo.git.reset("--hard", "HEAD~1") self.coder.repo.git.reset("--hard", "HEAD~1")
self.console.print( self.console.print(
f"[bright_black]{last_commit.message.strip()}\n" f"{last_commit.message.strip()}\n"
f"The above commit {self.coder.last_aider_commit_hash} " f"The above commit {self.coder.last_aider_commit_hash} "
"was reset and removed from git.\n" "was reset and removed from git.\n"
) )
@ -137,12 +137,11 @@ class Commands:
if not matched_files: if not matched_files:
if self.coder.repo is not None: if self.coder.repo is not None:
create_file = Confirm.ask( create_file = Confirm.ask(
f"[bright_black]No files matched '{word}'. " f"No files matched '{word}'. Do you want to create the file and add it to git?",
"Do you want to create the file and add it to git?",
) )
else: else:
create_file = Confirm.ask( create_file = Confirm.ask(
f"[bright_black]No files matched '{word}'. Do you want to create the file?" f"No files matched '{word}'. Do you want to create the file?"
) )
if create_file: if create_file:
@ -160,7 +159,7 @@ class Commands:
abs_file_path = os.path.abspath(os.path.join(self.coder.root, matched_file)) abs_file_path = os.path.abspath(os.path.join(self.coder.root, matched_file))
if abs_file_path not in self.coder.abs_fnames: if abs_file_path not in self.coder.abs_fnames:
self.coder.abs_fnames.add(abs_file_path) self.coder.abs_fnames.add(abs_file_path)
self.console.print(f"[bright_black]Added {matched_file} to the chat") self.console.print(f"Added {matched_file} to the chat")
added_fnames.append(matched_file) added_fnames.append(matched_file)
else: else:
self.console.print(f"[red]{matched_file} is already in the chat") self.console.print(f"[red]{matched_file} is already in the chat")
@ -197,7 +196,7 @@ class Commands:
for matched_file in matched_files: for matched_file in matched_files:
relative_fname = os.path.relpath(matched_file, self.coder.root) relative_fname = os.path.relpath(matched_file, self.coder.root)
self.coder.abs_fnames.remove(matched_file) self.coder.abs_fnames.remove(matched_file)
self.console.print(f"[bright_black]Removed {relative_fname} from the chat") self.console.print(f"Removed {relative_fname} from the chat")
def cmd_ls(self, args): def cmd_ls(self, args):
"List all known files and those included in the chat session" "List all known files and those included in the chat session"
@ -214,11 +213,11 @@ class Commands:
other_files.append(file) other_files.append(file)
if chat_files: if chat_files:
self.console.print("[bright_black]Files in chat:\n") self.console.print("Files in chat:\n")
for file in chat_files: for file in chat_files:
self.console.print(f"[bright_black] {file}") self.console.print(f" {file}")
if other_files: if other_files:
self.console.print("\n[bright_black]Repo files not in the chat:\n") self.console.print("\nRepo files not in the chat:\n")
for file in other_files: for file in other_files:
self.console.print(f"[bright_black] {file}") self.console.print(f" {file}")

View file

@ -6,7 +6,7 @@ from pygments.util import ClassNotFound
from prompt_toolkit import prompt from prompt_toolkit import prompt
from prompt_toolkit.completion import Completer, Completion from prompt_toolkit.completion import Completer, Completion
from prompt_toolkit.history import FileHistory from prompt_toolkit.history import FileHistory
from prompt_toolkit.shortcuts import CompleteStyle, confirm from prompt_toolkit.shortcuts import CompleteStyle
from rich.console import Console from rich.console import Console
import sys import sys
import time import time
@ -120,10 +120,10 @@ def get_input(history_file, fnames, commands):
def confirm_ask(question, default=None, yes=False): def confirm_ask(question, default=None, yes=False):
if yes: if yes:
return True return True
return confirm(question, default=default) return prompt(question + " ", default=default)
def prompt_ask(question, default=None, yes=False): def prompt_ask(question, default=None, yes=False):
if yes: if yes:
return True return True
return prompt(question, default=default) return prompt(question + " ", default=default)