module Browse where import Directory -- shorter names for some imported utilities (getDir,isDir) = (getDirectoryContents,doesDirectoryExist) -------------------- -- a DTree type for picture files and directories -- (Show instance below gives outlines, or derive it if you prefer) data DTree = Pic FilePath | Dir FilePath [DTree] -- deriving Show dirtree fp = if (dotted fp) then return [] else if (isGIF fp) then return [Pic fp] else iff (isDir fp) (subtrees fp) (return []) subtrees fp = getDir fp >>= mapM (dirtree . path fp) >>= \ts -> return [Dir fp (concat ts)] -------------------- -- put a nested directory of pictures on your -- H drive and adjust this path to point to it test = dirtree path >>= print . head where path = "/Users/fruehr/hugs/pics" -------------------- -- some utility functions for readability -- for unix directories (and Windows?) with "dot" links dotted fp = take 1 (reverse fp) == "." -- expand this definition if you use JPEGs, PNGs, etc. isGIF fp = ".gif" == snd (break (=='.') fp) path p f = p ++ '/' : f iff p q r = p >>= (\b -> if b then q else r) -- outline-style show function for DTrees instance Show (DTree) where show = sh "" where sh i (Pic f) = i ++ f sh i (Dir d []) = i ++ d ++ " [D]" sh i (Dir d ds) = i ++ d ++ " [D] \n" ++ unline (map (sh ('\t':i)) ds) unline = foldr1 (\x y->x++'\n':y)