module Main ( main ) where import IOMaybe import Control.Exception ( handle, IOException ) import System.Directory import System.IO import Control.Monad import Control.Applicative import Data.Maybe import System.Environment ( getArgs ) import System.FilePath liftIOWHandle :: IO a -> IOMaybe a liftIOWHandle act = IOMaybe $ handle h (Just <$> act) where h :: IOException -> IO (Maybe a) h ex = do hPrint stderr (show ex) return Nothing withDefault :: a -> IOMaybe a -> IOMaybe a withDefault x act = IOMaybe $ do val <- runIOMaybe act return . Just $ fromMaybe x val main = void . runIOMaybe $ do [dir] <- liftIOWHandle getArgs cont <- liftIOWHandle $ getDirectoryContents dir forM_ cont $ \x -> withDefault () . liftIOWHandle $ do let path = dir x isDir <- doesDirectoryExist path isFile <- doesFileExist path when isFile $ do line <- withFile path ReadMode hGetLine putStrLn $ x ++ ": \t" ++ line when isDir . putStrLn $ x ++ ": \t"