Comparing version
@@ -58,29 +58,35 @@ /** | ||
// Propagate keypress events directly on the readline | ||
// Make sure to check if rl exist because keypress (\r) will be triggered after rl "line" | ||
// events | ||
process.stdin.on("keypress", function( s, key ) { | ||
self.rl && self.rl.emit( "keypress", s, key ); | ||
}); | ||
process.stdin.on( "keypress", this.onKeypress ); | ||
// Make sure new prompt start on a newline when closing | ||
self.rl.on("close", function() { | ||
console.log("\n"); // Line return | ||
}); | ||
self.rl.on( "SIGINT", this.onForceClose ); | ||
// Start running the questions | ||
async.mapSeries( questions, onEach, onCompletion ); | ||
// Control flow functions | ||
function onEach( question, done ) { | ||
var onCompletion = function() { | ||
// Remove events listeners | ||
this.rl.removeListener("SIGINT", this.onForceClose); | ||
process.stdin.removeListener( "keypress", this.onKeypress ); | ||
// Close the readline | ||
this.rl.pause(); | ||
if ( _.isFunction(allDone) ) { | ||
allDone(this.answers); | ||
} | ||
}.bind(this); | ||
var onEach = function( question, done ) { | ||
// Callback to save answer and continu to next question | ||
function after( answer ) { | ||
self.answers[question.name] = answer; | ||
var after = function( answer ) { | ||
this.answers[question.name] = answer; | ||
done( null ); | ||
} | ||
}.bind(this); | ||
// Default type to input | ||
if ( !self.prompts[question.type] ) { | ||
if ( !this.prompts[question.type] ) { | ||
question.type = "input"; | ||
} | ||
var prompt = new self.prompts[question.type]( question, self.rl ); | ||
var prompt = new this.prompts[question.type]( question, this.rl ); | ||
@@ -94,18 +100,29 @@ // Check if prompt should be runned (if `when` return true) | ||
} | ||
}, self.answers ); | ||
} | ||
}, this.answers ); | ||
}.bind(this); | ||
function onCompletion() { | ||
// Remove events listeners | ||
self.rl.removeAllListeners(); | ||
process.stdin.removeAllListeners("keypress"); | ||
// Start running the questions | ||
async.mapSeries( questions, onEach, onCompletion ); | ||
// Close the readline | ||
self.rl.close(); | ||
self.rl = null; | ||
}; | ||
if ( _.isFunction(allDone) ) { | ||
allDone(self.answers); | ||
} | ||
} | ||
}; | ||
/** | ||
* Propagate keypress to the readline | ||
* @return {null} | ||
*/ | ||
cli.onKeypress = function( s, key ) { | ||
this.rl.emit( "keypress", s, key ); | ||
}.bind(cli); | ||
/** | ||
* Handle the ^C exit | ||
* @return {null} | ||
*/ | ||
cli.onForceClose = function() { | ||
this.rl.close(); | ||
console.log("\n"); // Line return | ||
}.bind(cli); |
@@ -29,4 +29,2 @@ /** | ||
// @NOTE: I keep both selected and raw default because I'd like Rawlist to update in | ||
// an auto-completion style when you type letter. That's a TODO. | ||
this.selected = 0; | ||
@@ -59,2 +57,3 @@ this.rawDefault = 0; | ||
this.rl.on( "line", this.onSubmit.bind(this) ); | ||
this.rl.on( "keypress", this.onKeypress.bind(this) ); | ||
@@ -147,1 +146,16 @@ // Init the prompt | ||
}; | ||
/** | ||
* When user press a key | ||
*/ | ||
Prompt.prototype.onKeypress = function( s, key ) { | ||
var input = Number(this.rl.line); | ||
var index = input - 1; | ||
if ( this.opt.choices[index] ) { | ||
this.selected = index; | ||
this.down().clean(1).render(); | ||
this.rl.output.write( input.toString() ); | ||
} | ||
}; |
{ | ||
"name": "inquirer", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"description": "A collection of common interactive command line user interfaces.", | ||
@@ -5,0 +5,0 @@ "main": "lib/inquirer.js", |
@@ -11,2 +11,3 @@ var EventEmitter = require("events").EventEmitter; | ||
close : sinon.stub().returns(stub), | ||
pause : sinon.stub().returns(stub), | ||
resume : sinon.stub().returns(stub), | ||
@@ -13,0 +14,0 @@ output : { |
@@ -26,5 +26,4 @@ /** | ||
}, function( answers ) { | ||
expect(inquirer.rl).to.not.exist; | ||
expect(rl.resume.called).to.be.true; | ||
expect(rl.close.called).to.be.true; | ||
expect(rl.pause.called).to.be.true; | ||
@@ -37,5 +36,4 @@ rl = inquirer.rl = new ReadlineStub(); | ||
}, function( answers ) { | ||
expect(inquirer.rl).to.not.exist; | ||
expect(rl.resume.called).to.be.true; | ||
expect(rl.close.called).to.be.true; | ||
expect(rl.pause.called).to.be.true; | ||
done(); | ||
@@ -42,0 +40,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
104483
30.92%1700
1.13%