/
birch
executable file
·51 lines (41 loc) · 1.32 KB
/
birch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env lumo
;; -*- mode: clojurescript -*-
;; vim: ft=clojure:
(require '[lumo.core :refer [*command-line-args*]]
'[clojure.string :as str])
(def node-fs (js/require "fs"))
(def node-path (js/require "path"))
(def read-dir (.-readdirSync node-fs))
(def stat (.-statSync node-fs))
(def path-join (.-join node-path))
(defn directory? [f]
(.isDirectory (stat f)))
(def I-branch "│ ")
(def T-branch "├── ")
(def L-branch "└── ")
(def SPACER " ")
(declare tree-entry)
(defn child-entries [path]
(map #(tree-entry path %1) (read-dir path)))
(defn tree-entry [parent name]
(let [path (path-join parent name)]
{:name name
:children (if (directory? path) (child-entries path))}))
(defn render-tree [{:keys [name children]}]
(cons
name
(mapcat (fn [child index]
(let [subtree (render-tree child)
last? (= index (dec (count children)))
prefix-first (if last? L-branch T-branch)
prefix-rest (if last? SPACER I-branch)]
(cons (str prefix-first (first subtree))
(map #(str prefix-rest %) (next subtree)))))
children
(range))))
(defn birch [dir]
(->> (tree-entry "" dir)
render-tree
(str/join "\n")
print))
(birch (first *command-line-args*))