Add path support in config.mash

This commit is contained in:
Maxwell 2024-07-15 10:03:44 +10:00
parent 0703cfc3dc
commit 745b3ae02f

41
main.py
View File

@ -13,16 +13,14 @@ from os.path import expanduser
# Some initial variables
version = "0.1.0"
version = "0.2.0"
user = os.getlogin()
initDirectory = expanduser("~")
currentDirectory = initDirectory
argList = ["", ""]
# Start work on checking whether a config file exists
if os.path.isfile(expanduser("~") + "/.config/mash/mash.conf") == False:
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"))
print("Config file does not exist. Make a file at .config/mash/mash.conf to configure things")
# Parse config file, adding variables
configFileFile = open((expanduser("~") + "/.config/mash/mash.conf"))
@ -50,7 +48,19 @@ for word in confArgList:
for word in confArgList:
if word == "motd:":
motd = confArgList[confArgList.index(word) + 1]
if word == ",mashPath:":
mashPathStr = (confArgList[confArgList.index(word) + 1])
pathStrIndex = 0
mashPath = [""]
for letter in mashPathStr:
if letter == ";":
pathStrIndex = pathStrIndex + 1
mashPath.append("")
else:
try:
mashPath[pathStrIndex] = mashPath[pathStrIndex] + letter
except Exception:
mashPath.append(letter)
# Write the time to the history file
historyFile = open((expanduser("~") + "/.history.mash"), "a")
historyFile.write("\n")
@ -59,9 +69,13 @@ historyFile.write("\n")
# Welcome message. Configurable with "motd: (your motd here),"
print(motd)
os.chdir(initDirectory)
currentDirectory = initDirectory
# Main loop.
try:
def mainLoop(startDir):
try:
currentDirectory = startDir
while True:
# Set directory and prompt for input.
os.chdir(currentDirectory)
@ -142,6 +156,9 @@ try:
print(configFile)
# If a built in command isn't what you wanted, run a new subprocess.
else:
for word in mashPath:
if os.path.isfile(word + "/" + argList[0]):
argList[0] = (word + "/" + argList[0])
try:
subprocess.run(argList)
except Exception:
@ -153,9 +170,11 @@ try:
historyFile.write("\n")
historyFile.close()
# Handle errors in the main loop
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:
print(" detected! Exiting...")
except KeyboardInterrupt:
print("\n")
mainLoop(currentDirectory)
mainLoop(initDirectory)