forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadline.js
More file actions
executable file
·51 lines (40 loc) · 1.15 KB
/
Copy pathreadline.js
File metadata and controls
executable file
·51 lines (40 loc) · 1.15 KB
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
/**
* DISCLAIMER: Had to implement custom input reading,
*
* because both node-read and node-prompt had bugs on Windows-8.1
* E.g. every input letter was double-printed
* (and I really need starred * input for passwords)
*
*/
var readline = require('readline');
/**
* @param options object { message: what to ask?, hidden: true for passwords }
* @param callback function Calls callback(null, result), no errors no matter what
*/
function readLine(options, callback) {
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
setup();
rl.question(options.message, function(result) {
tearDown();
callback(null, result);
});
function onReadable() {
if (!options.hidden) return;
hideInput();
}
function hideInput() {
if (!rl.line) return; // happens on \n when the input is finished
process.stdout.write("\033[2K\033[200D" + options.message + new Array(rl.line.length+1).join("*"));
}
function setup() {
process.stdin.on("readable", onReadable);
}
function tearDown() {
process.stdin.removeListener("readable", onReadable);
rl.close();
}
}
module.exports = readLine;