-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
61 lines (58 loc) · 1.64 KB
/
cli.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var colors = require('colors/safe');
var readline = require('readline');
module.exports = function(commands) {
var self = this;
this.prependCallback = null;
var rl = readline.createInterface(process.stdin, process.stdout);
this.rl = rl;
rl.setPrompt('> ');
rl.prompt();
rl.on('line', function(line) {
if(line.charAt(0) === '/') {
line = line.slice(1);
var spaceIdx = line.indexOf(' ');
var command = '';
spaceIdx>0?command = line.slice(0,spaceIdx):command = line;
var parameter = line.slice(spaceIdx+1);
if(commands.hasOwnProperty(command)) {
commands[command].handler(parameter);
}
else {
self.systemMessage('Unknown command '+command);
}
}
else {
commands.msg.handler(line);
}
rl.prompt();
})
rl.on('close',function(){
commands.fquit.handler();
process.exit(0);
});
}
module.exports.prototype.print = function(message) {
readline.clearLine(process.stdout, 0)
readline.cursorTo(process.stdout, 0)
console.log(message);
this.rl.prompt(true);
}
module.exports.prototype.info = function(message) {
var prepend = '';
if(this.prependCallback) {
prepend = colors.yellow(this.prependCallback());
prepend += ' ';
}
this.print(prepend+colors.grey(message));
};
module.exports.prototype.systemMessage = function(message) {
this.print(colors.green(message));
};
module.exports.prototype.message = function(remote,message) {
var prepend = '';
if(this.prependCallback) {
prepend = colors.yellow(this.prependCallback());
prepend += ' ';
}
this.print(prepend+colors.white.underline(remote) + ': '+colors.white(message));
};