Last active
June 16, 2018 02:32
-
-
Save MrFant/e14285d40e908c94ebcf4927094a57bb to your computer and use it in GitHub Desktop.
command-line fileBrowser base on nodeJS
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
/** | |
* nodejs的标准输入输出流实现命令行程序,实现文件的读取和显示,类似于*nix系统里的ls,cat,cd等命令 | |
*Module dependencies | |
*/ | |
// "use strict"; | |
var fs = require('fs'), | |
stdin = process.stdin, | |
stdout = process.stdout, | |
path = require('path'), | |
workDir=process.cwd(); | |
function async(err,files) { | |
console.log(''); | |
if (!files.length) { | |
return console.log(" \033[31m No files to show ! \033[39m\n"); | |
} | |
console.log(' Select which file or directory you want to see\n'); | |
let stats=[]; | |
function file(i) { | |
let filename = files[i]; | |
fs.stat(path.join(workDir,filename),function (err,stat) { | |
if (err) | |
return console.error(err); | |
stats[i]=stat; | |
if (stat.isDirectory()) { | |
console.log(' ' + i + ' \033[36m' + filename + '/\033[39m'); | |
} | |
else { | |
console.log(' ' + i + ' \033[90m' + filename + '\033[39m'); | |
} | |
// i++; | |
if (++i === files.length) { | |
// get input of choice. | |
read(); | |
} | |
else { | |
file(i); | |
} | |
}) | |
} | |
function option(data) { | |
let filename = files[Number(data)]; | |
if (!filename){ | |
stdout.write(' \033[31m Enter your choice:\033[39m '); | |
} | |
else if (!stats[Number(data)].isDirectory()) { | |
stdin.pause(); | |
fs.readFile(path.join(workDir,filename), 'utf8', function (err, data) { | |
if (err) | |
return console.error(err); | |
console.log(''); | |
console.log('\033[90m' + data.replace(/(.*)/g, ' $1') + '\033[39m'); | |
}); | |
} | |
else { | |
stdin.pause(); | |
fs.readdir(path.join(workDir,filename), function (err, files) { | |
if (err) | |
return console.error(err); | |
console.log(''); | |
console.log(' (' + files.length + ' files'); | |
files.forEach(function (file) { | |
console.log(' - ' + file); | |
}); | |
console.log(''); | |
}); | |
} | |
} | |
function read(){ | |
console.log(''); | |
stdout.write(' \033[33m Enter your choice:\033[39m '); | |
stdin.resume(); | |
// stdin.setEncoding('utf8'); | |
stdin.on('data',option); | |
} | |
file(0); | |
} | |
// equal to : fs.readdir('.', async()); | |
// fs.readdir(__dirname, async); | |
fs.readdir(workDir, async); |
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
{ | |
"name": "FileBrowser", | |
"version": "0.0.1", | |
"description": "A command-line cross-platform file browser !", | |
"dependencies": { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment