Skip to content

Commit 7001664

Browse files
committed
basic system and file system made
1 parent 497d8af commit 7001664

File tree

6 files changed

+138
-1
lines changed

6 files changed

+138
-1
lines changed

Main.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from file_system.virtual_fs import * # Import everything from virtual_fs.py
2+
3+
run = True
4+
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+
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
22+
23+
while run:
24+
user_input = input(f"Admin@Python-terminal {current_path} % ")
25+
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+
30+
command = user_input.split(maxsplit=1)
31+
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+
# Read the current content of the file
43+
existing_content = cat(filename)
44+
45+
# Get the new content for the file by prompting the user
46+
new_content = get_multiline_input(existing_content)
47+
48+
# Update the file's content with the new content
49+
edit(filename, new_content)
50+
print(f"File '{filename}' has been updated.")
51+
case ["quit"]:
52+
run = False
53+
case ["tree"]:
54+
tree()
55+
case _:
56+
print(f"zsh: command not found: {user_input}")

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# Python-Terminal
2-
This is a terminal made in python.
2+
This is a Unix like terminal made in python.

file_system/__init__.py

Whitespace-only changes.
194 Bytes
Binary file not shown.
3.39 KB
Binary file not shown.

file_system/virtual_fs.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Virtual filesystem definition
2+
virtual_fs = {
3+
"/": {"home": {}, "var": {}, "tmp": {}},
4+
"/home": {"user": {}},
5+
"/home/user": {"file1.txt": "HelloWorld!", "documents": {}},
6+
"/var": {},
7+
"/tmp": {},
8+
}
9+
10+
current_path = "/"
11+
12+
def cd(path=None):
13+
"""Change the current directory in the virtual filesystem."""
14+
global current_path
15+
if not path or path.strip() == "":
16+
print(current_path)
17+
elif path == "..":
18+
if current_path != "/":
19+
current_path = "/".join(current_path.rstrip("/").split("/")[:-1])
20+
if current_path == "":
21+
current_path = "/"
22+
elif path in virtual_fs.get(current_path, {}):
23+
if isinstance(virtual_fs[current_path][path], dict):
24+
current_path = current_path.rstrip("/") + "/" + path
25+
else:
26+
print(f"cd: no such file or directory: {path}")
27+
28+
29+
def ls():
30+
"""List the contents of the current directory."""
31+
contents = virtual_fs.get(current_path, {})
32+
for item in contents:
33+
print(item)
34+
35+
def cat(filename):
36+
"""Simulate opening and reading a file."""
37+
global current_path
38+
current_dir_contents = virtual_fs.get(current_path, {})
39+
40+
if filename in current_dir_contents:
41+
if isinstance(current_dir_contents[filename], str):
42+
print(current_dir_contents[filename])
43+
else:
44+
print(f"cat: {filename}: Is a directory")
45+
else:
46+
print(f"cat: {filename}: No such file or directory")
47+
48+
def edit(filename, new_content):
49+
"""Simulate editing the content of a file."""
50+
global current_path
51+
current_dir_contents = virtual_fs.get(current_path, {})
52+
53+
if filename in current_dir_contents:
54+
if isinstance(current_dir_contents[filename], str):
55+
# Update the content of the file
56+
current_dir_contents[filename] = new_content
57+
print(f"File '{filename}' has been updated.")
58+
else:
59+
print(f"edit: {filename}: Is a directory")
60+
else:
61+
print(f"edit: {filename}: No such file or directory")
62+
63+
64+
def print_tree(path, indent=""):
65+
"""Recursively print the directory tree, listing files and subdirectories."""
66+
# Get contents of the current path
67+
contents = virtual_fs.get(path, {})
68+
69+
# Iterate through the contents of the directory
70+
for item, content in contents.items():
71+
# If the content is a dictionary, it's a directory, so print its name and recurse into it
72+
if isinstance(content, dict):
73+
print(f"{indent}{item}/") # Print directory with a slash at the end
74+
print_tree(f"{path}/{item}", indent + " ") # Recurse into the directory
75+
else:
76+
# If it's not a dictionary, it's a file, so print its name
77+
print(f"{indent}{item}") # Print file name
78+
79+
def tree():
80+
"""Print the tree of the current directory starting from the root."""
81+
print_tree(current_path)

0 commit comments

Comments
 (0)