11import os
22
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-
583# Base directory for the virtual file system
594BASE_DIRECTORY = os .path .join (os .getcwd (), "virtual_fs" )
605
@@ -65,9 +10,13 @@ def find_alias(cmd, arg1, arg2):
6510# Current working directory starts at BASE_DIRECTORY
6611current_directory = BASE_DIRECTORY
6712
13+ # Alias storage
14+ ALIAS_FILE = os .path .join (BASE_DIRECTORY , "aliases.txt" )
15+ aliases = {}
16+
6817
18+ # File system operations
6919def create_folder (folder_name ):
70- """Creates a folder in the current directory."""
7120 folder_path = os .path .join (current_directory , folder_name )
7221 try :
7322 os .makedirs (folder_path , exist_ok = True )
@@ -77,7 +26,6 @@ def create_folder(folder_name):
7726
7827
7928def create_file (file_name , content = "" ):
80- """Creates a file in the current directory with optional content."""
8129 file_path = os .path .join (current_directory , file_name )
8230 try :
8331 with open (file_path , "w" ) as file :
@@ -88,15 +36,13 @@ def create_file(file_name, content=""):
8836
8937
9038def list_contents ():
91- """Lists the contents of the current directory."""
9239 try :
9340 return os .listdir (current_directory )
9441 except Exception as e :
9542 return f"Error listing contents: { e } "
9643
9744
9845def read_file (file_name ):
99- """Reads the content of a file."""
10046 file_path = os .path .join (current_directory , file_name )
10147 if os .path .exists (file_path ):
10248 try :
@@ -109,7 +55,6 @@ def read_file(file_name):
10955
11056
11157def delete_file (file_name ):
112- """Deletes a file from the current directory."""
11358 file_path = os .path .join (current_directory , file_name )
11459 if os .path .exists (file_path ):
11560 try :
@@ -122,7 +67,6 @@ def delete_file(file_name):
12267
12368
12469def delete_folder (folder_name ):
125- """Deletes a folder from the current directory."""
12670 folder_path = os .path .join (current_directory , folder_name )
12771 if os .path .exists (folder_path ):
12872 try :
@@ -135,7 +79,6 @@ def delete_folder(folder_name):
13579
13680
13781def rename_item (old_name , new_name ):
138- """Renames a file or folder in the current directory."""
13982 old_path = os .path .join (current_directory , old_name )
14083 new_path = os .path .join (current_directory , new_name )
14184 if os .path .exists (old_path ):
@@ -149,7 +92,6 @@ def rename_item(old_name, new_name):
14992
15093
15194def change_directory (new_directory ):
152- """Changes the current directory."""
15395 global current_directory
15496 target_directory = os .path .join (current_directory , new_directory )
15597 if os .path .exists (target_directory ) and os .path .isdir (target_directory ):
@@ -160,7 +102,6 @@ def change_directory(new_directory):
160102
161103
162104def go_back ():
163- """Navigates to the parent directory."""
164105 global current_directory
165106 if current_directory != BASE_DIRECTORY :
166107 current_directory = os .path .dirname (current_directory )
@@ -170,23 +111,39 @@ def go_back():
170111
171112
172113def print_working_directory ():
173- """Returns the current working directory, relative to BASE_DIRECTORY."""
174114 return "/" + os .path .relpath (current_directory , BASE_DIRECTORY ).replace ("\\ " , "/" )
175115
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 ):
177138 aliases [alias ] = command
178- print (f"Alias added: { alias } -> { command } " )
139+ save_aliases ()
140+ return f"Alias added: { alias } -> { command } "
141+
179142
180- def search_alias (alias , arg1 , arg2 ):
143+ def remove_alias (alias ):
181144 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."
191148 else :
192- print ( f"zsh: command not found: { alias } " )
149+ return f"Alias ' { alias } ' does not exist."
0 commit comments