3 import Control.Monad.State
4 import Control.Monad.Identity
5 import Control.Applicative
7 import qualified Control.Monad as M
8 from Control.Monad import `b`, mapM, class Monad(bind)
14 import System.Directory
19 import iTasks.Internal.SDS => qualified modify
20 import iTasks.Internal.Task
22 import StdDebug, StdMisc
24 derive class iTask RTree, FileInfo, Tm
26 Start w = startEngine (selecter "/home/mrl/projects" 2) w
28 recurseDirectory :: Int FilePath FilePath *World -> *(MaybeOSError (RTree (FilePath, Bool)), *World)
29 recurseDirectory maxdepth acc fp w
31 = case getFileInfo fp w of
32 (Error (i, e), w) = (Error (i, e +++ " in fileinfo " +++ fp), w)
34 | not fi.directory = (Ok $ RNode (fp, False) [], w)
35 = case readDirectory fp w of
36 (Error (i, e), w) = (Error (i, e +++ " in readDir " +++ fp), w)
38 | maxdepth == 0 = (Ok $ RNode (fp, True) [], w)
39 = appFst ((fmap $ RNode (fp, True)) o 'M'.sequence)
40 $ mapSt (recurseDirectory (maxdepth - 1) fp) (filter real cs) w
42 real i = IF_POSIX (i <> ".." && i <> ".") True
44 selecter :: !FilePath Int -> Task String
45 selecter fp maxdepth = accWorldOSError (recurseDirectory maxdepth fp "")
46 >>= \ds->editSelection "Examples" False (SelectInTree (\x->[evalState (toTree x) 0]) fromTree) ds []
49 toTree :: (RTree (FilePath, Bool)) -> State Int ChoiceNode
50 toTree (RNode (fp, _) forest)
51 = getState `b` \i->put (i+1) `b` \_->mapM toTree forest `b` \cs->pure
52 {id=i,label=bn fp,icon=Nothing,expanded=i==0,children=cs}
54 bn :: FilePath -> FilePath
55 bn x = if (endsWith {pathSeparator} x) (bn $ fst $ splitFileName x) (dropDirectory x)
57 fromTree :: (RTree (FilePath, Bool)) [Int] -> [FilePath]
58 fromTree t [i] = map fst $ filter (not o snd) [foldTree (\f fs->[f:flatten fs]) t !! i]