Spaces:
Sleeping
Sleeping
File size: 7,239 Bytes
8d9eecc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
import os
import getpass
import socket
from datetime import datetime
import curses
import re
from colors import *
from display import clear_and_setup, show_ascii_art
from gemini_client import ask_gemini, ask_gemini_with_file_generation
def my_ls():
try:
files = os.listdir('.')
print(f"{CYAN}π Files and Directories:{RESET}")
for item in sorted(files):
if os.path.isdir(item):
print(f" {BLUE}{BOLD}π {item}/{RESET}")
else:
print(f" {GREEN}π {item}{RESET}")
except PermissionError:
print(f"{RED}β Permission denied{RESET}")
def my_pwd():
print(f"{YELLOW}π Current directory: {BOLD}{os.getcwd()}{RESET}")
def my_date():
now = datetime.now()
print(f"{MAGENTA}π {now.strftime('%A, %B %d, %Y - %I:%M:%S %p')}{RESET}")
def my_whoami():
username = getpass.getuser()
hostname = socket.gethostname()
print(f"{CYAN}π€ You are: {BOLD}{username}{RESET} on Acidop shell")
def my_help():
print(f"{BOLD}{CYAN}π οΈ codic Built-in Commands:{RESET}")
print(f"{GREEN} ls {RESET}- List files and directories (custom)")
print(f"{GREEN} pwd {RESET}- Show current directory (custom)")
print(f"{GREEN} cd <dir> {RESET}- Change directory")
print(f"{GREEN} date {RESET}- Show current date and time (custom)")
print(f"{GREEN} whoami {RESET}- Show current user info (custom)")
print(f"{GREEN} clear {RESET}- Clear the screen")
print(f"{GREEN} help {RESET}- Show this help message")
print(f"{GREEN} ascii {RESET}- Show cool ASCII art")
print(f"{GREEN} exit {RESET}- Exit the shell")
print(f"{GREEN} ai {RESET}- Enter AI mode with Gemini 2.5 Flash")
print(f"{GREEN} ai gen {RESET}- Enter AI mode with automatic file generation")
print(f"{YELLOW} <command> {RESET}- Run any system command")
def handle_builtin_commands(command_parts):
cmd = command_parts[0].lower()
if cmd == "ls":
my_ls()
return True
elif cmd == "pwd":
my_pwd()
return True
elif cmd == "cd":
try:
if len(command_parts) == 1:
os.chdir(os.path.expanduser("~"))
else:
os.chdir(command_parts[1])
print(f"{GREEN}β
Changed to: {os.getcwd()}{RESET}")
except FileNotFoundError:
print(f"{RED}β Directory not found: {command_parts[1]}{RESET}")
except PermissionError:
print(f"{RED}β Permission denied{RESET}")
return True
elif cmd == "date":
my_date()
return True
elif cmd == "whoami":
my_whoami()
return True
elif cmd == "clear":
clear_and_setup()
return True
elif cmd == "help":
my_help()
return True
elif cmd == "ascii":
show_ascii_art()
return True
elif cmd == "ai":
if len(command_parts) > 1 and command_parts[1].lower() == "gen":
print(f"{YELLOW}π€ Entering AI Gen Mode (with file generation).{RESET}")
print(f"{CYAN}π‘ Tip: Ask me to 'create a Flask app' or 'build a React component'{RESET}")
print(f"{GREEN}π Current directory: {os.getcwd()}{RESET}")
print(f"{YELLOW}Type 'exit' to leave.{RESET}\n")
while True:
query = input(f"{MAGENTA}ai-gen> {RESET}").strip()
if query.lower() in ["exit", "quit"]:
print(f"{YELLOW}π Leaving AI Gen mode.{RESET}")
break
if not query:
continue
print(f"{YELLOW}π€ Thinking and generating...{RESET}\n")
print(f"{CYAN}π Where should I create the files?{RESET}")
print(f"{YELLOW} Examples:{RESET}")
print(f"{YELLOW} - Press Enter (current directory){RESET}")
print(f"{YELLOW} - my_project (creates folder in current dir){RESET}")
print(f"{YELLOW} - ~/Desktop/my_app (absolute path){RESET}")
print(f"{YELLOW} - ../parent_folder/project (relative path){RESET}")
project_dir = input(f"{CYAN} Path: {RESET}").strip()
if not project_dir:
project_dir = "."
print(f"{GREEN} β Using current directory: {os.getcwd()}{RESET}")
else:
project_dir = os.path.expanduser(project_dir)
if not os.path.isabs(project_dir):
abs_path = os.path.abspath(project_dir)
print(f"{GREEN} β Will create in: {abs_path}{RESET}")
else:
print(f"{GREEN} β Will create in: {project_dir}{RESET}")
if not os.path.exists(project_dir):
confirm = input(f"{YELLOW} Directory doesn't exist. Create it? (Y/n): {RESET}").strip().lower()
if confirm and confirm != 'y' and confirm != '':
print(f"{RED} β Cancelled{RESET}\n")
continue
try:
os.makedirs(project_dir, exist_ok=True)
print(f"{GREEN} β Created directory{RESET}")
except Exception as e:
print(f"{RED} β Failed to create directory: {e}{RESET}\n")
continue
print()
response, files_generated = ask_gemini_with_file_generation(query, project_dir)
clean_response = re.sub(r'<file path="[^"]+">.*?</file>', '', response, flags=re.DOTALL)
clean_response = re.sub(r'<command>.*?</command>', '', clean_response, flags=re.DOTALL)
clean_response = clean_response.strip()
if clean_response:
print(f"\n{GREEN}{clean_response}{RESET}\n")
if not files_generated and '<file' not in response:
print(f"{YELLOW}π‘ Tip: Ask me to create or generate files!{RESET}\n")
elif len(command_parts) == 1:
print(f"{YELLOW}π€ Entering AI mode. Type 'exit' to leave.{RESET}")
print(f"{CYAN}π‘ Use 'ai gen' for file generation mode{RESET}\n")
while True:
query = input(f"{CYAN}ai> {RESET}").strip()
if query.lower() in ["exit", "quit"]:
print(f"{YELLOW}π Leaving AI mode.{RESET}")
break
if not query:
continue
print(f"{YELLOW}π€ Thinking...{RESET}")
answer = ask_gemini(query)
print(f"{GREEN}{answer}{RESET}\n")
else:
query = " ".join(command_parts[1:])
print(f"{YELLOW}π€ Thinking...{RESET}")
answer = ask_gemini(query)
print(f"{GREEN}{answer}{RESET}")
return True
return False |