-import Text.GenPrint
-import iTasks => qualified >>=, >>|, forever, sequence, return
-
-derive class iTask RTree, ChoiceNode
-
-Start w = startEngine (selectFile "/opt/clean/lib/StdLib" () False) w
-
-selectFile :: FilePath d Bool -> Task [FilePath] | toPrompt d
-selectFile root p m = tbind
- (accWorldOSError (appFst (fmap numberTree) o recurseDirectory root ""))
- \tree->editSelection p m (SelectInTree (pure o toChoiceNode fp2cn) fromTree) tree []
-where
- numberTree :: ((RTree a) -> RTree (Int, a))
- numberTree = flip evalState zero o foldTree \a cs->
- (\lvs i->RNode (i, a) lvs) <$> sequence cs <*> getState <* modify ((+)one)
-
- toChoiceNode :: (Int a -> ChoiceNode) -> ((RTree (Int, a)) -> ChoiceNode)
- toChoiceNode tfun = foldTree \a cs->{ChoiceNode | uncurry tfun a & children=cs}
-
- fp2cn :: Int (FilePath, Bool) -> ChoiceNode
- fp2cn i (fp, bool) = {id =if bool (~i) i,label=dropDirectory fp,icon=Nothing,expanded=False,children=[]}
-
- fromTree :: (RTree (Int, (FilePath, Bool))) [Int] -> [FilePath]
- fromTree tree sel = [f\\(i, (f, _))<-leafs tree | isMember i sel]
-
-recurseDirectory :: !FilePath !FilePath !*World -> *(MaybeOSError (RTree (FilePath, Bool)), !*World)
-recurseDirectory acc fp w
- # fp = acc </> fp
- # (mfi, w) = getFileInfo fp w
- | isError mfi = (liftError mfi, w)
- # (Ok fi) = mfi
- | not fi.directory = (Ok $ RNode (fp, False) [], w)
- # (mcs, w) = readDirectory fp w
- | isError mfi = (liftError mcs, w)
- # (cs, w) = appFst sequence $ mapSt (recurseDirectory fp) (filter (\c->not (elem c [".", ".."])) (fromOk mcs)) w
- = (RNode (fp, True) <$> cs, w)