Created
June 9, 2011 06:48
-
-
Save killwing/1016211 to your computer and use it in GitHub Desktop.
[hackertyper] type codes in your shell...
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
var util = require('util'); | |
var fs = require('fs'); | |
var type = function(data, completer) { | |
var lines = data.split(' '); | |
var i = 0; | |
var timer = setInterval(function() { | |
var s = lines[i++]; | |
while (lines[i] == '') { | |
s += ' '; | |
i++; | |
} | |
process.stdout.write(s + ' '); | |
if (i == lines.length) { | |
clearInterval(timer); | |
completer(); | |
} | |
}, Math.floor(Math.random()*100)); | |
} | |
var read = function(file, completer) { | |
if (!/.*\.(h|c|cpp|js)/i.test(file)) { | |
console.log('ignore file: ', file); | |
completer(); | |
return; | |
} | |
fs.readFile(file, 'utf-8', function(err, data) { | |
if (err) { | |
console.error('read file error: ', file); | |
return; | |
} | |
console.log('typing file: ', file); | |
type(data, completer); | |
}); | |
} | |
var i = 0; | |
var readnext = function(files) { | |
read(files[i], function() { | |
console.log('finish file: ', files[i]); | |
i++; | |
if (i == files.length) { | |
console.log('finish all files'); | |
return; | |
} | |
readnext(files); | |
}); | |
} | |
// main | |
if (process.argv.length < 3) { | |
console.log('Pls indicate your source directory'); | |
return; | |
} | |
var dir = process.argv[2]; | |
fs.stat(dir, function(err, stats) { | |
if (err) { | |
console.log(err.message); | |
return; | |
} | |
if (!stats.isDirectory()) { | |
console.log('Not a directory'); | |
return; | |
} | |
process.chdir(dir); | |
fs.readdir('.', function(err, files) { | |
readnext(files); | |
}) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment