Skip to content

Commit 72ed1df

Browse files
committed
Added edit to edit text document added a system
monitor and also a system information like neofetch with ascii art
1 parent aa8293c commit 72ed1df

File tree

8 files changed

+127
-8
lines changed

8 files changed

+127
-8
lines changed

Main.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
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-
1+
import readline
2+
from file_system import *
73

84
def display_help():
95
commands = """
106
Available commands:
117
- mkdir <folder_name> : Create a folder
128
- touch <file_name> : Create a file
139
- ls : List contents of the current directory
14-
- read <file_name> : Read a file
10+
- cat <file_name> : Read a file
1511
- rm <file_name> : Delete a file
1612
- rmdir <folder_name> : Delete a folder
1713
- mv <old_name> <new_name> : Rename a file or folder
@@ -20,12 +16,14 @@ def display_help():
2016
- pwd : Print the current working directory
2117
- alias <name> <command> : Create an alias
2218
- unalias <name> : Remove an alias
19+
- edit <file_name> : Edit a file (opens in a basic text editor)
2320
- help : Show this help menu
2421
- exit : Exit the program
2522
"""
2623
print(commands)
2724

2825

26+
2927
def execute_command(command):
3028
parts = command.split(" ", 2)
3129
cmd = parts[0].lower()
@@ -46,7 +44,7 @@ def execute_command(command):
4644
elif cmd == "ls":
4745
contents = list_contents()
4846
print("\n".join(contents) if contents else "Directory is empty.")
49-
elif cmd == "read" and arg1:
47+
elif cmd == "cat" and arg1:
5048
print(read_file(arg1))
5149
elif cmd == "rm" and arg1:
5250
print(delete_file(arg1))
@@ -64,21 +62,47 @@ def execute_command(command):
6462
print(add_alias(arg1, arg2))
6563
elif cmd == "unalias" and arg1:
6664
print(remove_alias(arg1))
65+
elif cmd == "sysmonitor":
66+
sys_monitor()
67+
elif cmd == "sysinfo":
68+
sys_info()
6769
elif cmd == "help":
6870
display_help()
6971
elif cmd == "exit":
7072
print("[process Completed]")
7173
save_aliases()
7274
exit(0)
75+
elif cmd == "edit" and arg1:
76+
print(edit_file(arg1)) # Call the edit_file function
7377
else:
7478
print(f"zsh: command not found: {cmd}")
7579

7680

7781
def main():
7882
load_aliases()
83+
84+
# Set up history using the readline module
85+
# You can set a limit to how many commands are saved in history
86+
readline.set_history_length(100)
87+
88+
# Load previous history if available
89+
try:
90+
with open('history.txt', 'r') as history_file:
91+
for line in history_file:
92+
readline.add_history(line.strip())
93+
except FileNotFoundError:
94+
pass
95+
7996
while True:
8097
command = input("Admin@Python-Terminal ~ % ").strip()
98+
8199
if command:
100+
# Add the command to the history after execution
101+
readline.add_history(command)
102+
# Save the history to a file
103+
with open('history.txt', 'a') as history_file:
104+
history_file.write(command + "\n")
105+
82106
execute_command(command)
83107

84108

4.2 KB
Binary file not shown.

file_system.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import os
2+
import psutil
3+
import platform
4+
import pyfiglet
5+
import time
6+
from datetime import timedelta
27

38
# Base directory for the virtual file system
49
BASE_DIRECTORY = os.path.join(os.getcwd(), "virtual_fs")
@@ -147,3 +152,81 @@ def remove_alias(alias):
147152
return f"Alias '{alias}' removed."
148153
else:
149154
return f"Alias '{alias}' does not exist."
155+
156+
def edit_file(file_name):
157+
"""Allow the user to edit an existing file, clearing its contents first."""
158+
file_path = os.path.join(current_directory, file_name)
159+
160+
# Check if the file exists
161+
if not os.path.exists(file_path):
162+
return f"File '{file_name}' does not exist."
163+
164+
# Clear the file by opening it in write mode
165+
with open(file_path, "w") as file:
166+
pass # This clears the file content
167+
168+
print(f"File '{file_name}' has been cleared. You can now edit it.")
169+
170+
# Allow the user to add new lines to the file
171+
content = []
172+
while True:
173+
user_input = input("Enter text to add (or 'exit' to save and exit): ")
174+
if user_input.lower() == "exit":
175+
break
176+
else:
177+
content.append(user_input + "\n")
178+
179+
# Save the new content to the file
180+
with open(file_path, "w") as file:
181+
file.writelines(content)
182+
183+
return f"File '{file_name}' has been updated."
184+
185+
186+
def sys_monitor():
187+
mem = psutil.virtual_memory()
188+
cpu = psutil.cpu_percent(interval=1)
189+
print(f"Total Memory: {mem.total / (1024 * 1024)}MB")
190+
print(f"Used Memory: {mem.used / (1024 * 1024)}MB")
191+
print(f"Free Memory: {mem.free / (1024 * 1024)}MB")
192+
print(f"CPU Usage: {cpu}%")
193+
194+
def get_uptime():
195+
boot_time = psutil.boot_time()
196+
uptime_seconds = time.time() - boot_time
197+
uptime = str(timedelta(seconds=uptime_seconds))
198+
return uptime
199+
200+
def get_memory():
201+
mem = psutil.virtual_memory()
202+
total_mem = mem.total / (1024 * 1024 * 1024) # in GB
203+
used_mem = mem.used / (1024 * 1024 * 1024) # in GB
204+
return total_mem, used_mem
205+
206+
def get_cpu():
207+
cpu_count = psutil.cpu_count(logical=False) # Physical cores
208+
cpu_freq = psutil.cpu_freq().current # Current CPU frequency
209+
return cpu_count, cpu_freq
210+
211+
def get_disk_usage():
212+
disk = psutil.disk_usage('/')
213+
total_disk = disk.total / (1024 * 1024 * 1024) # in GB
214+
used_disk = disk.used / (1024 * 1024 * 1024) # in GB
215+
return total_disk, used_disk
216+
217+
def ascii_text(text):
218+
return pyfiglet.figlet_format(text, font="slant")
219+
220+
def sys_info():
221+
os_arch = platform.architecture()[0]
222+
uptime = get_uptime()
223+
total_mem, used_mem = get_memory()
224+
cpu_count, cpu_freq = get_cpu()
225+
print(ascii_text("System Info"))
226+
print(f"\nOS: Python-terminal 2.3.0 ({os_arch})")
227+
print(f"Host: Admin")
228+
print(f"Kernel: 2.3.0")
229+
print(f"Uptime: {uptime}")
230+
print(f"Shell: Python-Terminal Shell")
231+
print(f"CPU: {cpu_count} cores, {cpu_freq} MHz")
232+
print(f"RAM: {used_mem:.2f}GB / {total_mem:.2f}GB")

history.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
hello
2+
sysmonitor
3+
sysmonitor
4+
sysinfo
5+
exit
6+
sysmonitor
7+
sysinfo
8+
exity
9+
exit

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
psutil
2+
pyfiglet

virtual_fs/aliases.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ll=ls

virtual_fs/dev/test/hello_test.txt

Whitespace-only changes.

virtual_fs/home/hello.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)