Simple Haskell REPL For "Doing Haskell: Part Two: Writing a REPL to do nothing!"

-- | A simple Read-Eval-Print Loo (REPL) to demonstrate some of the                                  
-- features of the Haskell language.                                                                 

module Main where


import System.Exit  (exitSuccess)
import System.IO    (hPutStr, hFlush, hGetLine, stdin, stdout)
import Data.Text    (pack, unpack, toLower, toUpper)


-- |This is our command prompt for the REPL loop                                                     
prompt :: String
prompt = "OBJITSU> ";



-- |"main" is the entry point for our program                                                        
main :: IO ()
main = do
  hPutStr stdout prompt
  hFlush stdout
  cmd <- hGetLine stdin
  let parts = words cmd
  case (length parts) of
    0 -> main
    _ -> execCmd parts >> main



-- |This function is called once the REPL has some input from the user. It will                      
-- decide what to do based upon the value of the first word of input. Should                         
-- no input be given at all it just outputs the values for inspection.                               
execCmd :: [String] -> IO ()
execCmd (cmd:args) = do
  case unpack (toLower $ pack cmd) of
    "quit" -> exitSuccess
    "help" -> showHelp
    _      -> putStrLn $ "DO " ++ cmd ++ " with args: " ++ (unwords args)



-- |Displays some simple help text for the REPL loop                                                 
showHelp :: IO ()
showHelp = do
  mapM_ putStrLn
    [ "\nAvailable commands :-"
    , "quit -- quit the REPL"
    , "help -- show this help page"
    , "\n"
    ]