-
Notifications
You must be signed in to change notification settings - Fork 0
/
consoleIO.js
47 lines (37 loc) · 981 Bytes
/
consoleIO.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
const _ = require('lodash');
const readline = require('readline');
const deferred = require('deferred');
class ConsoleIO {
constructor () {
this.readlineDeferred = null;
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
this.rl.on('line', line => {
if (!this.readlineDeferred) {
return;
}
this.readlineDeferred.resolve(line);
this.readlineDeferred = null;
});
}
setPrompt (prompt) {
this.rl.setPrompt(prompt);
}
readline () {
this.readlineDeferred = deferred();
this.rl.prompt();
return this.readlineDeferred.promise;
}
close () {
this.rl.close();
}
addCloseHandler (handler) {
this.rl.on('close', handler);
}
writeline (str) {
console.log(_.isUndefined(str) ? '' : str);
}
}
module.exports = ConsoleIO;