]> gitweb @ CieloNegro.org - blackboard-dns.git/blobdiff - DDNS/Utils.hs
editzone
[blackboard-dns.git] / DDNS / Utils.hs
index 9c32824c0828722226b0c89e68ecb29984a0f0f1..e0ef4035ec85e7ef1688afe17f8f6d35fcad8007 100644 (file)
@@ -1,8 +1,16 @@
 module DDNS.Utils
     ( trim
+    , getInputLine'
+    , getInputLineWithDefault
+    , readInputLine'
+    , readInputLineWithDefault
     )
     where
 
+import           Control.Monad
+import           System.Console.Haskeline
+
+
 trim :: Maybe String -> Maybe String
 trim Nothing   = Nothing
 trim (Just xs) = case trimTail $ trimHead xs of
@@ -14,3 +22,28 @@ trim (Just xs) = case trimTail $ trimHead xs of
       trimHead ys       = ys
 
       trimTail = reverse . trimHead . reverse
+
+getInputLine' :: MonadException m => String -> InputT m String
+getInputLine' prompt
+    = do ret <- getInputLine prompt
+         case trim ret of
+           Just ret' -> return ret'
+           Nothing   -> fail "No input"
+
+getInputLineWithDefault :: MonadException m => String -> String -> InputT m String
+getInputLineWithDefault prompt defaultStr
+    = do ret <- getInputLine prompt
+         case trim ret of
+           Just ret' -> return ret'
+           Nothing   -> return defaultStr
+
+
+readInputLine' :: (MonadException m, Read r) => String -> InputT m r
+readInputLine' = liftM read . getInputLine'
+
+readInputLineWithDefault :: (MonadException m, Read r) => String -> r -> InputT m r
+readInputLineWithDefault prompt defaultValue
+    = do ret <- getInputLine prompt
+         case trim ret of
+           Just ret' -> return $ read ret'
+           Nothing   -> return defaultValue
\ No newline at end of file