Skip to content

Instantly share code, notes, and snippets.

@bobeff
Last active July 8, 2019 13:51
Show Gist options
  • Save bobeff/68dd15147813e163135b5c275163bd35 to your computer and use it in GitHub Desktop.
Save bobeff/68dd15147813e163135b5c275163bd35 to your computer and use it in GitHub Desktop.
import os, sets
proc matchYieldFilter(kind: PathComponent,
path: string,
yieldSet: set[PathComponent]): bool =
result = kind in yieldSet
proc matchFollowFilter(kind: PathComponent,
path: string,
followSet: set[PathComponent]): bool =
result = kind in followSet
type
YieldFilter* = concept filter
mixin matchYieldFilter
matchYieldFilter(PathComponent, string, filter)
FollowFilter* = concept filter
mixin matchFollowFilter
matchFollowFilter(PathComponent, string, filter)
iterator walkDirRec(dir: string,
yieldFilter: YieldFilter = {pcFile},
followFilter: FollowFilter = {pcDir},
relative = false): string {.tags: [ReadDirEffect].} =
mixin matchYieldFilter
mixin matchFollowFilter
var stack = @[""]
while stack.len > 0:
let d = stack.pop()
for k, p in walkDir(dir / d, relative = true):
let rel = d / p
if k in {pcDir, pcLinkToDir} and matchFollowFilter(k, rel, followFilter):
stack.add rel
if matchYieldFilter(k, rel, yieldFilter):
yield if relative: rel else: dir / rel
for file in walkDirRec("."):
echo file
proc matchFollowFilter(kind: PathComponent,
path: string,
skipDirs: HashSet[string]): bool =
result = not (path in skipDirs)
for file in walkDirRec(".", followFilter = toHashSet([".git", ".hg"])):
echo file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment