-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·37 lines (35 loc) · 1.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env node --harmony
'use strict';
const chalk = require('chalk');
const keypress = require('keypress');
const program = require('commander');
const colors = [chalk.red, chalk.green, chalk.yellow, chalk.blue, chalk.magenta, chalk.cyan, chalk.gray, chalk.white, chalk.black];
let type = process.argv.indexOf('-w')!=-1?'w':'l';
keypress(process.stdin);
function randomColor(){
return colors[Math.floor(Math.random()*colors.length)]
}
let currentColor = randomColor();
program.option('-w, --word', 'Change color every word (default is every letter)').action(function() {}).parse(process.argv);
process.stdin.on('keypress', function(ch,key) {
if (key){
if ((key.ctrl && key.name=='c') || key.name=='escape'){
process.stdin.pause();
} else if (key.name=="return"){
process.stdout.write('\n');
} else if (key.name=='space'){
currentColor = randomColor();
} else if (key.name == 'backspace'){
process.stdout.write('\b\x1b[K');
}
}
if(ch){
if (type == 'w'){
process.stdout.write(currentColor(ch));
}else{
process.stdout.write((randomColor())(ch));
}
}
});
process.stdin.setRawMode(true);
process.stdin.resume();