2024-07-15 13:06:07 +10:00
#!/usr/bin/python3
2024-07-05 09:42:14 +10:00
# 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.
2024-07-03 12:08:56 +10:00
import os
2024-07-03 19:24:12 +10:00
import socket
2024-07-03 12:08:56 +10:00
import subprocess
2024-07-04 13:08:32 +10:00
import shutil
2024-07-05 09:26:10 +10:00
from datetime import datetime
2024-07-04 09:49:07 +10:00
from os . path import expanduser
2024-07-03 12:08:56 +10:00
2024-07-05 09:42:14 +10:00
# Some initial variables
2024-07-15 10:03:44 +10:00
version = " 0.2.0 "
2024-07-03 13:31:27 +10:00
user = os . getlogin ( )
2024-07-04 09:49:07 +10:00
initDirectory = expanduser ( " ~ " )
2024-07-04 09:00:53 +10:00
argList = [ " " , " " ]
2024-07-05 09:42:14 +10:00
# Start work on checking whether a config file exists
2024-07-10 19:04:37 +10:00
if os . path . isfile ( expanduser ( " ~ " ) + " /.config/mash/mash.conf " ) == False :
2024-07-15 14:05:01 +10:00
configFilePath = " /etc/mash/mash.conf "
else :
configFilePath = ( expanduser ( " ~ " ) + " /.config/mash/mash.conf " )
2024-07-10 19:04:37 +10:00
# Parse config file, adding variables
2024-07-15 14:05:01 +10:00
configFileFile = open ( configFilePath )
2024-07-10 19:04:37 +10:00
configFile = configFileFile . read ( )
confArgNumber = 0
confArgList = [ " " , " " ]
for line in configFile :
for letter in line :
2024-07-11 16:55:08 +10:00
if letter == ' : ' :
confArgList [ confArgNumber ] = confArgList [ confArgNumber ] + " : "
2024-07-10 19:04:37 +10:00
confArgNumber = confArgNumber + 1
2024-07-11 09:05:38 +10:00
elif letter == ' , ' :
2024-07-15 17:03:15 +10:00
confArgList . append ( " " )
2024-07-11 09:05:38 +10:00
confArgNumber = confArgNumber + 1
2024-07-10 19:04:37 +10:00
else :
try :
confArgList [ confArgNumber ] = confArgList [ confArgNumber ] + letter
except Exception :
confArgList . append ( letter )
2024-07-11 09:05:38 +10:00
for word in confArgList :
if " \n " in word :
confArgList [ confArgList . index ( word ) ] = word . replace ( " \n " , " " )
2024-07-11 16:55:08 +10:00
for word in confArgList :
if word == " motd: " :
motd = confArgList [ confArgList . index ( word ) + 1 ]
2024-07-15 17:03:15 +10:00
if word == " mashPath: " :
2024-07-15 10:03:44 +10:00
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 )
2024-07-24 14:45:40 +10:00
if word == " colour: " :
colourConf = confArgList [ confArgList . index ( word ) + 1 ]
colourList = [ " " ]
colourListIndex = 0
for letter in colourConf :
if letter == " ; " :
colourListIndex = colourListIndex + 1
colourList . append ( " " )
else :
colourList [ colourListIndex ] = colourList [ colourListIndex ] + letter
colourCodes = [ ]
for word in colourList :
if word == ' white ' :
colourCodes . append ( ' \033 [97m ' )
elif word == ' blue ' :
colourCodes . append ( ' \033 [34m ' )
elif word == ' green ' :
colourCodes . append ( ' \033 [32m ' )
elif word == ' yellow ' :
colourCodes . append ( ' \033 [33m ' )
2024-07-05 09:42:14 +10:00
# Write the time to the history file
2024-07-05 09:26:10 +10:00
historyFile = open ( ( expanduser ( " ~ " ) + " /.history.mash " ) , " a " )
historyFile . write ( " \n " )
historyFile . write ( ( " Session on " + str ( datetime . now ( ) ) ) )
historyFile . write ( " \n " )
2024-07-11 16:55:08 +10:00
# Welcome message. Configurable with "motd: (your motd here),"
print ( motd )
2024-07-15 10:03:44 +10:00
os . chdir ( initDirectory )
currentDirectory = initDirectory
2024-07-04 09:49:07 +10:00
2024-07-05 09:42:14 +10:00
# Main loop.
2024-07-15 10:03:44 +10:00
def mainLoop ( startDir ) :
try :
currentDirectory = startDir
while True :
# Set directory and prompt for input.
os . chdir ( currentDirectory )
2024-07-24 14:45:40 +10:00
command = input ( colourCodes [ 0 ] + user + colourCodes [ 1 ] + " @ " + colourCodes [ 2 ] + socket . gethostname ( ) + colourCodes [ 3 ] + " " + currentDirectory + colourCodes [ 4 ] + " > " )
2024-07-15 14:05:01 +10:00
if command == " " :
mainLoop ( currentDirectory )
2024-07-15 10:03:44 +10:00
# Clean some things up for the argument parser.
argNumber = 0
argList . clear ( )
2024-07-05 09:42:14 +10:00
2024-07-15 10:03:44 +10:00
# Argument parser
for letter in command :
if letter == " " :
argNumber = argNumber + 1
else :
try :
argList [ argNumber ] = argList [ argNumber ] + letter
except Exception :
argList . append ( letter )
2024-07-05 09:42:14 +10:00
2024-07-15 10:03:44 +10:00
# Start work on pipes.
for word in argList :
if word == " && " :
print ( " This hasnt been implemented yet hahahahah " )
elif word == " & " :
print ( " This hasnt been implemented yet heheheheh " )
elif word == " | " :
print ( " What are you? A pipe master? We dont like those people here " )
2024-07-05 09:42:14 +10:00
2024-07-15 10:03:44 +10:00
# Built in commands section
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: " )
print ( " " )
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 ( " 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 ( " 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 " :
try :
if argList [ 1 ] == " " :
print ( os . listdir ( currentDirectory ) )
else :
argList [ 1 ] = mlsDirectory
if mlsDirectory [ 0 ] == " / " :
print ( os . listdir ( mlsDirectory ) )
else :
print ( os . listdir ( currentDirectory + " / " + mlsDirectory ) )
except Exception :
2024-07-04 17:20:49 +10:00
print ( os . listdir ( currentDirectory ) )
2024-07-15 10:03:44 +10:00
elif argList [ 0 ] == " cd " :
try :
directoryForCD = argList [ 1 ]
if directoryForCD [ 0 ] == " ~ " :
currentDirectory = expanduser ( " ~ " ) + directoryForCD . replace ( " ~ " , " " )
if directoryForCD [ 0 ] == " / " :
currentDirectory = directoryForCD
2024-07-04 17:20:49 +10:00
else :
2024-07-15 10:03:44 +10:00
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 ( ) )
elif argList [ 0 ] == " printconf " :
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 :
print ( " Command " , command , " either doesn ' t exist, isn ' t in the path or isn ' t in this directory. " )
2024-07-05 09:26:10 +10:00
2024-07-15 10:03:44 +10:00
# Write to the history file
historyFile = open ( ( expanduser ( " ~ " ) + " /.history.mash " ) , " a " )
historyFile . write ( command )
historyFile . write ( " \n " )
historyFile . close ( )
2024-07-05 09:42:14 +10:00
2024-07-15 10:03:44 +10:00
# Handle errors in the main loop
#except Exception:
#print("Looks like there was an error. Exiting...")
2024-07-05 09:42:14 +10:00
# Handle ^C
2024-07-15 10:03:44 +10:00
except KeyboardInterrupt :
print ( " \n " )
mainLoop ( currentDirectory )
2024-07-24 14:45:40 +10:00
mainLoop ( initDirectory )