Restructure main.py, remove dependence on shell

This commit is contained in:
Maxwell 2024-07-04 09:00:53 +10:00
parent 4f130ce28c
commit a68ce9c163
2 changed files with 19 additions and 50 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
builddir

68
main.py
View File

@ -11,10 +11,22 @@ user = os.getlogin()
print("Welcome to Mash, " + user + "@" + socket.gethostname() + "! Mash is currently on version " + version) print("Welcome to Mash, " + user + "@" + socket.gethostname() + "! Mash is currently on version " + version)
argList = ["", ""]
try: try:
while True: while True:
command = input(Fore.GREEN + user + Fore.WHITE + "@" + socket.gethostname() + Fore.BLUE + " " + currentDirectory + Fore.WHITE + " > ") command = input(Fore.GREEN + user + Fore.WHITE + "@" + socket.gethostname() + Fore.BLUE + " " + currentDirectory + Fore.WHITE + " > ")
if command == "help": 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":
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("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("Some commands you can run:")
print("") print("")
@ -25,60 +37,16 @@ try:
print("exit Exit Mash.") print("exit Exit Mash.")
print("") print("")
print("You can also run commands installed by your system. See your OS's documentation for more information.") print("You can also run commands installed by your system. See your OS's documentation for more information.")
elif command == "pwd": elif argList[0] == "exit":
print(currentDirectory)
elif command == "exit":
exit() exit()
elif command == "cd": elif argList[0] == "version":
try:
cdDirectory = input("Directory: ")
if cdDirectory[0] == "/":
if os.path.isdir(cdDirectory) == False:
if os.path.isfile(cdDirectory) == True:
print("That's not a directory, that's a file!")
else:
print("That directory doesn't exist.")
else:
currentDirectory = cdDirectory
elif cdDirectory == ".":
currentDirectory == currentDirectory
elif cdDirectory == "..":
currentDirectory == currentDirectory
print("I'm working on that lol")
else:
if os.path.isdir(currentDirectory + cdDirectory) == False:
if os.path.isfile(currentDirectory + cdDirectory) == True:
print("That's not a directory, that's a file!")
else:
print("That directory doesn't exist.")
else:
currentDirectory = cdDirectory
except Exception:
print("Either there was an error or that file doesn't exist.")
elif command == ("cat"):
try:
catFile = input("File to catalog: ")
if catFile[0] == "/":
catFileOpened = open(catFile)
print(catFileOpened.read)
else:
catFileOpened = open(currentDirectory + catFile)
print(catFileOpened.read())
except Exception:
print("File", catFile, "either doesn't exist or isn't in this directory.")
elif command == "touch":
touchName = input("File name?")
elif command == "ls":
print(os.listdir())
elif command == "version":
print(version) print(version)
else: else:
try: try:
subprocess.run(command, shell=True) subprocess.run(argList)
except Exception: except Exception:
print("Command", command, "either doesn't exist, isn't in the path or isn't in this directory.") print("Command", command, "either doesn't exist, isn't in the path or isn't in this directory.")
except Exception: #except Exception:
print("Looks like there was an error. Exiting...") # print("Looks like there was an error. Exiting...")
except KeyboardInterrupt: except KeyboardInterrupt:
print(" detected! Exiting...") print(" detected! Exiting...")