-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmenu.js
42 lines (32 loc) · 893 Bytes
/
menu.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
var readline = require('readline');
// var module = require("module")
module.exports = function (name, commands) {
var input = readline.createInterface(process.stdin, process.stdout);
input.setPrompt(name);
input.prompt();
commands.exit = function() {
input.close();
}
commands.list = function() {
console.log(Object.keys(commands).join('\n'))
}
var keys = Object.keys(commands);
input.on('line', function(line) {
var r, matched;
for( var i = 0, length = keys.length; i < length; i++ ) {
var r = line.match(new RegExp("^" + keys[i] + "$"));
if (r) {
commands[keys[i]](r);
matched = true;
}
}
if (!matched && line) {
console.log('unknown command, try "list"')
}
input.prompt();
}).on('close',function(){
console.log('exiting...')
process.exit(0);
});
return input;
}