]> gitweb @ CieloNegro.org - blackboard-dns.git/blob - DDNS/Utils.hs
editzone
[blackboard-dns.git] / DDNS / Utils.hs
1 module DDNS.Utils
2     ( trim
3     , getInputLine'
4     , getInputLineWithDefault
5     , readInputLine'
6     , readInputLineWithDefault
7     )
8     where
9
10 import           Control.Monad
11 import           System.Console.Haskeline
12
13
14 trim :: Maybe String -> Maybe String
15 trim Nothing   = Nothing
16 trim (Just xs) = case trimTail $ trimHead xs of
17                    "" -> Nothing
18                    ys -> Just ys
19     where
20       trimHead []       = []
21       trimHead (' ':ys) = trimHead ys
22       trimHead ys       = ys
23
24       trimTail = reverse . trimHead . reverse
25
26 getInputLine' :: MonadException m => String -> InputT m String
27 getInputLine' prompt
28     = do ret <- getInputLine prompt
29          case trim ret of
30            Just ret' -> return ret'
31            Nothing   -> fail "No input"
32
33 getInputLineWithDefault :: MonadException m => String -> String -> InputT m String
34 getInputLineWithDefault prompt defaultStr
35     = do ret <- getInputLine prompt
36          case trim ret of
37            Just ret' -> return ret'
38            Nothing   -> return defaultStr
39
40
41 readInputLine' :: (MonadException m, Read r) => String -> InputT m r
42 readInputLine' = liftM read . getInputLine'
43
44 readInputLineWithDefault :: (MonadException m, Read r) => String -> r -> InputT m r
45 readInputLineWithDefault prompt defaultValue
46     = do ret <- getInputLine prompt
47          case trim ret of
48            Just ret' -> return $ read ret'
49            Nothing   -> return defaultValue