mash/main.py

79 lines
3.2 KiB
Python
Raw Normal View History

#!/usr/bin/python
2024-07-03 12:08:56 +10:00
import os
import socket
2024-07-03 12:08:56 +10:00
import subprocess
import shutil
from colorama import Fore
2024-07-04 09:49:07 +10:00
from os.path import expanduser
2024-07-03 12:08:56 +10:00
2024-07-04 09:49:07 +10:00
version = "0.1.0"
2024-07-03 13:31:27 +10:00
user = os.getlogin()
2024-07-04 09:49:07 +10:00
initDirectory = expanduser("~")
currentDirectory = initDirectory
2024-07-03 12:08:56 +10:00
argList = ["", ""]
if os.path.isfile(expanduser("~") + "/.config/mash/mash.conf") == True:
configFile = open(expanduser("~") + "/.config/mash/mash.conf")
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"))
2024-07-04 09:49:07 +10:00
print("Welcome to Mash, " + user + "@" + socket.gethostname() + "! Mash is currently on version " + version)
try:
while True:
2024-07-04 09:49:07 +10:00
os.chdir(currentDirectory)
command = input(Fore.GREEN + user + Fore.WHITE + "@" + socket.gethostname() + Fore.BLUE + " " + currentDirectory + Fore.WHITE + " > ")
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":
2024-07-03 14:46:40 +10:00
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("")
print("pwd Print directory you are currently in")
print("cd Change your directory.")
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.")
2024-07-03 14:46:40 +10:00
print("version Print current version of Mash.")
print("exit Exit Mash.")
print("")
print("You can also run commands installed by your system. See your OS's documentation for more information.")
elif argList[0] == "exit":
exit()
elif argList[0] == "version":
print(version)
elif argList[0] == "mat":
matFile = open(argList[1])
print(matFile.read())
elif argList[0] == "mls":
os.listdir(currentDirectory)
2024-07-04 09:49:07 +10:00
elif argList[0] == "cd":
try:
directoryForCD = argList[1]
if directoryForCD[0] == "~":
currentDirectory = expanduser("~") + directoryForCD.replace("~", "")
if directoryForCD[0] == "/":
currentDirectory = directoryForCD
else:
currentDirectory = (currentDirectory + "/" + directoryForCD)
except Exception:
print("Either a directory was not provided or an error occured.")
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...")
except KeyboardInterrupt:
print(" detected! Exiting...")