oswalkdir.nim 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Compile-time only version for walkDir if you need it at compile-time
  10. ## for JavaScript.
  11. type
  12. PathComponent* = enum ## Enumeration specifying a path component.
  13. pcFile, ## path refers to a file
  14. pcLinkToFile, ## path refers to a symbolic link to a file
  15. pcDir, ## path refers to a directory
  16. pcLinkToDir ## path refers to a symbolic link to a directory
  17. proc staticWalkDir(dir: string; relative: bool): seq[
  18. tuple[kind: PathComponent, path: string]] =
  19. discard
  20. iterator walkDir*(dir: string; relative=false): tuple[kind: PathComponent, path: string] =
  21. for k, v in items(staticWalkDir(dir, relative)):
  22. yield (k, v)
  23. iterator walkDirRec*(dir: string, filter={pcFile, pcDir}): string =
  24. var stack = @[dir]
  25. while stack.len > 0:
  26. for k,p in walkDir(stack.pop()):
  27. if k in filter:
  28. case k
  29. of pcFile, pcLinkToFile: yield p
  30. of pcDir, pcLinkToDir: stack.add(p)