.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
--Tree.hs

module Main (main) where

import Control.Monad
import Data.List        -- isPrefix, sort
import System.Directory --getDirectoryContents, doesDirectoryExist.
import System.Environment


main = do
    args <- getArgs
    let path = args !! 0
    --putStrLn $ path 
    allEntries <- getDirectoryContents path
    let rippedEntries = allEntries \\ [".", ".."] --Get rid of them
    let entries = [path ++ "/"++ ent | ent <- rippedEntries]
    mapM_ (flip viewEntry 1) entries --1 starting value.  
    
getDepthString:: Int -> String
getDepthString depth = show $ concat $ replicate depth " "

--viewDir ::String -> IO ()
viewEntry entry depth= do
    --is entry a dir ?
    isDir <- doesDirectoryExist entry
    if isDir
        then viewDir entry $ depth +1 
        else viewFile entry $ depth +1

viewDir dir depth=
    do putStrLn $ (getDepthString depth) ++ "|" ++ dir ++ " DIR"
       allEntries <- getDirectoryContents dir
       let rippedEntries = allEntries \\ [".", ".."] --Get rid of them
       let entries = [dir ++ "/"++ ent | ent <- rippedEntries]
       mapM_ (flip viewEntry 1) entries
       
viewFile file depth=
    do putStrLn $  (getDepthString depth) ++ "--" ++ file ++ " FILE"