diff --git a/lib/inquirer.js b/lib/inquirer.js index 41e245613..196846520 100644 --- a/lib/inquirer.js +++ b/lib/inquirer.js @@ -23,8 +23,8 @@ inquirer.ui = { * Create a new self-contained prompt module. */ inquirer.createPromptModule = function () { - var promptModule = function( questions, allDone ) { - var ui = new inquirer.ui.Prompt( promptModule.prompts ); + var promptModule = function( questions, allDone, opt ) { + var ui = new inquirer.ui.Prompt( promptModule.prompts, opt ); ui.run( questions, allDone ); return ui; }; diff --git a/lib/ui/baseUI.js b/lib/ui/baseUI.js index d1a2452c0..b5fd38320 100644 --- a/lib/ui/baseUI.js +++ b/lib/ui/baseUI.js @@ -5,6 +5,7 @@ var _ = require("lodash"); var tty = require("../utils/tty"); var readlineFacade = require("readline2"); +var ttys = require("ttys"); /** @@ -18,9 +19,17 @@ module.exports = UI; */ function UI( opt ) { + this._input = (opt && opt.input) || ttys.stdin; + this._output = (opt && opt.output) || ttys.stdout; + // Instantiate the Readline interface // @Note: Don't reassign if already present (allow test to override the Stream) - this.rl || (this.rl = readlineFacade.createInterface()); + if (!this.rl) { + this.rl = readlineFacade.createInterface({ + input: this._input, + output: this._output + }); + } this.rl.resume(); this.onForceClose = this.onForceClose.bind(this); @@ -31,7 +40,7 @@ function UI( opt ) { process.on( "exit", this.onForceClose ); // Propagate keypress events directly on the readline - process.stdin.addListener( "keypress", this.onKeypress ); + this._input.addListener( "keypress", this.onKeypress ); } _.extend( UI.prototype, tty ); @@ -54,12 +63,12 @@ UI.prototype.onForceClose = function() { UI.prototype.close = function() { // Remove events listeners this.rl.removeListener( "SIGINT", this.onForceClose ); - process.stdin.removeListener( "keypress", this.onKeypress ); + this._input.removeListener( "keypress", this.onKeypress ); process.removeListener( "exit", this.onForceClose ); // Restore prompt functionnalities this.rl.output.unmute(); - process.stdout.write("\x1B[?25h"); // show cursor + this._output.write("\x1B[?25h"); // show cursor // Close the readline this.rl.output.end(); diff --git a/lib/ui/prompt.js b/lib/ui/prompt.js index a4990840d..0a3a4bca7 100644 --- a/lib/ui/prompt.js +++ b/lib/ui/prompt.js @@ -22,8 +22,8 @@ module.exports = PromptUI; * Constructor */ -function PromptUI( prompts ) { - Base.call(this); +function PromptUI( prompts, opt ) { + Base.call(this, opt); this.prompts = prompts; } util.inherits( PromptUI, Base ); diff --git a/package.json b/package.json index 7f6aff748..eca9d2062 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,8 @@ "mute-stream": "0.0.4", "readline2": "~0.1.0", "rx": "2.2.27", - "through": "~2.3.4" + "through": "~2.3.4", + "ttys": "0.0.3" }, "devDependencies": { "chai": "^1.10.0",