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 "" -> Nothing ys -> Just ys where trimHead [] = [] trimHead (' ':ys) = trimHead ys 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