summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2015-09-28 16:41:36 +0200
committerAraq <rumpf_a@web.de>2015-09-29 19:30:44 +0200
commitc852143f3a45ed42f03d4d70225c92079bd475ed (patch)
treead3b6868d2a4ff4bd31c76d7fd913b913c668ccc
parentab6f8f6e5b8aa365d7725d6866904c3fa2e0e553 (diff)
downloadNim-c852143f3a45ed42f03d4d70225c92079bd475ed.tar.gz
os.walkDir supports yielding relative paths
-rw-r--r--lib/pure/os.nim12
1 files changed, 8 insertions, 4 deletions
diff --git a/lib/pure/os.nim b/lib/pure/os.nim
index f413371cb..c01228563 100644
--- a/lib/pure/os.nim
+++ b/lib/pure/os.nim
@@ -810,11 +810,12 @@ type
 
 {.deprecated: [TPathComponent: PathComponent].}
 
-iterator walkDir*(dir: string): tuple[kind: PathComponent, path: string] {.
+iterator walkDir*(dir: string; relative=false): tuple[kind: PathComponent, path: string] {.
   tags: [ReadDirEffect].} =
   ## walks over the directory `dir` and yields for each directory or file in
   ## `dir`. The component type and full path for each item is returned.
-  ## Walking is not recursive.
+  ## Walking is not recursive. If ``relative`` is true the resulting path is
+  ## shortened to be relative to ``dir``.
   ## Example: This directory structure::
   ##   dirA / dirB / fileB1.txt
   ##        / dirC
@@ -843,7 +844,9 @@ iterator walkDir*(dir: string): tuple[kind: PathComponent, path: string] {.
             k = pcDir
           if (f.dwFileAttributes and FILE_ATTRIBUTE_REPARSE_POINT) != 0'i32:
             k = succ(k)
-          yield (k, dir / extractFilename(getFilename(f)))
+          let xx = if relative: extractFilename(getFilename(f))
+                   else: dir / extractFilename(getFilename(f))
+          yield (k, xx)
         if findNextFile(h, f) == 0'i32: break
       findClose(h)
   else:
@@ -855,7 +858,8 @@ iterator walkDir*(dir: string): tuple[kind: PathComponent, path: string] {.
         var y = $x.d_name
         if y != "." and y != "..":
           var s: Stat
-          y = dir / y
+          if not relative:
+            y = dir / y
           var k = pcFile
 
           when defined(linux) or defined(macosx) or defined(bsd):