Last active
May 8, 2017 21:45
-
-
Save crdrost/000efeb73b77a728c9cde0f4bc86748f to your computer and use it in GitHub Desktop.
dirtree - shows you where you are in a filesystem tree and some of what's around
This file contains 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 node | |
/* Copyright 2017 CR Drost. | |
* | |
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of | |
* the MPL was not distributed with this file, uou can obtain one at https://mozilla.org/MPL/2.0/. | |
*/ | |
const fs = require('fs'), | |
bold = text => '\u001b[1m' + text + '\u001b[22m', | |
red = text => '\u001b[31m' + text + '\u001b[39m', | |
id = text => text; | |
let data = process | |
.cwd() | |
.split('/') | |
.slice(1) | |
.reverse() | |
.map((x, i) => { | |
let dir = [...Array(i + 1)].map(_ => '..').join('/'), ls; | |
try { | |
ls = fs.readdirSync(dir).filter(name => name[0] != '.').filter(s => { | |
try { | |
return fs.statSync(dir + '/' + s).isDirectory(); | |
} catch (yolo) { | |
return false; | |
} | |
}); | |
return { before: ls.filter(q => q < x), curr: x, after: ls.filter(q => q > x), err: false }; | |
} catch (e) { | |
return { before: [], curr: x, after: [], err: true }; | |
} | |
}) | |
.reduce( | |
({ err, acc }, x) => ({ | |
err: x.err, | |
acc: (x.before.length ? x.before.join('\n') + '\n' : '') + | |
(err ? red : id)(bold(x.curr)) + | |
(acc === '' ? '' : '\n ' + acc.replace(/\n/g, '\n ')) + | |
(x.after.length ? '\n' + x.after.join('\n') : '') | |
}), | |
{ err: false, acc: '' } | |
); | |
console.log((data.err ? red : id)(bold('/')) + '\n ' + data.acc.replace(/\n/g, '\n ')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment