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 subprocess
import shutil
from datetime import datetime
from colorama import Fore
from os.path import expanduser
@ -19,6 +20,11 @@ else:
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"))
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)
try:
@ -35,6 +41,9 @@ try:
argList[argNumber] = argList[argNumber] + letter
except Exception:
argList.append(letter)
for word in argList:
if word == "&&":
print("This hasnt been implemented yet hahahahah")
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:")
@ -77,12 +86,22 @@ try:
currentDirectory = (currentDirectory + "/" + directoryForCD)
except Exception:
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:
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...")
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:
print(" detected! Exiting...")