infinitymatter commited on
Commit
1243cd0
·
verified ·
1 Parent(s): d352977

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +157 -0
main.py ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import psutil
4
+ # Try to import readline or fallback to pyreadline3 (Windows)
5
+ try:
6
+ import readline
7
+ except ImportError:
8
+ try:
9
+ import pyreadline3 as readline
10
+ except ImportError:
11
+ readline = None # no history/autocomplete on Windows if missing
12
+
13
+
14
+ # ========== Optional AI Module (Natural Language Commands) ==========
15
+ try:
16
+ from transformers import pipeline
17
+ ai_enabled = True
18
+ nlp = pipeline("text2text-generation", model="google/flan-t5-small")
19
+ except Exception:
20
+ ai_enabled = False
21
+
22
+
23
+ # ========== Command Handlers ==========
24
+ def cmd_ls(args):
25
+ path = args[0] if args else "."
26
+ try:
27
+ for item in os.listdir(path):
28
+ print(item)
29
+ except FileNotFoundError:
30
+ print(f"ls: cannot access '{path}': No such file or directory")
31
+
32
+
33
+ def cmd_cd(args):
34
+ if not args:
35
+ print("cd: missing operand")
36
+ return
37
+ try:
38
+ os.chdir(args[0])
39
+ except FileNotFoundError:
40
+ print(f"cd: {args[0]}: No such file or directory")
41
+
42
+
43
+ def cmd_pwd(args):
44
+ print(os.getcwd())
45
+
46
+
47
+ def cmd_mkdir(args):
48
+ if not args:
49
+ print("mkdir: missing operand")
50
+ return
51
+ try:
52
+ os.mkdir(args[0])
53
+ except FileExistsError:
54
+ print(f"mkdir: cannot create directory '{args[0]}': File exists")
55
+
56
+
57
+ def cmd_rm(args):
58
+ if not args:
59
+ print("rm: missing operand")
60
+ return
61
+ target = args[0]
62
+ if os.path.isdir(target):
63
+ try:
64
+ shutil.rmtree(target)
65
+ except Exception as e:
66
+ print(f"rm: cannot remove '{target}': {e}")
67
+ else:
68
+ try:
69
+ os.remove(target)
70
+ except FileNotFoundError:
71
+ print(f"rm: cannot remove '{target}': No such file or directory")
72
+
73
+
74
+ def cmd_monitor(args):
75
+ cpu = psutil.cpu_percent(interval=1)
76
+ mem = psutil.virtual_memory()
77
+ print(f"CPU Usage: {cpu}%")
78
+ print(f"Memory Usage: {mem.percent}%")
79
+
80
+
81
+ def cmd_help(args):
82
+ print("Available commands:")
83
+ for cmd in commands.keys():
84
+ print(f" - {cmd}")
85
+ if ai_enabled:
86
+ print(" - ai (natural language queries)")
87
+
88
+
89
+ # ========== AI Command ==========
90
+ def cmd_ai(args):
91
+ if not ai_enabled:
92
+ print("AI features not available (transformers not installed).")
93
+ return
94
+ if not args:
95
+ print("ai: missing query")
96
+ return
97
+ query = " ".join(args)
98
+ print(f"AI Query: {query}")
99
+ try:
100
+ result = nlp(query, max_length=100)[0]['generated_text']
101
+ print("AI Suggestion:", result)
102
+ except Exception as e:
103
+ print(f"AI error: {e}")
104
+
105
+
106
+ # ========== Command Map ==========
107
+ commands = {
108
+ "ls": cmd_ls,
109
+ "cd": cmd_cd,
110
+ "pwd": cmd_pwd,
111
+ "mkdir": cmd_mkdir,
112
+ "rm": cmd_rm,
113
+ "monitor": cmd_monitor,
114
+ "help": cmd_help,
115
+ "ai": cmd_ai,
116
+ }
117
+
118
+
119
+ # ========== Autocomplete ==========
120
+ def completer(text, state):
121
+ options = [cmd for cmd in commands.keys() if cmd.startswith(text)]
122
+ if state < len(options):
123
+ return options[state]
124
+ return None
125
+
126
+ if readline:
127
+ readline.parse_and_bind("tab: complete")
128
+ readline.set_completer(completer)
129
+
130
+
131
+ # ========== Main Loop ==========
132
+ def main():
133
+ print("Python Terminal Emulator (type 'help' for commands, 'exit' to quit)")
134
+ while True:
135
+ try:
136
+ user_input = input(f"{os.getcwd()} $ ").strip()
137
+ if not user_input:
138
+ continue
139
+ if user_input.lower() in ["exit", "quit"]:
140
+ print("Exiting terminal.")
141
+ break
142
+
143
+ parts = user_input.split()
144
+ cmd, args = parts[0], parts[1:]
145
+
146
+ if cmd in commands:
147
+ commands[cmd](args)
148
+ else:
149
+ print(f"{cmd}: command not found. Type 'help' for available commands.")
150
+ except KeyboardInterrupt:
151
+ print("\nUse 'exit' to quit.")
152
+ except Exception as e:
153
+ print("Error:", e)
154
+
155
+
156
+ if __name__ == "__main__":
157
+ main()