Skip to content

Instantly share code, notes, and snippets.

@plexus
Created April 18, 2017 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plexus/484c003eaae3c2bd4e7753c64a6ab91e to your computer and use it in GitHub Desktop.
Save plexus/484c003eaae3c2bd4e7753c64a6ab91e to your computer and use it in GitHub Desktop.
(ns birch.core
(:require [cljs.nodejs :as nodejs]
[clojure.string :as str]))
(nodejs/enable-util-print!)
(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))
(def cli-color (js/require "cli-color"))
(def blue-text (.-blue cli-color))
(defn directory? [f]
(.isDirectory (stat f)))
(def I-branch "│ ")
(def T-branch "├── ")
(def L-branch "└── ")
(def SPACER " ")
(declare tree-entry)
(defn child-entries [path]
(let [files (read-dir path)]
(map #(tree-entry path %1) files)))
(defn tree-entry [parent name]
(let [path (path-join parent name)]
{:name name
:directory? (directory? path)
:children (if (directory? path) (child-entries path))}))
(defn render-tree [{:keys [name children directory?]}]
(cons
(if directory?
(blue-text name)
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 -main [dir]
(->> (tree-entry "" dir)
render-tree
(str/join "\n")
print))
(set! *main-cli-fn* -main)
(.install (js/require "source-map-support"))
{
"name": "command_line_tools_with_lumo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cli-color": "^1.2.0"
},
"bin": {
"birch": "birch-compiled.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment