## TODO 1: Implement simple  -- ie, see '##file' in print.tableOfBackups()  (jesus.r)
## TODO 2: Check for gitData


cleanFileStringOutput <- function(filestring, alignright=TRUE, minPad=nchar(projName), simple=FALSE, projName=getProjName(warn=FALSE)) {
  utilsDir <- setNames(nm="UTILS", getOption("utilsDir", "jljsdklfjsdkljfkldsjfldlksdjfs"))
  utilsDir.sub <- as.data.table(file.info(dir(utilsDir, include.dirs=TRUE, full.names=TRUE)), keep.rownames=TRUE)[(isdir), rn]
  setattr(utilsDir.sub, "names", basename(utilsDir.sub))
  utilsDir.sub <- utilsDir.sub[names(utilsDir.sub) %ni% getOption("ignore.subfolders")]

  dirs_to_replace <- c(
      utilsDir
    , setNames(nm="UTILS", paste0(utilsDir, "utilsRS.r"))
    , utilsDir.sub
    , if (exists("srcDir"))     setNames(nm=projName, srcDir)
    , if (exists("ingestDir"))  setNames(nm=paste0("ingest @ ", projName),  ingestDir)
    , if (exists("outDir"))     setNames(nm=paste0(   "out @ ", projName),  outDir)
    , if (exists("datDir"))     setNames(nm=paste0(   "data @ ", projName), dataDir)
    , setNames(nm = "ORCH", "~rsaporta/git/orch")
    , setNames(nm = "src @ ORCH", "~rsaporta/git/orch/src")
    , setNames(nm = "ingest @ ORCH", "~rsaporta/git/orch/ingest")
    # , setNames(nm = "~", "~rsaporta")
  )

  ## Clean by adding trailing "/"
  dirs_to_replace[!grepl("/$", dirs_to_replace)] <- paste0(dirs_to_replace[!grepl("/$", dirs_to_replace)], "/")

  ## Sort in reverse order
  dirs_to_replace <- dirs_to_replace[rev(order(path.expand(dirs_to_replace)))]

  ## add utilsRS
  dirs_to_replace <- c(setNames(nm="utilsRS", paste0(dirname(utilsDir), "/utilsRS.r")), dirs_to_replace)

  ## remove double slashes
  dirs_to_replace <- gsub("//", "/", dirs_to_replace)

  fstr.pe <- path.expand(filestring)
  pat <- path.expand(dirs_to_replace)
  rep <- sprintf("[%s] ", names(dirs_to_replace))

  if (alignright) {
    ## find the largest. 
    mx <- max(minPad,  mnchar( names(dirs_to_replace) [sapply(pat, function(x) any(grepl(x, fstr.pe)))] ))
    ## Add 3 for the '[] '
    mx <- mx + 3
    rep <- paste0(pasteR(" ", n=mx - nchar(rep)), rep)
  }

  ## execute sub
  ret <- msub(pattern = pat, replacement = rep, x=fstr.pe)

  ## Any unchanged ones, put back the original filestring since we had caled path.unexpand
  ## Also make sure to put the names back
  unchanged <- ret == fstr.pe
  ret[unchanged] <- filestring[unchanged]
  setattr(ret, "names", names(filestring))

  return(ret)
}
