Skip to content

Commit

Permalink
Edit the command with prompt_toolkit instead of readline (#4)
Browse files Browse the repository at this point in the history
Thanks, @jkseppan
  • Loading branch information
jkseppan authored Mar 26, 2024
1 parent 8e4156b commit e471b28
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
19 changes: 11 additions & 8 deletions llm_cmd.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import click
import llm
import readline
import subprocess

from prompt_toolkit.shortcuts import prompt
from prompt_toolkit.lexers import PygmentsLexer
from pygments.lexers.shell import BashLexer


SYSTEM_PROMPT = """
Return only the command to be executed as a raw string, no string delimiters
wrapping it, no yapping, no markdown, no fenced code blocks, what you return
Expand Down Expand Up @@ -39,16 +43,15 @@ def cmd(args, model, system, key):


def interactive_exec(command):
# Set the initial text for the input
readline.set_startup_hook(lambda: readline.insert_text(command))
if '\n' in command:
print("Multiline command - Meta-Enter or Esc Enter to execute")
edited_command = prompt("> ", default=command, lexer=PygmentsLexer(BashLexer), multiline=True)
else:
edited_command = prompt("> ", default=command, lexer=PygmentsLexer(BashLexer))
try:
edited_command = input()
output = subprocess.check_output(
edited_command, shell=True, stderr=subprocess.STDOUT
)
print(output.decode())
except subprocess.CalledProcessError as e:
print(f"Command failed with error: {e.output.decode()}")
finally:
# Remove the startup hook to avoid affecting future inputs
readline.set_startup_hook(None)
print(f"Command failed with error (exit status {e.returncode}): {e.output.decode()}")
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ classifiers = [
"License :: OSI Approved :: Apache Software License"
]
dependencies = [
"llm"
"llm",
"prompt_toolkit>=3.0.43",
"pygments>=2.17.2",
]

[project.urls]
Expand Down

0 comments on commit e471b28

Please sign in to comment.