GitXplorerGitXplorer
w

prompt-improved

public
4 stars
1 forks
0 issues

Commits

List of commits on branch master.
Unverified
db1d0c3e26dbb490830a2ad7fa5b38fa6db027bf

Update README.md

wwesleytodd committed 10 years ago
Unverified
665f2d1266e6ec3dab911b0374dc4b005b91cbb5

random

wwesleytodd committed 11 years ago
Unverified
4c3f756d3ebc0d24a19959670ce8bfaf134ff27d

Added repository field to package.json

wwesleytodd committed 11 years ago
Unverified
1743600fb0099a80743cd8918ed3360d3bafd987

Converts boolean default overrides to y/n

wwesleytodd committed 11 years ago
Unverified
edfe56591f93ed9b28217d6a294e39493e67f945

No longer display confirmation information about prompts that were not shown

wwesleytodd committed 11 years ago
Unverified
c49f2bf5d0caf814ebc6f8b767b3aa0d6fcd7bdd

Fixed after filters to run even if it is the default value

wwesleytodd committed 11 years ago

README

The README file for this repository.

Prompts Improved

Node.js prompts like you always wanted. Style them to your heart's content using Chalk, an amazing CLI package for colors and text styling. Features include:

  • Default options
  • Boolean questions (simple y/n answers get converted to true/false)
  • Input validation (using regex's or functions)
  • Input filters (both before validation and after)
  • Required fields
  • Input attempt limits (think limiting password tries)
  • Ask single or multiple questions
  • When asking multiple questions, subsequent questions can be dependant on previous input. Allows for simple logic in prompt path.
  • Configurable prefixs and suffixes
  • Override defaults at runtime (overrideDefaults: {key: value})
  • Custom error messaging
  • Confirmation prompt for input of multiple questions
  • Input timeouts
  • Per-prompt configuration
  • Convient error handeling
  • (Coming Soon) Multiple choice questions

Examples

Just ask your user a question:

var Prompt = require('prompt-imporved');

var prompt = new Prompt({
	// Some options for all prompts
	prefix: '[?] ',
	prefixTheme: Prompt.chalk.green
});

prompt.ask('Where in the world is Carmen Sandiego?', function(err, res) {
	if (err) return console.error(err); // err is null if no errors
	console.log('Response: ' + res);
});

// Outputs: [?] Where in the world is Carmen Sandiego?: 

Options can be provided for all prompts or overridden on a per-prompt basis:

var Prompt = require('prompt-imporved');

var prompt = new Prompt({
	suffix: '? '
});

prompt.ask('How is your day going beautiful', function(err, res) {
	if (err) return console.error(err);
	console.log('Response: ' + res);
});

// Outputs: How is your day going beautiful?  

Asking multiple questions:

var Prompt = require('prompt-imporved');
var prompt = new Prompt();
prompt.ask([{
	question: 'Who\'s your daddy?',
	key: 'father',
}, {
	question: 'What is your Mothers name?',
}], function(err, res) {
	if (err) return console.error(err);
	console.log('Father\'s name: ' res.father);
	console.log('Mother\'s name: ' res['What is your Mothers name?']);
});

Full example will all options:

var Prompt = require('prompt-imporved');

// These are all the defaults
var prompt = new Prompt({
	prefix        : '',
	suffix        : ': ',
	defaultPrefix : ' (',
	defaultSuffix : ')',
	textTheme     : Prompt.chalk.bold,
	prefixTheme   : Prompt.chalk.white,
	suffixTheme   : Prompt.chalk.white,
	defaultTheme  : Prompt.chalk.white,
	inputError    : 'Error encountered, try again.',
	requiredError : 'Required! Try again.',
	invalidError  : 'Invalid input: ',
	attemptsError : 'Maximum attempts reached!',
	stdin         : process.stdin,
	stdout        : process.stdout,
	stderr        : process.stderr,
	timeout       : null
});

// Each question can have it's own options
prompt.ask([{
	question: 'A yes or no question',
	key: 'answer-key',
	attempts: 3,
	required: true,
	default: 'Y',
	boolean: true
}{
	question: 'Something where the first letter should be uppercase',
	key: 'answer-key1',
	before: function(val) {
		return val.charAt(0).toUpperCase() + val.slice(1);
	},
	depends: function(answers) {
		return !!answers['answer-key'];
	}
}], function(err, res) {
	if (err) return console.error(err);
	console.log('Response: ' + res);
});

Tests

Unit tests and test coverage are available:

// runs the tests and watches for file changes
$ grunt 

// Runs the coverage generator and opens your borwser to the results
$ grunt coverage

Contributions

Please contribute. Make pull requests against the develop branch.