Skip to content

Commit

Permalink
UI: options 'input' & 'output', defaults to ttys
Browse files Browse the repository at this point in the history
Conflicts:
	package.json
  • Loading branch information
naholyr committed Dec 15, 2014
1 parent dca962a commit b666d2a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/inquirer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
17 changes: 13 additions & 4 deletions lib/ui/baseUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
var _ = require("lodash");
var tty = require("../utils/tty");
var readlineFacade = require("readline2");
var ttys = require("ttys");


/**
Expand All @@ -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);
Expand All @@ -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 );

Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions lib/ui/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"

This comment has been minimized.

Copy link
@adriengibrat

adriengibrat Jan 21, 2015

Got errors on windows and centos about dev/tty since this morning... don't have time for further investigation, but seems related. I've openned an issue on ttys.

},
"devDependencies": {
"chai": "^1.10.0",
Expand Down

0 comments on commit b666d2a

Please sign in to comment.