Fixed double-text bug.

This commit is contained in:
justin-slothware committed 2025-07-07 14:25:02 -07:00
1 parent 61412d928e
commit 7c3daa1c92
1 file changed
+26 -12
+26 -12
View File
@@ -11,12 +11,12 @@ import shutil
import textwrap import textwrap
from pathlib import Path from pathlib import Path
import colorama import colorama
import readline
colorama.init() colorama.init()
def get_api_key(): def get_api_key():
api_key = os.getenv("OPENAI_API_KEY") api_key = os.getenv("OPENAI_API_KEY")
if api_key: if api_key:
return api_key return api_key
@@ -38,8 +38,13 @@ def get_api_key():
return api_key return api_key
export_line = f'\nexport OPENAI_API_KEY="{api_key}"\n' export_line = f'\nexport OPENAI_API_KEY="{api_key}"\n'
try: try:
# Check if the key already exists in shell config
if shell_config.exists():
content = shell_config.read_text()
if api_key in content:
print(f"API key already present in {shell_config}.")
return api_key
with open(shell_config, "a") as f: with open(shell_config, "a") as f:
f.write(export_line) f.write(export_line)
print(f"✅ API key saved to {shell_config}. It will be available next time you launch the terminal.") print(f"✅ API key saved to {shell_config}. It will be available next time you launch the terminal.")
@@ -71,23 +76,27 @@ def print_imessage(sender, text, is_user=False):
wrapper = textwrap.TextWrapper(width=max_bubble_width) wrapper = textwrap.TextWrapper(width=max_bubble_width)
lines = wrapper.wrap(text.strip()) lines = wrapper.wrap(text.strip())
# Bubble color color = "\033[44m" if is_user else "\033[100m" # Blue for user, Gray for ChatGPT
color = "\033[44m" if is_user else "\033[100m" # Blue or Gray
reset = "\033[0m" reset = "\033[0m"
# Top and bottom border
top = f"{color}╭{'─' * (max_bubble_width + 2)}╮{reset}" top = f"{color}╭{'─' * (max_bubble_width + 2)}╮{reset}"
bottom = f"{color}╰{'─' * (max_bubble_width + 2)}╯{reset}" bottom = f"{color}╰{'─' * (max_bubble_width + 2)}╯{reset}"
print(" " * indent + f"{sender}:") print(" " * indent + f"{sender}:")
print(" " * indent + top) print(" " * indent + top)
for line in lines: for line in lines:
pad = " " * (max_bubble_width - len(line)) pad = " " * (max_bubble_width - len(line))
print(" " * indent + f"{color}│ {line}{pad} │{reset}") print(" " * indent + f"{color}│ {line}{pad} │{reset}")
print(" " * indent + bottom + "\n") print(" " * indent + bottom + "\n")
def read_input_silently(prompt="You: "):
try:
print(prompt, end="", flush=True)
user_input = input()
sys.stdout.write("\033[F\033[K") # Move cursor up and clear line
return user_input.strip()
except EOFError:
return ""
def chat_with_gpt(): def chat_with_gpt():
current_model = "gpt-3.5-turbo" current_model = "gpt-3.5-turbo"
available_models = ["gpt-3.5-turbo", "gpt-4"] available_models = ["gpt-3.5-turbo", "gpt-4"]
@@ -98,7 +107,10 @@ def chat_with_gpt():
conversation_history = [] conversation_history = []
while True: while True:
user_input = input("You: ").strip() user_input = read_input_silently()
if not user_input:
continue
print_imessage("You", user_input, is_user=True) print_imessage("You", user_input, is_user=True)
if user_input.lower() == "help": if user_input.lower() == "help":
@@ -157,9 +169,11 @@ def chat_with_gpt():
try: try:
content = last_response content = last_response
if "```" in content: if "```" in content:
content = content.split("```")[1] # Extract first code block content if present
if "\n" in content: parts = content.split("```")
content = "\n".join(content.split("\n")[1:]) if len(parts) >= 3:
content = parts[2].strip()
else:
content = content.strip("`").strip() content = content.strip("`").strip()
with open(filename, "w") as f: with open(filename, "w") as f: