Commit c20eebd2 authored by Jurrien Stutterheim's avatar Jurrien Stutterheim

Refactor doProjectPathAction

parent 8d8db4f8
......@@ -187,33 +187,18 @@ doProjectAction _ _ _ _ world =
, " path : manage project paths"] world
doProjectPathAction :: .String .String Project .PathAction *World -> .World
doProjectPathAction cleanhome pn project (AddPathAction path) world
# (ok, world) = saveProject cleanhome prj pn world
| not ok = abort "Failed to add path to project" // TODO: Improve
= showLines ["Path '" +++ path +++ "' successfully added to project"] world
where prj = PR_SetPaths False paths (path :! paths) project // TODO: Double check to see if PR_SetPaths is used correctly
paths = PR_GetPaths project
doProjectPathAction cleanhome pn project (AddPathAction path) world =
doModPaths cleanhome pn project (\paths -> path :! paths) world
doProjectPathAction cleanhome pn project (RemovePathAction i) world
# (ok, world) = saveProject cleanhome prj pn world
| not ok = abort "Failed to remove path from project" // TODO: Improve
= showLines ["Path " +++ toString i +++ " successfully removed from project"] world
where paths = PR_GetPaths project
paths` = rmStrictListIdx i paths
prj = PR_SetPaths False paths paths` project // TODO: Double check to see if PR_SetPaths is used correctly
doProjectPathAction cleanhome pn project (RemovePathAction i) world =
doModPaths cleanhome pn project (rmStrictListIdx i) world
doProjectPathAction _ _ project ListPathsAction world = showLines ["Paths for project:" : paths] world
where paths = map f (zip2 [0..] (StrictListToList (PR_GetPaths project)))
f (n, p) = " [" +++ toString n +++ "] " +++ p
// TODO: Refactor and combine with removepath and addpath
doProjectPathAction cleanhome pn project (MovePathAction i pdir) world
# (ok, world) = saveProject cleanhome prj pn world
| not ok = abort "Failed to move path" // TODO: Improve
= showLines ["Path " +++ toString i +++ " successfully moved"] world
where paths = PR_GetPaths project
paths` = moveStrictListIdx i pdir paths
prj = PR_SetPaths False paths paths` project // TODO: Double check to see if PR_SetPaths is used correctly
doProjectPathAction cleanhome pn project (MovePathAction i pdir) world =
doModPaths cleanhome pn project (moveStrictListIdx i pdir) world
doProjectPathAction _ _ _ _ world =
help "cpm project <projectname> path <action>"
......@@ -223,6 +208,15 @@ doProjectPathAction _ _ _ _ world =
, " remove <i> : remove path <i> from the list of projects"
, " move <i> <up|down> : move path <i> up or down one position" ] world
doModPaths :: String String Project .([!String!] -> [!String!]) *World -> .World
doModPaths cleanhome pn project f world
# (ok, world) = saveProject cleanhome prj pn world
| not ok = abort "Failed to modify project paths"
= showLines ["Successfully modified project paths"] world
where paths = PR_GetPaths project
prj = PR_SetPaths False paths (f paths) project
openProject :: !FilePath !FilePath !*World -> (!MaybeErrorString Project,!*World)
openProject cleanhome projectfile world
# ((prj, ok, err), world) = accFiles (ReadProjectFile projectfile cleanhome) world
......@@ -232,12 +226,12 @@ openProject cleanhome projectfile world
saveProject :: !FilePath !Project !FilePath !*World -> (Bool, !*World)
saveProject cleanhome prj projectfile world = accFiles (SaveProjectFile projectfile prj cleanhome) world
rmStrictListIdx :: !Int !(List String) -> List String
rmStrictListIdx :: Int u:[!.a!] -> v:[!.a!], [u <= v]
rmStrictListIdx 0 (_ :! t) = t
rmStrictListIdx n (h :! t) | n > 0 = h :! (rmStrictListIdx (n - 1) t)
rmStrictListIdx n _ = abort ("Index " +++ toString n +++ " out of bounds")
moveStrictListIdx :: !Int !PathDirection !(List String) -> List String
moveStrictListIdx :: .Int .PathDirection .[!a!] -> .[!a!]
moveStrictListIdx i dir xs
| i < 0 || i > (LLength xs - 1) = abort ("Index " +++ toString i +++ " out of bounds")
| otherwise = ListToStrictList (msl dir (splitAt i (StrictListToList xs)))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment