Last active
January 1, 2016 18:59
-
-
Save Varriount/8187182 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# | |
# osPlus.nim | |
# | |
# Enhancements and additional procedures for os.nim | |
# | |
import os | |
const | |
DirLike = {pkDir, pkLinkToDir} | |
FileLike = {pkFile, pkLinkToFile} | |
type | |
PathPair = tuple[k: PathKind, p: string] | |
# Possible additions | |
# - Add recursion depth argument | |
iterator walkDirRecEx*(dir: string, follow=dirLike, give=fileLike): PathPair {. | |
tags: [FReadDir].} = | |
## Recursively traverses directory `dir`. | |
## Follows the kinds of paths specified by `followFilter` and yields | |
## the kinds specified by `yieldFilter` | |
## | |
## Warning - Modifying the directory structure while the iterator | |
## is traversing may result in undefined behavior! | |
## | |
## Walking is recursive. `followFilter` and `yieldFilter` control | |
## the behaviour of the iterator: | |
## | |
## --------------------- --------------------------------------------- | |
## filter meaning | |
## --------------------- --------------------------------------------- | |
## ``pcFile`` yield real files | |
## ``pcLinkToFile`` yield symbolic links to files | |
## ``pcDir`` follow/yield real directories | |
## ``pcLinkToDir`` follow/yield symbolic links to directories | |
## --------------------- --------------------------------------------- | |
## | |
assert((fileLike * followFilter) == {}) | |
var stack = @[dir] | |
while stack.len > 0: | |
for kind, path in walkDir(stack.pop()): | |
if kind in followFilter: | |
stack.add(path) | |
if kind in yieldFilter: | |
yield (kind, path) | |
when defined(isMainModule): | |
for kind, path in walkDirRecEx(getCurrentDir(), yieldFilter=fileLike+dirLike): | |
echo(path & ": " & $kind) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment