Commits

Miki Tebeka committed af73402

ChangeLog

Comments (0)

Files changed (3)

+2011-01-10 version 0.1.1
+    * Added "walk"
+
-(defproject fs "0.1.0-SNAPSHOT"
+(defproject fs "0.1.1-SNAPSHOT"
   :description "File system utilities for clojure"
   :url "https://bitbucket.org/tebeka/fs/src"
   :dependencies [[org.clojure/clojure "1.2.0"]])
 (ns ^{:doc "File system utilities in Clojure"
       :author "Miki Tebeka <miki.tebeka@gmail.com>"}
   fs
+  (:require [clojure.zip :as zip])
   (:import java.io.File))
 
 (def separator File/separator)
 
+(defn- strinfify [file]
+  (.getCanonicalPath file))
+
 (defn listdir
   "List files under path."
   [path]
-  (seq (.list (File. path))))
+  (map strinfify (seq (.list (File. path)))))
 
 (defn executable?
   "Return true if path is executable."
 (defn normpath
   "Return nomralized (canonical) path."
   [path]
-  (.getCanonicalPath (File. path)))
+  (strinfify (File. path)))
 
 (defn basename
   "Return basename (file part) of path.\n\t(basename \"/a/b/c\") -> \"c\""
   []
   (abspath "."))
 
-; FIXME: ...
-;(defn dir-seq [root]
+; walk helper functions
+(defn- w-directory? [f]
+  (.isDirectory f))
+(defn- w-file? [f]
+  (.isFile f))
+(defn- w-children [f]
+  (.listFiles f))
+(defn- w-base [f]
+  (.getName f))
+
+(defn walk [path func]
+  "Walk over directory structure. Calls 'func' with [root dirs files]"
+  (loop [loc (zip/zipper w-directory? w-children nil (File. path))]
+    (when (not (zip/end? loc))
+      (let [file (zip/node loc)]
+        (if (w-file? file)
+          (recur (zip/next loc))
+          (let [kids (w-children file)
+                dirs (map w-base (filter w-directory? kids))
+                files (map w-base (filter w-file? kids))]
+            (func (strinfify file) dirs files)
+            (recur (zip/next loc))))))))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.