Skip to content

Commit 300f9af

Browse files
committed
aliases added
alias ll ls makes ll and alias od ls
1 parent ade5790 commit 300f9af

File tree

5 files changed

+94
-130
lines changed

5 files changed

+94
-130
lines changed

Main.py

Lines changed: 59 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
from file_system import *
1+
from file_system import (
2+
create_folder, create_file, list_contents, read_file, delete_file,
3+
delete_folder, rename_item, change_directory, go_back, print_working_directory,
4+
load_aliases, save_aliases, add_alias, remove_alias, aliases
5+
)
6+
27

38
def display_help():
49
commands = """
@@ -13,67 +18,69 @@ def display_help():
1318
- cd <folder_name> : Change directory
1419
- cd .. : Move to the parent directory
1520
- pwd : Print the current working directory
21+
- alias <name> <command> : Create an alias
22+
- unalias <name> : Remove an alias
1623
- help : Show this help menu
1724
- exit : Exit the program
1825
"""
1926
print(commands)
2027

2128

22-
def main():
23-
while True:
24-
command = input("Admin@Python-Terminal ~ % ").strip()
25-
if not command:
26-
continue
29+
def execute_command(command):
30+
parts = command.split(" ", 2)
31+
cmd = parts[0].lower()
2732

33+
# Check if the command matches an alias
34+
if cmd in aliases:
35+
command = aliases[cmd]
2836
parts = command.split(" ", 2)
2937
cmd = parts[0].lower()
30-
arg1 = parts[1] if len(parts) > 1 else None
31-
arg2 = parts[2] if len(parts) > 2 else None
3238

33-
if cmd == "mkdir" and arg1:
34-
print(create_folder(arg1))
35-
elif cmd == "touch" and arg1:
36-
print(create_file(arg1))
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 arg1:
44-
print(read_file(arg1))
45-
elif cmd == "rm" and arg1:
46-
print(delete_file(arg1))
47-
elif cmd == "rmdir" and arg1:
48-
print(delete_folder(arg1))
49-
elif cmd == "mv" and arg1 and arg2:
50-
print(rename_item(arg1, arg2))
51-
elif cmd == "cd" and arg1:
52-
print(change_directory(arg1))
53-
elif cmd == "cd":
54-
print("zsh: cd must have 1 argument")
55-
elif cmd == "back":
56-
print(go_back())
57-
elif cmd == "pwd":
58-
print(print_working_directory())
59-
elif cmd == "help":
60-
display_help()
61-
elif cmd == "exit":
62-
print("[process Completed]")
63-
break
64-
elif cmd == "alias" and arg1 and arg2:
65-
alias(arg1, arg2)
66-
elif cmd:
67-
if arg1:
68-
arg2 = ""
69-
search_alias(cmd, arg1, arg2)
70-
else:
71-
arg1 = ""
72-
arg2 = ""
73-
search_alias(cmd, arg1, arg2)
74-
else:
75-
if arg1 and arg2:
76-
search_alias(arg1, arg2)
39+
arg1 = parts[1] if len(parts) > 1 else None
40+
arg2 = parts[2] if len(parts) > 2 else None
41+
42+
if cmd == "mkdir" and arg1:
43+
print(create_folder(arg1))
44+
elif cmd == "touch" and arg1:
45+
print(create_file(arg1))
46+
elif cmd == "ls":
47+
contents = list_contents()
48+
print("\n".join(contents) if contents else "Directory is empty.")
49+
elif cmd == "read" and arg1:
50+
print(read_file(arg1))
51+
elif cmd == "rm" and arg1:
52+
print(delete_file(arg1))
53+
elif cmd == "rmdir" and arg1:
54+
print(delete_folder(arg1))
55+
elif cmd == "mv" and arg1 and arg2:
56+
print(rename_item(arg1, arg2))
57+
elif cmd == "cd" and arg1:
58+
print(change_directory(arg1))
59+
elif cmd == "cd":
60+
print("zsh: cd must have 1 argument")
61+
elif cmd == "pwd":
62+
print(print_working_directory())
63+
elif cmd == "alias" and arg1 and arg2:
64+
print(add_alias(arg1, arg2))
65+
elif cmd == "unalias" and arg1:
66+
print(remove_alias(arg1))
67+
elif cmd == "help":
68+
display_help()
69+
elif cmd == "exit":
70+
print("[process Completed]")
71+
save_aliases()
72+
exit(0)
73+
else:
74+
print(f"zsh: command not found: {cmd}")
75+
76+
77+
def main():
78+
load_aliases()
79+
while True:
80+
command = input("Admin@Python-Terminal ~ % ").strip()
81+
if command:
82+
execute_command(command)
83+
7784

7885
if __name__ == "__main__":
7986
main()

__pycache__/Main.cpython-312.pyc

3.13 KB
Binary file not shown.
-1.69 KB
Binary file not shown.

file_system.py

Lines changed: 35 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,5 @@
11
import 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
594
BASE_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
6611
current_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
6919
def 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

7928
def 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

9038
def 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

9845
def 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

11157
def 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

12469
def 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

13781
def 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

15194
def 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

162104
def 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

172113
def 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."

virtual_fs/aliases.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)