oswalkdir.nim 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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;
  21. path: string] =
  22. for k, v in items(staticWalkDir(dir, relative)):
  23. yield (k, v)
  24. iterator walkDirRec*(dir: string; filter = {pcFile, pcDir}): string =
  25. var stack = @[dir]
  26. while stack.len > 0:
  27. for k, p in walkDir(stack.pop()):
  28. if k in filter:
  29. case k
  30. of pcFile, pcLinkToFile: yield p
  31. of pcDir, pcLinkToDir: stack.add(p)