mash/main.py

66 lines
2.5 KiB
Python
Raw Normal View History

#!/usr/bin/python
2024-07-03 12:08:56 +10:00
import os
import socket
2024-07-03 12:08:56 +10:00
import subprocess
from colorama import Fore
2024-07-04 09:49:07 +10:00
from os.path import expanduser
2024-07-03 12:08:56 +10:00
2024-07-04 09:49:07 +10:00
version = "0.1.0"
2024-07-03 13:31:27 +10:00
user = os.getlogin()
2024-07-04 09:49:07 +10:00
initDirectory = expanduser("~")
currentDirectory = initDirectory
2024-07-03 12:08:56 +10:00
argList = ["", ""]
2024-07-04 09:49:07 +10:00
print("Welcome to Mash, " + user + "@" + socket.gethostname() + "! Mash is currently on version " + version)
try:
while True:
2024-07-04 09:49:07 +10:00
os.chdir(currentDirectory)
command = input(Fore.GREEN + user + Fore.WHITE + "@" + socket.gethostname() + Fore.BLUE + " " + currentDirectory + Fore.WHITE + " > ")
argNumber = 0
argList.clear()
for letter in command:
if letter == " ":
argNumber = argNumber + 1
else:
try:
argList[argNumber] = argList[argNumber] + letter
except Exception:
argList.append(letter)
if argList[0] == "help":
2024-07-03 14:46:40 +10:00
print("This is Mash, Max's Shell. Mash has a few built in commands, which you can use to run commands, view files and interact with your system.")
print("Some commands you can run:")
print("")
print("pwd Print directory you are currently in")
print("cd Change your directory.")
print("cat View contents of a file.")
print("version Print current version of Mash.")
print("exit Exit Mash.")
print("")
print("You can also run commands installed by your system. See your OS's documentation for more information.")
elif argList[0] == "exit":
exit()
elif argList[0] == "version":
print(version)
2024-07-04 09:49:07 +10:00
elif argList[0] == "cd":
try:
directoryForCD = argList[1]
if directoryForCD[0] == "~":
currentDirectory = expanduser("~") + directoryForCD.replace("~", "")
if directoryForCD[0] == "/":
currentDirectory = directoryForCD
else:
currentDirectory = (currentDirectory + "/" + directoryForCD)
except Exception:
print("Either a directory was not provided or an error occured.")
else:
try:
subprocess.run(argList)
except Exception:
print("Command", command, "either doesn't exist, isn't in the path or isn't in this directory.")
#except Exception:
# print("Looks like there was an error. Exiting...")
except KeyboardInterrupt:
print(" detected! Exiting...")