From a68ce9c1636093426e190f8646596fd8e8c8f986 Mon Sep 17 00:00:00 2001 From: max Date: Thu, 4 Jul 2024 09:00:53 +1000 Subject: [PATCH] Restructure main.py, remove dependence on shell --- .gitignore | 1 + main.py | 68 +++++++++++++++--------------------------------------- 2 files changed, 19 insertions(+), 50 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a57078d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +builddir diff --git a/main.py b/main.py index 35731f2..f2d0fa2 100755 --- a/main.py +++ b/main.py @@ -11,10 +11,22 @@ user = os.getlogin() print("Welcome to Mash, " + user + "@" + socket.gethostname() + "! Mash is currently on version " + version) +argList = ["", ""] + try: while True: 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("Some commands you can run:") print("") @@ -25,60 +37,16 @@ try: print("exit Exit Mash.") print("") print("You can also run commands installed by your system. See your OS's documentation for more information.") - elif command == "pwd": - print(currentDirectory) - elif command == "exit": + elif argList[0] == "exit": exit() - elif command == "cd": - 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": + elif argList[0] == "version": print(version) else: try: - subprocess.run(command, shell=True) + 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 Exception: +# print("Looks like there was an error. Exiting...") except KeyboardInterrupt: print(" detected! Exiting...")