Add basic history support

This commit is contained in:
Maxwell 2024-07-05 09:26:10 +10:00
parent bad0c2f777
commit 929ddb73f3

23
main.py
View File

@ -3,6 +3,7 @@ import os
import socket import socket
import subprocess import subprocess
import shutil import shutil
from datetime import datetime
from colorama import Fore from colorama import Fore
from os.path import expanduser from os.path import expanduser
@ -19,6 +20,11 @@ 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"))
historyFile = open((expanduser("~") + "/.history.mash"), "a")
historyFile.write("\n")
historyFile.write(("Session on " + str(datetime.now())))
historyFile.write("\n")
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)
try: try:
@ -35,6 +41,9 @@ try:
argList[argNumber] = argList[argNumber] + letter argList[argNumber] = argList[argNumber] + letter
except Exception: except Exception:
argList.append(letter) argList.append(letter)
for word in argList:
if word == "&&":
print("This hasnt been implemented yet hahahahah")
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:")
@ -77,12 +86,22 @@ try:
currentDirectory = (currentDirectory + "/" + directoryForCD) currentDirectory = (currentDirectory + "/" + directoryForCD)
except Exception: except Exception:
print("Either a directory was not provided or an error occured.") print("Either a directory was not provided or an error occured.")
elif argList[0] == "history":
historyReadFile = open((expanduser("~") + "/.history.mash"))
print(historyReadFile.read())
elif argList[0] == "mtime":
print(datetime.now())
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.")
except Exception:
print("Looks like there was an error. Exiting...") historyFile = open((expanduser("~") + "/.history.mash"), "a")
historyFile.write(command)
historyFile.write("\n")
historyFile.close()
#except Exception:
# print("Looks like there was an error. Exiting...")
except KeyboardInterrupt: except KeyboardInterrupt:
print(" detected! Exiting...") print(" detected! Exiting...")