Skip to content

Instantly share code, notes, and snippets.

@ykst
Created June 23, 2012 11:14
Show Gist options
  • Save ykst/2977922 to your computer and use it in GitHub Desktop.
Save ykst/2977922 to your computer and use it in GitHub Desktop.
lstree implementation by Haskell
module Main where
import Directory (getDirectoryContents)
import Data.Tree (drawTree, unfoldTreeM)
import System.Environment (getArgs)
import Control.Applicative ((<$>), (<*>), pure)
import System.Posix.Files (isSymbolicLink, isDirectory,
readSymbolicLink,getSymbolicLinkStatus)
import System.FilePath.Posix ((</>), takeFileName)
main = getArgs >>= \args -> case args of
[arg] -> putStrLn . drawTree =<< unfoldTreeM phi arg
_ -> putStrLn "USAGE: lstree <dirpath>"
where phi path = proc =<< getSymbolicLinkStatus path
where proc fstat
| isSymbolicLink fstat =
readSymbolicLink path >>= \lname ->
pure ("@" ++ takeFileName path ++ " -> " ++ lname,[])
| isDirectory fstat =
getDirectoryContents path >>= \children ->
(,) <$> pure (takeFileName path)
<*> pure [path </> child | child <- children, head child /= '.']
| otherwise = pure (takeFileName path,[])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment