64 lines
1.5 KiB
Haskell
64 lines
1.5 KiB
Haskell
|
{-# LANGUAGE MultiWayIf #-}
|
||
|
|
||
|
import Data.List.Split (splitOn)
|
||
|
import System.Directory (setCurrentDirectory)
|
||
|
import System.Exit (exitSuccess)
|
||
|
import System.IO
|
||
|
import System.Process (callCommand)
|
||
|
|
||
|
prompt :: IO ()
|
||
|
prompt = do
|
||
|
input <- moshReadLine promptStringOne
|
||
|
moshExecute input
|
||
|
prompt
|
||
|
|
||
|
moshReadLine :: String -> IO String
|
||
|
moshReadLine ps1 = do
|
||
|
putStr ps1
|
||
|
hFlush stdout
|
||
|
getLine
|
||
|
|
||
|
moshExecute :: String -> IO ()
|
||
|
moshExecute input = do
|
||
|
if
|
||
|
| command == "exit" -> exitSuccess
|
||
|
| command == "help" -> moshHelp
|
||
|
| command == "cd" -> changeDirectory path
|
||
|
| otherwise -> callCommand input
|
||
|
where
|
||
|
command = head (splitOn " " input)
|
||
|
path = last (splitOn " " input)
|
||
|
|
||
|
changeDirectory :: FilePath -> IO ()
|
||
|
changeDirectory path = do
|
||
|
setCurrentDirectory path
|
||
|
|
||
|
moshHelp :: IO ()
|
||
|
moshHelp = do
|
||
|
putStrLn "---------------------"
|
||
|
putStrLn "MoSH -- Monadic Shell"
|
||
|
putStrLn "Author: sajenim"
|
||
|
putStrLn "---------------------"
|
||
|
putStrLn "Type program names and arguments, and hit enter."
|
||
|
putStrLn ""
|
||
|
putStrLn "The following are built in:"
|
||
|
putStr (unlines (prepend " " builtinCommands))
|
||
|
putStrLn ""
|
||
|
putStrLn "Use the man command for information on other programs"
|
||
|
|
||
|
builtinCommands :: [String]
|
||
|
builtinCommands = [ "cd"
|
||
|
, "help"
|
||
|
, "exit"
|
||
|
]
|
||
|
|
||
|
promptStringOne :: String
|
||
|
promptStringOne = "> "
|
||
|
|
||
|
prepend :: String -> [String] -> [String]
|
||
|
prepend s = map (s ++)
|
||
|
|
||
|
main :: IO ()
|
||
|
main = do
|
||
|
prompt
|