added feature for editing code before running

This commit is contained in:
test 2024-01-20 20:16:04 -05:00
parent ed1d399b85
commit a17bb46153

23
llm_cmd
View File

@ -7,6 +7,8 @@ from bs4 import BeautifulSoup
from rich.console import Console
from rich.markdown import Markdown
import sys, os
import tempfile
try:
if len(sys.argv[1]) > 1:
@ -96,6 +98,7 @@ for code in soup.findAll('code'):
code = code.replace("<code>bash\n","")
code = code.replace("<code>","")
code = code.replace("</code>","")
code = code.replace("&amp;", "&")
if len(code) < 4:
continue
@ -103,8 +106,24 @@ for code in soup.findAll('code'):
console.print(Markdown(f"```bash\n{code}\n```"))
print("--------------------------------------------------------")
answer = input("Would you like to run this code [N/y]:").lower()
answer = input("Would you like to run this code [N/y/e/d]:").lower()
if answer == "y" or answer == "yes":
print("\n")
os.system(code)
print("")
print("")
elif answer == "e" or answer == "edit":
file_content = code
t = tempfile.NamedTemporaryFile(delete=False)
t.write(file_content.encode())
t.close()
try:
editor = os.environ['EDITOR']
except KeyError:
editor = 'nano'
os.system(f"{editor} {t.name}")
os.system(f"bash {t.name}")
os.system(f"rm -rf {t.name}")
elif answer == "d" or answer == "done":
exit()