Skip to content

Commit

Permalink
fix: improved input prompt for ask_execute
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Oct 27, 2023
1 parent 4632050 commit b68d7d4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion gptme/tools/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def execute_patch(codeblock: str, fn: str, ask: bool) -> Generator[Message, None
Executes the patch.
"""
if ask:
confirm = ask_execute()
confirm = ask_execute("Apply patch?")
if not confirm:
print("Patch not applied")
return
Expand Down
10 changes: 4 additions & 6 deletions gptme/tools/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ def execute_save(fn: str, code: str, ask: bool) -> Generator[Message, None, None
code = code.lstrip("\n")

if ask:
print(f"Save to {fn}?")
confirm = ask_execute(fn)
confirm = ask_execute(f"Save to {fn}?")
print()
else:
confirm = True
print("Skipping save confirmation.")

if ask and not confirm:
Expand All @@ -26,8 +26,7 @@ def execute_save(fn: str, code: str, ask: bool) -> Generator[Message, None, None

# if the file exists, ask to overwrite
if path.exists():
print("File already exists.")
overwrite = ask_execute("overwrite")
overwrite = ask_execute("File exists, overwrite?")
print()
if not overwrite:
# early return
Expand All @@ -36,8 +35,7 @@ def execute_save(fn: str, code: str, ask: bool) -> Generator[Message, None, None

# if the folder doesn't exist, ask to create it
if not path.parent.exists():
print("Folder does not exist.")
create = ask_execute("create")
create = ask_execute("Folder doesn't exist, create it?")
print()
if create:
path.parent.mkdir(parents=True)
Expand Down
12 changes: 5 additions & 7 deletions gptme/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,20 @@ def epoch_to_age(epoch):
return f"{age.days} days ago ({datetime.fromtimestamp(epoch).strftime('%Y-%m-%d')})"


def print_preview(code=None, lang=None):
# print a preview section header
def print_preview(code: str, lang: str):
print()
print("[bold white]Preview[/bold white]")
if code:
print(Syntax(code.strip(), lang))
print()
print(Syntax(code.strip(), lang))
print()


def ask_execute(default=True) -> bool:
def ask_execute(question="Execute code?", default=True) -> bool:
# TODO: add a way to outsource ask_execute decision to another agent/LLM
console = Console()
choicestr = f"({'Y' if default else 'y'}/{'n' if default else 'N'})"
# answer = None
# while not answer or answer.lower() not in ["y", "yes", "n", "no", ""]:
answer = console.input(
f"[bold yellow on red] {EMOJI_WARN} Execute code? {choicestr} [/] ",
f"[bold yellow on red] {EMOJI_WARN} {question} {choicestr} [/] ",
)
return answer.lower() in (["y", "yes"] + [""] if default else [])

0 comments on commit b68d7d4

Please sign in to comment.