diff options
| -rw-r--r-- | Cmd.hs | 6 | ||||
| -rw-r--r-- | Main.hs | 57 |
2 files changed, 31 insertions, 32 deletions
@@ -7,4 +7,10 @@ data Cmd = Cmd String String (State -> IO ()) instance Show Cmd where show (Cmd a b _) = a ++ " - " ++ b +cmdGet :: String -> [Cmd] -> Maybe Cmd +cmdGet x commands = + let command = [ (Cmd a b c) | (Cmd a b c) <- commands, a == x ] + in if length command == 0 + then Nothing + else Just (head command) @@ -23,36 +23,29 @@ r = main dispatch :: Dispatch dispatch cmd sendMessage env@(Env state conf _) = - let (Cmd _ _ dispatchFunction) = getCmd ("!" ++ cmd) - in dispatchFunction state - where - getCmd x = - let command = [ (Cmd a b c) | (Cmd a b c) <- commands, a == x ] - in if length command == 0 - then getCmd "!i" - else head command - - commands = [ - Cmd "!h" "Prints help" printHelp, - Cmd "!i" "Prints infos" printInfos, - Cmd "!p" "Prints current state" printState, - Cmd "!s" "Stores current state to file" storeState, - Cmd "!q" "quits" quit - ] - - printHelp _ = printHelp' commands - where printHelp' = sendMessage . concat . showL - printInfos _ = do - sendMessage $ (envGet "name" env) - ++ " " ++ (envGet "version" env) - ++ " (try !h)" - printState = sendMessage . show - storeState state = do - sendMessage "Storing current state" - stateSave (envGet "databaseFile" env) state - quit state = do - sendMessage "Good bye" - stateSave (envGet "databaseFile" env) state - exitWith ExitSuccess - + let commands = [ + Cmd "!h" "Prints help" printHelp, + Cmd "!i" "Prints infos" printInfos, + Cmd "!p" "Prints current state" printState, + Cmd "!s" "Stores current state to file" storeState, + Cmd "!q" "quits" quit + ] + printHelp _ = printHelp' commands + where printHelp' = sendMessage . concat . showL + printInfos _ = do + sendMessage $ (envGet "name" env) + ++ " " ++ (envGet "version" env) + ++ " (try !h)" + printState = sendMessage . show + storeState state = do + sendMessage "Storing current state" + stateSave (envGet "databaseFile" env) state + quit state = do + sendMessage "Good bye" + stateSave (envGet "databaseFile" env) state + exitWith ExitSuccess + + in case cmdGet ("!" ++ cmd) commands of + Just (Cmd _ _ dispatchFunction) -> dispatchFunction state + Nothing -> dispatch "i" sendMessage env |
