1
1
import os
2
2
3
-
4
- def display_help ():
5
- commands = """
6
- Available commands:
7
- - mkdir <folder_name> : Create a folder
8
- - touch <file_name> : Create a file
9
- - ls : List contents of the current directory
10
- - read <file_name> : Read a file
11
- - rm <file_name> : Delete a file
12
- - rmdir <folder_name> : Delete a folder
13
- - mv <old_name> <new_name> : Rename a file or folder
14
- - cd <folder_name> : Change directory
15
- - cd .. : Move to the parent directory
16
- - pwd : Print the current working directory
17
- - help : Show this help menu
18
- - exit : Exit the program
19
- """
20
- print (commands )
21
-
22
- aliases = {}
23
- def find_alias (cmd , arg1 , arg2 ):
24
- cmd = ""
25
- if cmd == "mkdir" and arg1 :
26
- print (create_folder (arg1 ))
27
- elif cmd == "touch" and arg1 :
28
- print (create_file (arg1 ))
29
- elif cmd == "ls" :
30
- contents = list_contents ()
31
- if isinstance (contents , list ):
32
- print ("\n " .join (contents ) if contents else "Directory is empty." )
33
- else :
34
- print (contents )
35
- elif cmd == "read" and arg1 :
36
- print (read_file (arg1 ))
37
- elif cmd == "rm" and arg1 :
38
- print (delete_file (arg1 ))
39
- elif cmd == "rmdir" and arg1 :
40
- print (delete_folder (arg1 ))
41
- elif cmd == "mv" and arg1 and arg2 :
42
- print (rename_item (arg1 , arg2 ))
43
- elif cmd == "cd" and arg1 :
44
- print (change_directory (arg1 ))
45
- elif cmd == "cd" :
46
- print ("zsh: cd must have 1 argument" )
47
- elif cmd == "back" :
48
- print (go_back ())
49
- elif cmd == "pwd" :
50
- print (print_working_directory ())
51
- elif cmd == "help" :
52
- display_help ()
53
- elif cmd == "alias" and arg1 and arg2 :
54
- alias (arg1 , arg2 )
55
- else :
56
- print (f"zsh: command not found: { cmd } " )
57
-
58
3
# Base directory for the virtual file system
59
4
BASE_DIRECTORY = os .path .join (os .getcwd (), "virtual_fs" )
60
5
@@ -65,9 +10,13 @@ def find_alias(cmd, arg1, arg2):
65
10
# Current working directory starts at BASE_DIRECTORY
66
11
current_directory = BASE_DIRECTORY
67
12
13
+ # Alias storage
14
+ ALIAS_FILE = os .path .join (BASE_DIRECTORY , "aliases.txt" )
15
+ aliases = {}
16
+
68
17
18
+ # File system operations
69
19
def create_folder (folder_name ):
70
- """Creates a folder in the current directory."""
71
20
folder_path = os .path .join (current_directory , folder_name )
72
21
try :
73
22
os .makedirs (folder_path , exist_ok = True )
@@ -77,7 +26,6 @@ def create_folder(folder_name):
77
26
78
27
79
28
def create_file (file_name , content = "" ):
80
- """Creates a file in the current directory with optional content."""
81
29
file_path = os .path .join (current_directory , file_name )
82
30
try :
83
31
with open (file_path , "w" ) as file :
@@ -88,15 +36,13 @@ def create_file(file_name, content=""):
88
36
89
37
90
38
def list_contents ():
91
- """Lists the contents of the current directory."""
92
39
try :
93
40
return os .listdir (current_directory )
94
41
except Exception as e :
95
42
return f"Error listing contents: { e } "
96
43
97
44
98
45
def read_file (file_name ):
99
- """Reads the content of a file."""
100
46
file_path = os .path .join (current_directory , file_name )
101
47
if os .path .exists (file_path ):
102
48
try :
@@ -109,7 +55,6 @@ def read_file(file_name):
109
55
110
56
111
57
def delete_file (file_name ):
112
- """Deletes a file from the current directory."""
113
58
file_path = os .path .join (current_directory , file_name )
114
59
if os .path .exists (file_path ):
115
60
try :
@@ -122,7 +67,6 @@ def delete_file(file_name):
122
67
123
68
124
69
def delete_folder (folder_name ):
125
- """Deletes a folder from the current directory."""
126
70
folder_path = os .path .join (current_directory , folder_name )
127
71
if os .path .exists (folder_path ):
128
72
try :
@@ -135,7 +79,6 @@ def delete_folder(folder_name):
135
79
136
80
137
81
def rename_item (old_name , new_name ):
138
- """Renames a file or folder in the current directory."""
139
82
old_path = os .path .join (current_directory , old_name )
140
83
new_path = os .path .join (current_directory , new_name )
141
84
if os .path .exists (old_path ):
@@ -149,7 +92,6 @@ def rename_item(old_name, new_name):
149
92
150
93
151
94
def change_directory (new_directory ):
152
- """Changes the current directory."""
153
95
global current_directory
154
96
target_directory = os .path .join (current_directory , new_directory )
155
97
if os .path .exists (target_directory ) and os .path .isdir (target_directory ):
@@ -160,7 +102,6 @@ def change_directory(new_directory):
160
102
161
103
162
104
def go_back ():
163
- """Navigates to the parent directory."""
164
105
global current_directory
165
106
if current_directory != BASE_DIRECTORY :
166
107
current_directory = os .path .dirname (current_directory )
@@ -170,23 +111,39 @@ def go_back():
170
111
171
112
172
113
def print_working_directory ():
173
- """Returns the current working directory, relative to BASE_DIRECTORY."""
174
114
return "/" + os .path .relpath (current_directory , BASE_DIRECTORY ).replace ("\\ " , "/" )
175
115
176
- def alias (alias , command ):
116
+
117
+ # Alias management
118
+ def load_aliases ():
119
+ global aliases
120
+ try :
121
+ with open (ALIAS_FILE , "r" ) as file :
122
+ for line in file :
123
+ line = line .strip ()
124
+ if "=" in line :
125
+ alias , command = line .split ("=" , 1 )
126
+ aliases [alias ] = command
127
+ except FileNotFoundError :
128
+ pass
129
+
130
+
131
+ def save_aliases ():
132
+ with open (ALIAS_FILE , "w" ) as file :
133
+ for alias , command in aliases .items ():
134
+ file .write (f"{ alias } ={ command } \n " )
135
+
136
+
137
+ def add_alias (alias , command ):
177
138
aliases [alias ] = command
178
- print (f"Alias added: { alias } -> { command } " )
139
+ save_aliases ()
140
+ return f"Alias added: { alias } -> { command } "
141
+
179
142
180
- def search_alias (alias , arg1 , arg2 ):
143
+ def remove_alias (alias ):
181
144
if alias in aliases :
182
- if arg1 and arg2 :
183
- find_alias (alias , arg1 , arg2 )
184
- elif arg1 :
185
- arg2 = ""
186
- find_alias (alias , arg1 , arg2 )
187
- else :
188
- arg1 = ""
189
- arg2 = ""
190
- find_alias (alias , arg1 , arg2 )
145
+ del aliases [alias ]
146
+ save_aliases ()
147
+ return f"Alias '{ alias } ' removed."
191
148
else :
192
- print ( f"zsh: command not found: { alias } " )
149
+ return f"Alias ' { alias } ' does not exist."
0 commit comments