flatiron / prompt Goto Github PK
View Code? Open in Web Editor NEWa beautiful command-line prompt for node.js
Home Page: http://github.com/flatiron/prompt
License: MIT License
a beautiful command-line prompt for node.js
Home Page: http://github.com/flatiron/prompt
License: MIT License
related : nodejitsu/jitsu#18
Basically you have to jump through hoops right now if there is a list of properties being added to an object and you need to use a function (in this case a http request) to test if something is valid. In this case we should allow validator to be a simple function that matches :
function myValidator ( newValue, next ) {
next( isValid( newValue ) );
}
There seems no chance for user to go back and fix it.
?> numbers1 54
?> numbers2 a <= INVALID VALUE
?> numbers3
-------------- { '0': '54' }
-------------- { '0': 'a' }
error: Invalid input for numbers
?> numbers3 <= HOW TO FIX number2 ?
Quick question....
Why in your package.json you used a fixed version. It should be better to use a ~ in the require, it helps to follow the latest module based on its minor version without any change on your module.
eg. ~0.6.2 instead of 0.6.2
See : https://github.com/isaacs/node-semver
My 2 cents
From what I can tell, these features (at least) are not documented.
I'll see what I can do.
There should be a way to do conditional prompt. Useful when some questions depends on the answer of others.
Although prompting is a good UX for first time users, power users may prefer passing in all the options as cli arguments.
I propose adding prompt.override
which could be used to achive this:
prompt.override = {name: 'whoever'}
prompt.get(['name'], function (err,answer){
//it will just take name from .override and not prompt the user.
})
Then it would be a one line change to add this feature to all the nodejitsu cli tools, prompt.override = optimist.argv
the option would still be validated, and would call back an error if it failed.
While using Prompt's 'hidden' property to ask for password data, I noticed that hitting a wrong key and then hitting backspace
did not allow me to correct my mistake on the fly - when I checked the data that Prompt
returned, every letter key I pressed was present in the final string, as if backspace
had never been pressed.
Currently prompt properties are contained in an array of objects with a name
properties in each object.
JSON-Schema properties are stored as key-value pairs where the key is the name
property and the value is an object which represents the specific property.
We should stick to the JSON-Schema format since we are already using it in resourceful
and revalidator
.
The signature has changed. The "capitalize" function is no longer part of the "inflect" submodule but is on utile directly
so
capitalize = utile.inflect.capitalize,
is now
capitalize = utile.capitalize,
prompt.js
The following needs to be changed from:
var events = require('events'),
readline = require('readline'),
utile = require('utile'),
async = utile.async,
capitalize = utile.inflect.capitalize,
read = require('read'),
validate = require('revalidator').validate,
winston = require('winston');
to:
var events = require('events'),
readline = require('readline'),
utile = require('utile'),
async = utile.async,
capitalize = utile.capitalize,
read = require('read'),
validate = require('revalidator').validate,
winston = require('winston');
Using process.openStdin()
cause some problems in windows, when prompting in a event loop queued callback. I first open this issue in flatiron/flatiron all the explanation is here.
The easy way to reproduce this problem is running something like this:
process.nextTick(function(){
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('error', function (err) {
console.log(err);
process.exit();
});
process.stdin.on('data', function (chunk) {
process.stdout.write('data: ' + chunk);
});
});
process.openStdin();
process.stdin.pause();
We already use the old implemenation of read
that was in npm; we should use this module directly now.
nt
Currently, there is no way enter valid values for arrays.
Example:
var prompt = require('../lib/prompt');
var schema = {
properties: {
flavors: {
type: 'array'
}
}
};
prompt.start();
prompt.get(schema, function (err, result) {
if(err) {
console.log('cancelled error');
return;
}
console.log(err, result);
});
Instead of not being able to enter any values, the user should be presented with a mini-menu for adding items to an array until they are done adding items.
Would it be possible to use an alternate name or create an alias for the "default" property? JSHint is reporting it as a reserved word when I try to lint my code (because it is a reserved word).
The idea is to eliminate the need to call prompt.start()
.
In a closure containing prompt.start and prompt.get:
var started = false;
in prompt.start:
started = true;
in prompt.get:
if (!started) prompt.start();
This could be implemented directly with extendFunction:
(function() {
var started = false;
extendFunction('prompt.start', function(){
started = true;
});
extendFunction('prompt.get', function(){
if (!started) prompt.start();
});
})();
There are a couple of typos in examples/override-validation.js
You will only be asked for name because it's invalid
Hi,
It should be great to browse prompt history (or a defined list of value) by up/down key stroke.
ps: thanks for this great node tool :)
See new tests in test/interactive-test.js
Running prompt on a Windows machine within the Cygwin environment produces the following error output. (I've reduced the test-case down to a 'simplest' example).
Node-prompt does run properly in the normal windows environment, but I'd like to use prompt along with some libraries that require cygwin-type support.
It does seem to be a prompt specific problem as other nodejs libraries I've used do not have the same problem (examples: http, express, optimist, socket.io)
Simple Example (prompt-test.js)
var prompt = require('prompt');
prompt.start();
prompt.get(['input'], function (err, result) {
console.log('input: ' + result.input);
});
Output
prompt: input:
events.js:72
throw er; // Unhandled 'error' event
^
Error: read ENOTCONN
at errnoException (net.js:884:11)
at Socket._read (net.js:389:21)
at Socket.Readable.read (_stream_readable.js:304:10)
at Socket.read (net.js:291:43)
at new Socket (net.js:185:10)
at process.stdin (node.js:660:19)
at EventEmitter.prompt.start (~\node_modules\prompt\lib\prompt.js:75:38)
at Object.<anonymous> (\path\to\file\prompt-test.js:2:8)
When prompting the user for input, it should be possible to use a falsy value as an option, e.g. options.message to be the empty string, or options.colors to be false
.
In addition, when options.message
is the empty string, the delimiter should not be printed before the question.
Hi guys prompt is a great module, but I've found a couple of minor issues:
var schema={
properties:{
filename: {
before: function(value) { return 'v -> ' + value.red; },
// description:'Enter output file name'.green,
pattern: /\b|(^[a-zA-Z\s\-]+$)/,
message: 'Name must be only letters, spaces, or dashes',
default: 'filename.json',
required: false
},
}
}
If description is uncommented, then the before function is ignored. Why?
pattern has to be \b|(actual pattern) to allow empty strings, as in (http://stackoverflow.com/questions/3333461/regular-expression-which-matches-a-pattern-or-is-an-empty-string). Otherwise required flag is ignored and an error message is prompted, ignoring the default value and asking for a valid string.
Using Node.js 0.8.15 on Ubuntu 12.04
could we get non-zero exit codes if a user exits out of a prompt?
i tried this, didn't work :(
process.on('exit', function () {
process.exit(1);
});
Main things that needs working on is process.binding(stdio)
.
_
nt. see: https://gist.github.com/1424874
on node v0.8.16 prompt is not blocking when using example code:
var prompt = require('prompt');
//
// Start the prompt
//
prompt.start();
//
// Get two properties from the user: username and email
//
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);
});
It's already in the source code as a comment, but I wanted to track it here.
e.g. If I do this:
var promptProperties = [
{
description: "Enter your username",
name: 'username',
validator: /^[a-zAZ0-9\._-]+$/,
warning: 'Username must be only letters and/or numbers'
},
{
description: "Enter your password",
name: 'password',
hidden: true
},
{
description: "Enter the message subject",
name: 'subject'
},
{
description: "Enter the email message",
name: 'message'
}
];
then the first prompt shows prompt: username:
but if I do
var promptProperties = [
{
description: "Enter your username",
name: 'username',
},
{
description: "Enter your password",
name: 'password',
hidden: true
},
{
description: "Enter the message subject",
name: 'subject'
},
{
description: "Enter the email message",
name: 'message'
}
];
then the first prompt shows prompt: Enter your username:
as expected.
Including both a type:'number' and description key seems to break the input.
var fields = {
price : {
description : 'Price',
default : 10,
type : 'number'
}
};
prompt.get({ properties : fields }, function (err, result) {
// other stuff...
});
Output:
prompt: Price: (10)
error: Invalid input for Price
prompt: Price: (10) 1
error: Invalid input for Price
Using message
instead fixes the problem, but doesn't give you a nice name in the input.
When I have a prompt set to hidden
it only records the first character when pasting a value.
If I type the value it works just fine.
It would be nice if the ANSI colors on the prompt: [message]
string were configurable somehow, to allow greater flexibility in custom prompts.
To replicate:
var prompt = require('../lib/prompt');
var schema = {
properties: {
cookies: {
name: 'cookies',
format: 'number',
default: 10
}
}
};
prompt.start();
prompt.get(schema, function (err, result) {
console.log(result);
});
prompt: cookies: (10)
[ENTER]
If user presses ENTER without typing value, 10 ( the default value ) is not picked up in the result.
nt
Here is a gist of what I'm encountering: https://gist.github.com/76bb706abfc904bf2200
Is this related to the 0.6 compatibility?
$ npm install -d
npm info it worked if it ends with ok
npm info using [email protected]
npm info using [email protected]
npm info preinstall [email protected]
npm http GET https://registry.npmjs.org/vows
npm http GET https://registry.npmjs.org/pkginfo
npm http GET https://registry.npmjs.org/read
npm http GET https://registry.npmjs.org/revalidator
npm http GET https://registry.npmjs.org/utile
npm http GET https://registry.npmjs.org/winston
npm http 304 https://registry.npmjs.org/read
npm ERR! Error: No compatible version found: read@'>=1.0.0- <1.1.0-'
npm ERR! Valid install targets:
npm ERR! ["0.0.1","0.0.2","0.0.3","0.1.0","0.1.1"]
npm ERR! at installTargetsError (/usr/local/lib/node_modules/npm/lib/cache.js:496:10)
npm ERR! at next_ (/usr/local/lib/node_modules/npm/lib/cache.js:446:17)
npm ERR! at next (/usr/local/lib/node_modules/npm/lib/cache.js:423:44)
npm ERR! at /usr/local/lib/node_modules/npm/lib/cache.js:416:5
npm ERR! at saved (/usr/local/lib/node_modules/npm/lib/utils/npm-registry-client/get.js:151:7)
npm ERR! at Object.oncomplete (/usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:230:7)
npm ERR! You may report this log at:
npm ERR! http://github.com/isaacs/npm/issues
npm ERR! or email it to:
npm ERR! [email protected]
npm ERR!
npm ERR! System Darwin 12.0.0
npm ERR! command "node" "/usr/local/bin/npm" "install" "-d"
npm ERR! cwd /Users/kimon/Documents/fourleafcl/temp/prompt/prompt
npm ERR! node -v v0.6.19
npm ERR! npm -v 1.1.24
npm ERR! message No compatible version found: read@'>=1.0.0- <1.1.0-'
npm ERR! message Valid install targets:
npm ERR! message ["0.0.1","0.0.2","0.0.3","0.1.0","0.1.1"]
npm http 304 https://registry.npmjs.org/utile
npm http 304 https://registry.npmjs.org/pkginfo
npm http 304 https://registry.npmjs.org/revalidator
npm http 304 https://registry.npmjs.org/winston
npm http 200 https://registry.npmjs.org/vows
npm http GET https://registry.npmjs.org/vows/-/vows-0.6.3.tgz
npm http 200 https://registry.npmjs.org/vows/-/vows-0.6.3.tgz
npm info shasum 5b4b2abd99ef1e6b224dc7c58031742f7288b02a
npm info shasum /var/folders/8h/hdz315fj0mdd96p8h5pwfpf00000gn/T/npm-1344029022746/1344029022746-0.9754867081064731/tmp.tgz
npm info shasum 5b4b2abd99ef1e6b224dc7c58031742f7288b02a
npm info shasum /Users/kimon/.npm/vows/0.6.3/package.tgz
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/kimon/Documents/fourleafcl/temp/prompt/prompt/npm-debug.log
npm not ok
npm not ok
This doesn't seem to work:
prompt.get([ 'hookName' ], function (err, results) {
introspect.generate(results.hookName);
});
because instead of results
having a .hookName property, it has a .hookname property.
Imo, it would be nice to fix this, but .toLowerCase-ing strikes me as a deliberate thing to do.
schema:
schema =
properties:
'username':
description: '用户名'
pattern: /^[a-zA-Z\s\-]+$/
message: '用户名必须由字母、空格、横线组成'
required: true
'password':
description: '密码'
required: true
hidden: true
result:
prompt: 用户名d
prompt: 密码:
{ username: 'd', password: 'a' }
and i have another question:
How to remove the prefix of each line such as "prompt: " "error: ". or place them with other strings
提示:
错误:
...
I see that winston supports configurable colors. It would be nice if prompt did, too. Perhaps it could use the same code to do it if winston's color facility got reorganized a little bit.
Is possible to have a synchrronous prompt?
Hi,
See line 194 of prompt.js:
191 prompt.getInput = function (prop, callback) {
192 var name = prop.message || prop.name || prop,
193 propName = prop.name || prop,
194 delim = prompt.delimiter + ' ',
195 raw = [prompt.message, delim + name.grey, delim.grey],
196 read = prop.hidden ? prompt.readLineHidden : prompt.readLine,
197 length, msg;
If I set prompt.message and prompt.delimiter to '' (empty string) I would like the property message to begin in the first column of the terminal. As it stands, it will be indented by a single space.
The README states:
The basic structure of a prompt is this:
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.
This isn't really the case. The default prompt.delimiter is ":" and you pad it with an additional space.
Thanks,
-John
Not sure if this is Mac OS specific or not.
To reproduce:
var prompt = require('../lib/prompt');
var schema = {
properties: {
cookies: {
name: 'cookies',
type: 'number',
default: 0
}
}
};
prompt.start();
prompt.get(schema, function (err, result) {
if(err) {
console.log('cancelled error');
return;
}
console.log(err, result);
});
**Run the script, and press CTRL-C when prompted ( to cancel prompt )
~/dev/flatiron/prompt/node examples/schema.js
prompt: cookies: cancelled error
~/dev/flatiron/prompt/
Notice that the console.log doesn't register as a new line? It's dumping it on the same line as the prompt. Canceling a prompt should jump the cursor to the start of the next line.
I have been looking for a way to nest prompts, but have been unsuccessful:
prompt.get(['question'], function(err, results) {
if (results.question.toLowerCase() === 'y' || 'yes') {
prompt.get(['question2'], function(err, results){
do something;
})
} else {
process.emit('exit');
}
});
When I run this, regardless of what value the question prompt returns, question2 gets asked.
I have been using prompt for a large portion of my script, so would like to incorporate it in this instance as well. Before I go off and write a replacement function, I wanted to see if somehting like this would be supported (and I am just missing it)
Thanks
The validation code is a really great feature of this module, I think it would be very helpful to be able to apply it to ordinary command line options and configuration settings -- these are also user input, and thus need to be validated.
It would be very nice to be able to still do console.log statements to stdout and the prompt would fix itself. Or at minimum have the ability to do prompt.reprompt() and whatever the current prompt, if there is one, is reprinted to stdout.
To make it full fledged goodness you would need to cache the characters typed at the current prompted, print X delete characters removing the old prompt and the cached thus far typed characters and then, after console.log...., reprompt the current prompt with cached content.
FYI - I have socket.io stuff and incoming data is printed out and that is breaking the prompts.
When setting a field to hidden: true
the input is not hidden when typing.
Tested on OSX.
For some reason you are making my prompt grey when I don't want it to. This should be up to the user to choose.
Reference: https://github.com/flatiron/prompt/blob/master/lib/prompt.js#L392
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.