|
1 |
| -from file_system.virtual_fs import * # Import everything from virtual_fs.py |
| 1 | +from file_system import create_folder, create_file, list_contents, read_file, delete_file, delete_folder |
2 | 2 |
|
3 |
| -run = True |
| 3 | +def display_help(): |
| 4 | + """Displays available commands.""" |
| 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 virtual directory |
| 10 | +- read <file_name> : Read a file |
| 11 | +- rm <file_name> : Delete a file |
| 12 | +- rmdir <folder_name> : Delete a folder |
| 13 | +- help : Show this help menu |
| 14 | +- exit : Exit the program |
| 15 | +""" |
| 16 | + print(commands) |
4 | 17 |
|
5 |
| -def get_multiline_input(existing_content=""): |
6 |
| - """Function to get multiline input from the user, showing existing content.""" |
7 |
| - print("Enter the content for the file (Type 'DONE' on a new line to finish):") |
8 |
| - |
9 |
| - # Show existing content if available |
10 |
| - if existing_content: |
11 |
| - print("\nCurrent content of the file:") |
12 |
| - print(existing_content) |
13 |
| - print("\nYou can now modify the content. Continue editing...\n") |
14 | 18 |
|
15 |
| - content = [] |
16 |
| - while True: |
17 |
| - line = input() |
18 |
| - if line.strip().upper() == "DONE": # If the user types DONE, stop collecting input |
19 |
| - break |
20 |
| - content.append(line) |
21 |
| - return "\n".join(content) # Join the content with newlines between each line |
| 19 | +def main(): |
| 20 | + """Command interpreter for the file system.""" |
| 21 | + print("Welcome to the Python Terminal Virtual File System!") |
| 22 | + display_help() |
22 | 23 |
|
23 |
| -while run: |
24 |
| - user_input = input(f"Admin@Python-terminal {current_path} % ") |
| 24 | + while True: |
| 25 | + command = input("\n>>> ").strip() |
| 26 | + if not command: |
| 27 | + continue |
25 | 28 |
|
26 |
| - # Handle empty input case |
27 |
| - if user_input.strip() == "": # If the user input is empty or just spaces |
28 |
| - continue # Simply skip to the next loop iteration |
| 29 | + parts = command.split(" ", 1) |
| 30 | + cmd = parts[0].lower() |
| 31 | + arg = parts[1] if len(parts) > 1 else None |
29 | 32 |
|
30 |
| - command = user_input.split(maxsplit=1) |
| 33 | + if cmd == "mkdir" and arg: |
| 34 | + print(create_folder(arg)) |
| 35 | + elif cmd == "touch" and arg: |
| 36 | + print(create_file(arg)) |
| 37 | + elif cmd == "ls": |
| 38 | + contents = list_contents() |
| 39 | + if isinstance(contents, list): |
| 40 | + print("\n".join(contents) if contents else "Directory is empty.") |
| 41 | + else: |
| 42 | + print(contents) |
| 43 | + elif cmd == "read" and arg: |
| 44 | + print(read_file(arg)) |
| 45 | + elif cmd == "rm" and arg: |
| 46 | + print(delete_file(arg)) |
| 47 | + elif cmd == "rmdir" and arg: |
| 48 | + print(delete_folder(arg)) |
| 49 | + elif cmd == "help": |
| 50 | + display_help() |
| 51 | + elif cmd == "exit": |
| 52 | + print("Exiting the program. Goodbye!") |
| 53 | + break |
| 54 | + else: |
| 55 | + print("Unknown command. Type 'help' for a list of commands.") |
31 | 56 |
|
32 |
| - match command: |
33 |
| - case ["cd"]: |
34 |
| - cd() |
35 |
| - case ["cd", path]: |
36 |
| - cd(path) |
37 |
| - case ["ls"]: |
38 |
| - ls() |
39 |
| - case ["cat", filename]: |
40 |
| - cat(filename) |
41 |
| - case ["edit", filename] if len(command) > 1: # Check if the filename is provided |
42 |
| - existing_content = cat(filename) |
43 |
| - new_content = get_multiline_input(existing_content) |
44 |
| - edit(filename, new_content) |
45 |
| - print(f"File '{filename}' has been updated.") |
46 |
| - case ["quit"]: |
47 |
| - run = False |
48 |
| - case ["pwd"]: |
49 |
| - pwd() |
50 |
| - case ["mkdir", dirname]: |
51 |
| - mkdir(dirname) |
52 |
| - case ["touch", filename]: |
53 |
| - touch(filename) |
54 |
| - case _: |
55 |
| - print(f"zsh: command not found: {user_input}") |
| 57 | +if __name__ == "__main__": |
| 58 | + main() |
0 commit comments