倉庫readlineの簡単な使用
1905 ワード
var readline = require('readline');
var prefix = '> ';
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
completer: function(line) {// Tab ,
var completions = 'hello exit bye quit'.split(' ')
var hits = completions.filter(function(c) {
if (c.indexOf(line) == 0) {
return c;
}
});
return [hits && hits.length ? hits : completions, line];
},
terminal: true
});
var name;
rl.on('line', function(line) {//
switch(line.trim()) {
case 'hello':
console.log('hello ' + name);
break;
case 'exit':
case 'quit':
case 'bye':
rl.close();
break;
default:
console.log('Say what? I might have heard `' + line.trim() + '`');
break;
}
rl.prompt(true);
}).on('SIGINT', function() {// Ctrl + C
rl.question('Sure to exit ? ', function(answer) {
if (answer.match(/^y(es)?$/i)) {
rl.close();
}else {
rl.prompt(true);
}
});
}).on('close', function() {//
console.log('Bye! Have a good day!');
process.exit(0);
});
rl.setPrompt(prefix);
rl.prompt(true);
rl.question('What is your name?', function(answer) {
console.log('welcome you, ' + answer);
name = answer;
rl.prompt(true);
});