Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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