mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 22:10:33 +00:00
Merge pull request #9803 from GULPF/walkdirrec-relative
Add `relative` parameter to walkDirRec
This commit is contained in:
@@ -1564,11 +1564,14 @@ iterator walkDir*(dir: string; relative=false): tuple[kind: PathComponent, path:
|
||||
k = getSymlinkFileKind(y)
|
||||
yield (k, y)
|
||||
|
||||
iterator walkDirRec*(dir: string, yieldFilter = {pcFile},
|
||||
followFilter = {pcDir}): string {.tags: [ReadDirEffect].} =
|
||||
iterator walkDirRec*(dir: string,
|
||||
yieldFilter = {pcFile}, followFilter = {pcDir},
|
||||
relative = false): string {.tags: [ReadDirEffect].} =
|
||||
## Recursively walks over the directory `dir` and yields for each file
|
||||
## or directory in `dir`.
|
||||
## The full path for each file or directory is returned.
|
||||
## If ``relative`` is true the resulting path is
|
||||
## shortened to be relative to ``dir``, otherwise the full path is returned.
|
||||
##
|
||||
## **Warning**:
|
||||
## Modifying the directory structure while the iterator
|
||||
## is traversing may result in undefined behavior!
|
||||
@@ -1591,13 +1594,15 @@ iterator walkDirRec*(dir: string, yieldFilter = {pcFile},
|
||||
## ``pcLinkToDir`` follow symbolic links to directories
|
||||
## --------------------- ---------------------------------------------
|
||||
##
|
||||
var stack = @[dir]
|
||||
var stack = @[""]
|
||||
while stack.len > 0:
|
||||
for k, p in walkDir(stack.pop()):
|
||||
let d = stack.pop()
|
||||
for k, p in walkDir(dir / d, relative = true):
|
||||
let rel = d / p
|
||||
if k in {pcDir, pcLinkToDir} and k in followFilter:
|
||||
stack.add(p)
|
||||
stack.add rel
|
||||
if k in yieldFilter:
|
||||
yield p
|
||||
yield if relative: rel else: dir / rel
|
||||
|
||||
proc rawRemoveDir(dir: string) {.noNimScript.} =
|
||||
when defined(windows):
|
||||
|
||||
Reference in New Issue
Block a user