1 {-# LANGUAGE TupleSections #-}
4 ( textPrompt, yornPrompt, dependantPrompt
8 import Data.Char (toLower)
9 import Control.Monad (join)
11 isEmpty :: String -> Bool
14 cannotBeEmpty :: String -> Either String String
15 cannotBeEmpty xs = case isEmpty xs of
16 True -> Left "Value cannot be empty"
19 prompt :: String -> (String -> Either String a) -> IO a
20 prompt xs validate = do
23 case validate response of
24 Left error -> putStrLn error >> prompt xs validate
25 Right result -> return result
27 subPrompt :: String -> (String -> Bool) -> IO a -> IO (Maybe (String, a))
28 subPrompt xs showNext p = do
29 let v x = case showNext x of
30 True -> Right $ p >>= return . Just . (x ,)
31 False -> Right $ return Nothing
35 textPrompt :: String -> IO String
36 textPrompt xs = prompt xs cannotBeEmpty
38 dependantPrompt :: String -> String -> IO (Maybe (String,String))
39 dependantPrompt xs1 xs2 = subPrompt xs1 (not . isEmpty) $ prompt xs2 cannotBeEmpty
41 yornPrompt :: String -> IO Bool
43 let v x = case (map toLower x) of
46 _ -> Left "Please answer with y or n"
47 prompt (xs ++ " (y/n):") v