Add colorful CLI with CoderCLI> prompt and /command shortcuts

parent 8eee7e27
...@@ -16,6 +16,21 @@ from dataclasses import dataclass, field ...@@ -16,6 +16,21 @@ from dataclasses import dataclass, field
import requests import requests
# ANSI color codes
class Colors:
"""ANSI color codes for terminal output."""
RESET = "\033[0m"
BOLD = "\033[1m"
DIM = "\033[2m"
RED = "\033[91m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
BLUE = "\033[94m"
MAGENTA = "\033[95m"
CYAN = "\033[96m"
WHITE = "\033[97m"
# Default system prompt for normal models # Default system prompt for normal models
DEFAULT_SYSTEM_PROMPT = """You are Coder, an AI coding assistant. You help users write, read, and modify code files. You have access to tools for file operations. DEFAULT_SYSTEM_PROMPT = """You are Coder, an AI coding assistant. You help users write, read, and modify code files. You have access to tools for file operations.
...@@ -683,86 +698,90 @@ class CoderClient: ...@@ -683,86 +698,90 @@ class CoderClient:
def run_interactive_shell(client: CoderClient) -> None: def run_interactive_shell(client: CoderClient) -> None:
"""Run interactive REPL shell.""" """Run interactive REPL shell."""
print("=" * 60) print(f"{Colors.CYAN}{'=' * 60}{Colors.RESET}")
print(" coder - Interactive Coding Assistant") print(f"{Colors.BOLD}{Colors.GREEN} coder - Interactive Coding Assistant{Colors.RESET}")
print("=" * 60) print(f"{Colors.CYAN}{'=' * 60}{Colors.RESET}")
print("Type 'quit', 'exit' or press Ctrl+C to exit.") print(f"{Colors.DIM}Type /quit, /exit or press Ctrl+C to exit.{Colors.RESET}")
print("Type 'clear' to clear conversation history.") print(f"{Colors.DIM}Type /clear to clear conversation history.{Colors.RESET}")
print("Type 'help' for more commands.") print(f"{Colors.DIM}Type /help for more commands.{Colors.RESET}")
print("-" * 60) print(f"{Colors.CYAN}{'-' * 60}{Colors.RESET}")
while True: while True:
try: try:
print() # Colorful prompt
user_input = input("> ").strip() prompt = f"{Colors.BOLD}{Colors.BLUE}CoderCLI>{Colors.RESET} "
user_input = input(prompt).strip()
if not user_input: if not user_input:
continue continue
if user_input.lower() in ('quit', 'exit', 'q'): # Handle commands with / prefix
print("Goodbye!") cmd = user_input.lower()
if cmd in ('/quit', '/exit', '/q'):
print(f"{Colors.GREEN}Goodbye!{Colors.RESET}")
break break
if user_input.lower() == 'clear': if cmd == '/clear' or cmd == '/c':
client.clear_history() client.clear_history()
print("Conversation history cleared.") print(f"{Colors.YELLOW}Conversation history cleared.{Colors.RESET}")
continue continue
if user_input.lower() == 'help': if cmd == '/help' or cmd == '/h':
print_help() print_help()
continue continue
if user_input.lower().startswith('/read '): if cmd.startswith('/read '):
path = user_input[6:].strip() path = user_input[6:].strip()
result = client.tool_executor._read_file(path) result = client.tool_executor._read_file(path)
if 'content' in result: if 'content' in result:
print(f"\n--- Content of {path} ---") print(f"\n{Colors.CYAN}--- Content of {path} ---{Colors.RESET}")
print(result['content']) print(result['content'])
print("--- End ---") print(f"{Colors.CYAN}--- End ---{Colors.RESET}")
else: else:
print(f"Error: {result.get('error', 'Unknown error')}") print(f"{Colors.RED}Error: {result.get('error', 'Unknown error')}{Colors.RESET}")
continue continue
if user_input.lower().startswith('/exec '): if cmd.startswith('/exec '):
command = user_input[6:].strip() command = user_input[6:].strip()
result = client.tool_executor._execute_command(command, ".") result = client.tool_executor._execute_command(command, ".")
print(f"\n$ {command}") print(f"\n{Colors.GREEN}$ {command}{Colors.RESET}")
if result.get('stdout'): if result.get('stdout'):
print(result['stdout']) print(result['stdout'])
if result.get('stderr'): if result.get('stderr'):
print("stderr:", result['stderr'], file=sys.stderr) print(f"{Colors.RED}stderr: {result['stderr']}{Colors.RESET}", file=sys.stderr)
if result.get('returncode', 0) != 0: if result.get('returncode', 0) != 0:
print(f"Exit code: {result['returncode']}") print(f"{Colors.RED}Exit code: {result['returncode']}{Colors.RESET}")
continue continue
# Send message to LLM # Send message to LLM
client.chat(user_input) client.chat(user_input)
except KeyboardInterrupt: except KeyboardInterrupt:
print("\nGoodbye!") print(f"\n{Colors.GREEN}Goodbye!{Colors.RESET}")
break break
except EOFError: except EOFError:
print("\nGoodbye!") print(f"\n{Colors.GREEN}Goodbye!{Colors.RESET}")
break break
def print_help(): def print_help():
"""Print help information.""" """Print help information."""
print(""" print(f"""
Commands: {Colors.BOLD}{Colors.CYAN}Commands:{Colors.RESET}
quit, exit, q Exit the shell {Colors.GREEN}/quit, /exit, /q{Colors.RESET} Exit the shell
clear Clear conversation history {Colors.GREEN}/clear, /c{Colors.RESET} Clear conversation history
help Show this help message {Colors.GREEN}/help, /h{Colors.RESET} Show this help message
Shortcuts: {Colors.BOLD}{Colors.CYAN}Shortcuts:{Colors.RESET}
/read <path> Read a file directly {Colors.YELLOW}/read <path>{Colors.RESET} Read a file directly
/exec <command> Execute a shell command directly {Colors.YELLOW}/exec <command>{Colors.RESET} Execute a shell command directly
The assistant can use tools to: {Colors.BOLD}{Colors.CYAN}The assistant can use tools to:{Colors.RESET}
- read_file: Read file contents - {Colors.BLUE}read_file{Colors.RESET}: Read file contents
- write_file: Write/create files - {Colors.BLUE}write_file{Colors.RESET}: Write/create files
- apply_diff: Apply patches to files - {Colors.BLUE}apply_diff{Colors.RESET}: Apply patches to files
- execute_command: Run shell commands - {Colors.BLUE}execute_command{Colors.RESET}: Run shell commands
""") """)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment