| | import os |
| | import shutil |
| | import psutil |
| | |
| | try: |
| | import readline |
| | except ImportError: |
| | try: |
| | import pyreadline3 as readline |
| | except ImportError: |
| | readline = None |
| |
|
| |
|
| | |
| | try: |
| | from transformers import pipeline |
| | ai_enabled = True |
| | nlp = pipeline("text2text-generation", model="google/flan-t5-small") |
| | except Exception: |
| | ai_enabled = False |
| |
|
| |
|
| | |
| | def cmd_ls(args): |
| | path = args[0] if args else "." |
| | try: |
| | for item in os.listdir(path): |
| | print(item) |
| | except FileNotFoundError: |
| | print(f"ls: cannot access '{path}': No such file or directory") |
| |
|
| |
|
| | def cmd_cd(args): |
| | if not args: |
| | print("cd: missing operand") |
| | return |
| | try: |
| | os.chdir(args[0]) |
| | except FileNotFoundError: |
| | print(f"cd: {args[0]}: No such file or directory") |
| |
|
| |
|
| | def cmd_pwd(args): |
| | print(os.getcwd()) |
| |
|
| |
|
| | def cmd_mkdir(args): |
| | if not args: |
| | print("mkdir: missing operand") |
| | return |
| | try: |
| | os.mkdir(args[0]) |
| | except FileExistsError: |
| | print(f"mkdir: cannot create directory '{args[0]}': File exists") |
| |
|
| |
|
| | def cmd_rm(args): |
| | if not args: |
| | print("rm: missing operand") |
| | return |
| | target = args[0] |
| | if os.path.isdir(target): |
| | try: |
| | shutil.rmtree(target) |
| | except Exception as e: |
| | print(f"rm: cannot remove '{target}': {e}") |
| | else: |
| | try: |
| | os.remove(target) |
| | except FileNotFoundError: |
| | print(f"rm: cannot remove '{target}': No such file or directory") |
| |
|
| |
|
| | def cmd_monitor(args): |
| | cpu = psutil.cpu_percent(interval=1) |
| | mem = psutil.virtual_memory() |
| | print(f"CPU Usage: {cpu}%") |
| | print(f"Memory Usage: {mem.percent}%") |
| |
|
| |
|
| | def cmd_help(args): |
| | print("Available commands:") |
| | for cmd in commands.keys(): |
| | print(f" - {cmd}") |
| | if ai_enabled: |
| | print(" - ai (natural language queries)") |
| |
|
| |
|
| | |
| | def cmd_ai(args): |
| | if not ai_enabled: |
| | print("AI features not available (transformers not installed).") |
| | return |
| | if not args: |
| | print("ai: missing query") |
| | return |
| | query = " ".join(args) |
| | print(f"AI Query: {query}") |
| | try: |
| | result = nlp(query, max_length=100)[0]['generated_text'] |
| | print("AI Suggestion:", result) |
| | except Exception as e: |
| | print(f"AI error: {e}") |
| |
|
| |
|
| | |
| | commands = { |
| | "ls": cmd_ls, |
| | "cd": cmd_cd, |
| | "pwd": cmd_pwd, |
| | "mkdir": cmd_mkdir, |
| | "rm": cmd_rm, |
| | "monitor": cmd_monitor, |
| | "help": cmd_help, |
| | "ai": cmd_ai, |
| | } |
| |
|
| |
|
| | |
| | def completer(text, state): |
| | options = [cmd for cmd in commands.keys() if cmd.startswith(text)] |
| | if state < len(options): |
| | return options[state] |
| | return None |
| |
|
| | if readline: |
| | readline.parse_and_bind("tab: complete") |
| | readline.set_completer(completer) |
| |
|
| |
|
| | |
| | def main(): |
| | print("Python Terminal Emulator (type 'help' for commands, 'exit' to quit)") |
| | while True: |
| | try: |
| | user_input = input(f"{os.getcwd()} $ ").strip() |
| | if not user_input: |
| | continue |
| | if user_input.lower() in ["exit", "quit"]: |
| | print("Exiting terminal.") |
| | break |
| |
|
| | parts = user_input.split() |
| | cmd, args = parts[0], parts[1:] |
| |
|
| | if cmd in commands: |
| | commands[cmd](args) |
| | else: |
| | print(f"{cmd}: command not found. Type 'help' for available commands.") |
| | except KeyboardInterrupt: |
| | print("\nUse 'exit' to quit.") |
| | except Exception as e: |
| | print("Error:", e) |
| |
|
| |
|
| | if __name__ == "__main__": |
| | main() |
| |
|