Last active
March 6, 2024 20:40
-
-
Save alpox/71a57de195285312e3253f655350b7f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bb | |
(ns c2 | |
(:require [babashka.fs :as fs])) | |
(def start (first *command-line-args*)) | |
(defn line-prefix [levels] | |
(apply str | |
(for [[global-level written] (map-indexed vector levels) | |
:let [global-level (inc global-level) | |
current-level? (= global-level (count levels))]] | |
(if (= 0 written) | |
(if current-level? | |
"└── " | |
" ") | |
(if current-level? | |
"├── " | |
"│ "))))) | |
(defn print-tree [dir] | |
(loop [[child & more] (.listFiles dir) | |
levels [(inc (count more))]] | |
(when-not (nil? child) | |
(let [level (- (count (fs/components child)) | |
(count (fs/components dir))) | |
levels (subvec levels 0 level) | |
levels (update levels (dec level) (fnil dec 1)) | |
children (.listFiles child) | |
prefix (line-prefix levels)] | |
(println (str prefix (-> child fs/absolutize fs/normalize))) | |
(recur (apply conj more children) | |
(conj levels (count children))))))) | |
(print-tree (fs/file start)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment