Add a whole lot of comments

This commit is contained in:
Maxwell 2024-07-05 09:42:14 +10:00
parent 929ddb73f3
commit d87b1ff110

32
main.py
View File

@ -1,38 +1,52 @@
#!/usr/bin/python #!/usr/bin/python
# Imports, of course.
# You'll need to install colorama from Pip if you want to use colours in the terminal. I'm probably going to replace this when I get around to making a config file properly.
import os import os
import socket import socket
import subprocess import subprocess
import shutil import shutil
import curses
from datetime import datetime from datetime import datetime
from colorama import Fore from colorama import Fore
from os.path import expanduser from os.path import expanduser
# Some initial variables
version = "0.1.0" version = "0.1.0"
user = os.getlogin() user = os.getlogin()
initDirectory = expanduser("~") initDirectory = expanduser("~")
currentDirectory = initDirectory currentDirectory = initDirectory
argList = ["", ""] argList = ["", ""]
# Start work on checking whether a config file exists
if os.path.isfile(expanduser("~") + "/.config/mash/mash.conf") == True: if os.path.isfile(expanduser("~") + "/.config/mash/mash.conf") == True:
configFile = open(expanduser("~") + "/.config/mash/mash.conf") configFile = open(expanduser("~") + "/.config/mash/mash.conf")
else: else:
print("Config file does not exist. Copying default file to ~/.config/mash/mash.conf") print("Config file does not exist. Copying default file to ~/.config/mash/mash.conf")
shutil.copyfile("/etc/mash/mash.conf", (expanduser("~") + "/.config/mash/mash.conf")) shutil.copyfile("/etc/mash/mash.conf", (expanduser("~") + "/.config/mash/mash.conf"))
# Write the time to the history file
historyFile = open((expanduser("~") + "/.history.mash"), "a") historyFile = open((expanduser("~") + "/.history.mash"), "a")
historyFile.write("\n") historyFile.write("\n")
historyFile.write(("Session on " + str(datetime.now()))) historyFile.write(("Session on " + str(datetime.now())))
historyFile.write("\n") historyFile.write("\n")
# Welcome message. Probably going to be configurable soon enough.
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)
# Main loop.
try: try:
while True: while True:
# Set directory and prompt for input.
os.chdir(currentDirectory) os.chdir(currentDirectory)
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 + " > ")
# Clean some things up for the argument parser.
argNumber = 0 argNumber = 0
argList.clear() argList.clear()
# Argument parser
for letter in command: for letter in command:
if letter == " ": if letter == " ":
argNumber = argNumber + 1 argNumber = argNumber + 1
@ -41,15 +55,19 @@ try:
argList[argNumber] = argList[argNumber] + letter argList[argNumber] = argList[argNumber] + letter
except Exception: except Exception:
argList.append(letter) argList.append(letter)
# Start work on pipes.
for word in argList: for word in argList:
if word == "&&": if word == "&&":
print("This hasnt been implemented yet hahahahah") print("This hasnt been implemented yet hahahahah")
# Built in commands section
if argList[0] == "help": 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("")
print("pwd Print directory you are currently in")
print("cd Change your directory.") print("cd Change your directory.")
print("history See previously ran commands.")
print("mat View contents of a file. If you're on a Unix-like OS, use 'cat' instead.") print("mat View contents of a file. If you're on a Unix-like OS, use 'cat' instead.")
print("mls View all files in a directory. If you're on a Unix-like OS, use 'ls' instead.") print("mls View all files in a directory. If you're on a Unix-like OS, use 'ls' instead.")
print("version Print current version of Mash.") print("version Print current version of Mash.")
@ -91,17 +109,23 @@ try:
print(historyReadFile.read()) print(historyReadFile.read())
elif argList[0] == "mtime": elif argList[0] == "mtime":
print(datetime.now()) print(datetime.now())
# If a built in command isn't what you wanted, run a new subprocess.
else: else:
try: try:
subprocess.run(argList) 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.")
# Write to the history file
historyFile = open((expanduser("~") + "/.history.mash"), "a") historyFile = open((expanduser("~") + "/.history.mash"), "a")
historyFile.write(command) historyFile.write(command)
historyFile.write("\n") historyFile.write("\n")
historyFile.close() historyFile.close()
#except Exception:
# print("Looks like there was an error. Exiting...") # Handle errors in the main loop
except Exception:
print("Looks like there was an error. Exiting...")
# Handle ^C
except KeyboardInterrupt: except KeyboardInterrupt:
print(" detected! Exiting...") print(" detected! Exiting...")