Skip to content

Commit

Permalink
Allow user to pass in outputFunction argument to the prompt function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kami committed May 17, 2011
1 parent 8c794c2 commit ced2af3
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var sprintf = require('sprintf').sprintf;
var USE_ANSI_CODES = true;

var PRINT_FUNC = sys.print;
var PUTS_FUNC = PUTS_FUNC;
var PUTS_FUNC = sys.puts;

/* Style table taken from the Node util module */
var STYLES = { 'bold' : [1, 22],
Expand Down Expand Up @@ -243,9 +243,11 @@ function printWrapped(text, width, indent, outputFunction) {
* @param {String} question Question which is sent to standard output
* @param {Array} validOptions Array of valid options (e.g. ['yes', 'no'])
* @param {String} defaultOption Option which is used as a default if empty line is received
* @param {Function} outputFunction Output function (defaults to PUTS_FUNC).
* @param {Function} callback Callback which is called with user input
*/
function prompt(question, validOptions, defaultOption, callback) {
function prompt(question, validOptions, defaultOption, outputFunction, callback) {
outputFunction = outputFunction || PUTS_FUNC;
var stdin = process.openStdin();
stdin.resume();
var options, option, questionMark, dataString;
Expand All @@ -270,7 +272,7 @@ function prompt(question, validOptions, defaultOption, callback) {
questionMark = '';
}

PRINT_FUNC(sprintf('%s%s%s ', question, options, questionMark));
outputFunction(sprintf('%s%s%s ', question, options, questionMark));

function handleData(data) {
dataString = data.toString().trim();
Expand All @@ -281,10 +283,11 @@ function prompt(question, validOptions, defaultOption, callback) {

if (dataString) {
if (validOptions && validOptions.indexOf(dataString) === -1) {
PUTS_FUNC(sprintf('Invalid option "%s", valid options are: %s', dataString,
validOptions.join(', ')));
outputFunction(sprintf('Invalid option "%s", valid options are: %s', dataString,
validOptions.join(', ')));
return;
}

// Pause is necessary to get Node to exit without manual intervention
stdin.pause();
stdin.removeListener('data', handleData);
Expand Down Expand Up @@ -384,9 +387,6 @@ function puts() {
}
}

exports.USE_ANSI_CODES = USE_ANSI_CODES;
exports.PRINT_FUNC = PRINT_FUNC;

exports.getStylesLength = getStylesLength;
exports.lpad = lpad;
exports.rpad = rpad;
Expand Down

0 comments on commit ced2af3

Please sign in to comment.