Comparing version 0.1.3 to 0.1.4
@@ -37,2 +37,4 @@ /* | ||
prompt.allowEmpty = false; | ||
prompt.message = 'prompt'; | ||
prompt.delimiter = ':'; | ||
@@ -66,9 +68,11 @@ // | ||
stdout = options.stdout || process.stdout; | ||
// | ||
// By default: Remeber the last `10` prompt property / | ||
// By default: Remeber the last `10` prompt property / | ||
// answer pairs and don't allow empty responses globally. | ||
// | ||
prompt.memory = options.memory || 10; | ||
prompt.memory = options.memory || 10; | ||
prompt.allowEmpty = options.allowEmpty || false; | ||
prompt.message = options.message || prompt.message; | ||
prompt.delimiter = options.delimiter || prompt.delimiter; | ||
@@ -119,3 +123,3 @@ process.on('SIGINT', function () { | ||
// Returns the `property:value` pair from within the prompts | ||
// `history` array. | ||
// `history` array. | ||
// | ||
@@ -126,3 +130,3 @@ prompt.history = function (search) { | ||
} | ||
var names = history.map(function (pair) { | ||
@@ -133,7 +137,7 @@ return typeof pair.property === 'string' | ||
}); | ||
if (~names.indexOf(search)) { | ||
return null; | ||
} | ||
return history.filter(function (name) { | ||
@@ -191,3 +195,4 @@ return typeof pair.property === 'string' | ||
var name = prop.message || prop.name || prop, | ||
raw = ['prompt', ': ' + name.grey, ': '.grey], | ||
delim = prompt.delimiter + ' ', | ||
raw = [prompt.message, delim + name.grey, delim.grey], | ||
read = prop.hidden ? prompt.readLineHidden : prompt.readLine, | ||
@@ -230,3 +235,3 @@ length, msg; | ||
validator = prop.validator; | ||
function next(valid) { | ||
@@ -376,21 +381,27 @@ if (arguments.length < 1) { | ||
stdin.on('error', callback); | ||
stdin.on('data', function data (c) { | ||
c = '' + c; | ||
switch (c) { | ||
case '\n': case '\r': case '\r\n': case '\u0004': | ||
stdio.setRawMode(false); | ||
stdin.removeListener('data', data); | ||
stdin.removeListener('error', callback); | ||
value = value.trim(); | ||
stdout.write('\n'); | ||
stdout.flush(); | ||
prompt.pause(); | ||
return callback(null, value.replace(/[\s\S][\x7f\x08]/g,'')); | ||
case '\u0003': case '\0': | ||
stdout.write('\n'); | ||
process.exit(1); | ||
break; | ||
default: | ||
value = value + c; | ||
break; | ||
stdin.on('data', function data (line) { | ||
line = line + ''; | ||
for(var i = 0; i < line.length; i++) { | ||
c = line[i]; | ||
switch (c) { | ||
case '\n': case '\r': case '\r\n': case '\u0004': | ||
stdio.setRawMode(false); | ||
stdin.removeListener('data', data); | ||
stdin.removeListener('error', callback); | ||
value = value.trim(); | ||
stdout.write('\n'); | ||
stdout.flush(); | ||
prompt.pause(); | ||
return callback(null, value); | ||
case '\x7f': case'\x08': | ||
value = value.slice(0,-1); | ||
break; | ||
case '\u0003': case '\0': | ||
stdout.write('\n'); | ||
process.exit(1); | ||
break; | ||
default: | ||
value = value + c; | ||
break; | ||
} | ||
} | ||
@@ -414,3 +425,3 @@ }); | ||
}); | ||
// | ||
@@ -417,0 +428,0 @@ // If the length of the `history` Array |
{ | ||
"name": "prompt", | ||
"description": "A beautiful command-line prompt for node.js", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"author": "Nodejitsu Inc. <info@nodejitsu.com>", | ||
"contributors": [ | ||
{ "name": "Charlie Robbins", "email": "charlie@nodejitsu.com" }, | ||
{ "name": "Joshua Holbrook", "email": "josh.holbrook@gmail.com" } | ||
{ "name": "Joshua Holbrook", "email": "josh.holbrook@gmail.com" }, | ||
{ "name": "Bradley Meck", "email": "bradley.meck@gmail.com" } | ||
], | ||
@@ -27,2 +28,2 @@ "repository": { | ||
} | ||
} | ||
} |
210
README.md
@@ -5,2 +5,8 @@ # node-prompt | ||
## Features | ||
* prompts the user for input | ||
* supports validation and defaults | ||
* hides passwords | ||
## Installation | ||
@@ -10,3 +16,3 @@ | ||
``` | ||
curl http://npmjs.org/install.sh | sh | ||
curl http://npmjs.org/install.sh | sh | ||
``` | ||
@@ -16,3 +22,3 @@ | ||
``` | ||
[sudo] npm install prompt | ||
[sudo] npm install prompt | ||
``` | ||
@@ -27,20 +33,20 @@ | ||
``` js | ||
var prompt = require('prompt'); | ||
var prompt = require('prompt'); | ||
// | ||
// Start the prompt | ||
// | ||
prompt.start(); | ||
// | ||
// Start the prompt | ||
// | ||
prompt.start(); | ||
// | ||
// Get two properties from the user: username and email | ||
// | ||
prompt.get(['username', 'email'], function (err, result) { | ||
// | ||
// Get two properties from the user: username and email | ||
// Log the results. | ||
// | ||
prompt.get(['username', 'email'], function (err, result) { | ||
// | ||
// Log the results. | ||
// | ||
console.log('Command-line input received:'); | ||
console.log(' username: ' + result.username); | ||
console.log(' email: ' + result.email); | ||
}) | ||
console.log('Command-line input received:'); | ||
console.log(' username: ' + result.username); | ||
console.log(' email: ' + result.email); | ||
}) | ||
``` | ||
@@ -51,8 +57,8 @@ | ||
``` | ||
$ node examples/simple-prompt.js | ||
prompt: username: some-user | ||
prompt: email: some-user@some-place.org | ||
Command-line input received: | ||
username: some-user | ||
email: some-user@some-place.org | ||
$ node examples/simple-prompt.js | ||
prompt: username: some-user | ||
prompt: email: some-user@some-place.org | ||
Command-line input received: | ||
username: some-user | ||
email: some-user@some-place.org | ||
``` | ||
@@ -64,31 +70,31 @@ | ||
``` js | ||
var properties = [ | ||
{ | ||
name: 'name', | ||
validator: /^[a-zA-Z\s\-]+$/, | ||
warning: 'Name must be only letters, spaces, or dashes', | ||
empty: false | ||
}, | ||
{ | ||
name: 'password', | ||
hidden: true | ||
} | ||
]; | ||
var properties = [ | ||
{ | ||
name: 'name', | ||
validator: /^[a-zA-Z\s\-]+$/, | ||
warning: 'Name must be only letters, spaces, or dashes', | ||
empty: false | ||
}, | ||
{ | ||
name: 'password', | ||
hidden: true | ||
} | ||
]; | ||
// | ||
// Start the prompt | ||
// | ||
prompt.start(); | ||
// | ||
// Start the prompt | ||
// | ||
prompt.start(); | ||
// | ||
// Get two properties from the user: email, password | ||
// | ||
prompt.get(properties, function (err, result) { | ||
// | ||
// Get two properties from the user: email, password | ||
// Log the results. | ||
// | ||
prompt.get(properties, function (err, result) { | ||
// | ||
// Log the results. | ||
// | ||
console.log('Command-line input received:'); | ||
console.log(' name: ' + result.name); | ||
console.log(' password: ' + result.password); | ||
}); | ||
console.log('Command-line input received:'); | ||
console.log(' name: ' + result.name); | ||
console.log(' password: ' + result.password); | ||
}); | ||
``` | ||
@@ -98,13 +104,13 @@ | ||
``` bash | ||
$ node examples/property-prompt.js | ||
prompt: name: nodejitsu000 | ||
error: Invalid input for name | ||
error: Name must be only letters, spaces, or dashes | ||
prompt: name: Nodejitsu Inc | ||
prompt: password: | ||
Command-line input received: | ||
name: Nodejitsu Inc | ||
password: some-password | ||
``` | ||
$ node examples/property-prompt.js | ||
prompt: name: nodejitsu000 | ||
error: Invalid input for name | ||
error: Name must be only letters, spaces, or dashes | ||
prompt: name: Nodejitsu Inc | ||
prompt: password: | ||
Command-line input received: | ||
name: Nodejitsu Inc | ||
password: some-password | ||
``` | ||
@@ -117,11 +123,11 @@ ## Valid Property Settings | ||
``` js | ||
{ | ||
message: 'Enter your password', // Prompt displayed to the user. If not supplied name will be used. | ||
name: 'password' // Key in the JSON object returned from `.get()`. | ||
validator: /^\w+$/ // Regular expression that input must be valid against. | ||
warning: 'Password must be letters' // Warning message to display if validation fails. | ||
hidden: true // If true, characters entered will not be output to console. | ||
default: 'lamepassword' // Default value to use if no value is entered. | ||
empty: false // If false, value entered must be non-empty. | ||
} | ||
{ | ||
message: 'Enter your password', // Prompt displayed to the user. If not supplied name will be used. | ||
name: 'password' // Key in the JSON object returned from `.get()`. | ||
validator: /^\w+$/ // Regular expression that input must be valid against. | ||
warning: 'Password must be letters' // Warning message to display if validation fails. | ||
hidden: true // If true, characters entered will not be output to console. | ||
default: 'lamepassword' // Default value to use if no value is entered. | ||
empty: false // If false, value entered must be non-empty. | ||
} | ||
``` | ||
@@ -133,28 +139,66 @@ | ||
``` js | ||
var obj = { | ||
password: 'lamepassword', | ||
mindset: 'NY' | ||
} | ||
var obj = { | ||
password: 'lamepassword', | ||
mindset: 'NY' | ||
} | ||
// | ||
// Log the initial object. | ||
// | ||
console.log('Initial object to be extended:'); | ||
console.dir(obj); | ||
// | ||
// Add two properties to the empty object: username and email | ||
// | ||
prompt.addProperties(obj, ['username', 'email'], function (err) { | ||
// | ||
// Log the initial object. | ||
// Log the results. | ||
// | ||
console.log('Initial object to be extended:'); | ||
console.log('Updated object received:'); | ||
console.dir(obj); | ||
}); | ||
``` | ||
// | ||
// Add two properties to the empty object: username and email | ||
// | ||
prompt.addProperties(obj, ['username', 'email'], function (err) { | ||
// | ||
// Log the results. | ||
// | ||
console.log('Updated object received:'); | ||
console.dir(obj); | ||
}); | ||
## Customizing your prompt | ||
Aside from changing `property.message`, you can also change `prompt.message` | ||
and `prompt.delimiter` to change the appearance of your prompt. | ||
The basic structure of a prompt is this: | ||
``` js | ||
prompt.message + prompt.delimiter + property.message + prompt.delimiter; | ||
``` | ||
The default `prompt.message` is "prompt," the default `prompt.delimiter` is | ||
": ", and the default `property.message` is `property.name`. | ||
Changing these allows you to customize the appearance of your prompts! In | ||
addition, node-prompt supports ANSI color codes via the | ||
[colors module](https://github.com/Marak/colors.js) for custom colors. For a | ||
very colorful example: | ||
``` js | ||
var prompt = require("prompt"); | ||
// | ||
// The colors module adds color properties to String.prototype | ||
// | ||
require("colors"); | ||
// | ||
// Setting these properties customizes the prompt. | ||
// | ||
prompt.message = "Question!".rainbow; | ||
prompt.delimiter = "><".green; | ||
prompt.start(); | ||
prompt.get([{ name: "name", | ||
message: "What is your name?".magenta }], function (err, result) { | ||
console.log("You said your name is: ".cyan + result.name.cyan); | ||
}); | ||
``` | ||
## Running tests | ||
``` | ||
vows test/*-test.js --spec | ||
vows test/*-test.js --spec | ||
``` | ||
@@ -161,0 +205,0 @@ |
@@ -59,2 +59,6 @@ /* | ||
}, | ||
notblank: { | ||
name: 'notblank', | ||
empty: false | ||
}, | ||
password: { | ||
@@ -61,0 +65,0 @@ name: 'password', |
@@ -20,3 +20,3 @@ /* | ||
// | ||
prompt.started = false | ||
prompt.started = false; | ||
prompt.start(); | ||
@@ -23,0 +23,0 @@ winston.info('These node-prompt tests are interactive'); |
@@ -16,2 +16,6 @@ /* | ||
topic: function () { | ||
// | ||
// Reset the prompt for mock testing | ||
// | ||
prompt.started = false; | ||
prompt.start({ | ||
@@ -38,3 +42,3 @@ stdin: helpers.stdin, | ||
prompt.readLineHidden(this.callback); | ||
helpers.stdin.write('no-\x08backspace.\xff'); | ||
helpers.stdin.write('no-\x08backspace.\x7f'); | ||
helpers.stdin.write('\n'); | ||
@@ -74,2 +78,27 @@ }, | ||
}, | ||
"with any field that is not supposed to be empty": { | ||
"and we don't provide any input": { | ||
topic: function () { | ||
var that = this; | ||
helpers.stdout.once('data', function (msg) { | ||
that.msg = msg; | ||
}); | ||
helpers.stderr.once('data', function (msg) { | ||
that.errmsg = msg; | ||
}); | ||
prompt.getInput(helpers.properties.notblank, function () {}); | ||
prompt.once('invalid', this.callback.bind(null, null)) | ||
helpers.stdin.write('\n'); | ||
}, | ||
"should prompt with an error": function (ign, prop, input) { | ||
assert.isObject(prop); | ||
assert.equal(input, ''); | ||
assert.isTrue(this.errmsg.indexOf('Invalid input') !== -1); | ||
assert.isTrue(this.msg.indexOf('notblank') !== -1); | ||
} | ||
} | ||
}, | ||
"with a hidden field that is not supposed to be empty": { | ||
@@ -221,5 +250,8 @@ "and we provide valid input": { | ||
prompt.properties['fnvalidator'] = helpers.properties['fnvalidator']; | ||
prompt.get('fnvalidator', this.callback); | ||
prompt.get(helpers.properties.fnvalidator, this.callback); | ||
helpers.stdin.write('fn123\n'); | ||
}, | ||
"should accept a value that is checked": function (err, result) { | ||
assert.isNull(err); | ||
assert.equal(result['fnvalidator'],'fn123'); | ||
} | ||
@@ -235,5 +267,8 @@ }, | ||
prompt.properties['cbvalidator'] = helpers.properties['cbvalidator']; | ||
prompt.get('cbvalidator', this.callback); | ||
prompt.get(helpers.properties.cbvalidator, this.callback); | ||
helpers.stdin.write('cb123\n'); | ||
}, | ||
"should not accept a value that is correct": function (err, result) { | ||
assert.isNull(err); | ||
assert.equal(result['cbvalidator'],'cb123'); | ||
} | ||
@@ -240,0 +275,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
96649
18
1184
201