Comparing version 0.1.3 to 0.1.4
67
main.js
/*jslint node: true, nomen: true, vars: true, plusplus: true*/ | ||
'use strict'; | ||
// DEPENDENCIES | ||
var util = require('util'); | ||
// GLOBALS | ||
var stdin = process.stdin; | ||
var MAX_PROMPT_TRIES = 3; | ||
// MAIN LOGIC | ||
function preprocess (argv) { | ||
@@ -281,11 +290,13 @@ | ||
var inputdata = ''; | ||
var inputdata; | ||
stdin.resume(); | ||
process.stdin.resume(); | ||
process.stdin.on('data', function (text) { | ||
var listener = function (text) { | ||
inputdata += String(text); | ||
}); | ||
}; | ||
process.stdin.on('end', function () { | ||
stdin.on('data', listener); | ||
stdin.on('end', function () { | ||
stdin.removeListener('data', listener); | ||
callback(inputdata); | ||
@@ -296,9 +307,43 @@ }); | ||
/** | ||
* Printf-like output | ||
* @param {String} format (use %s, %d or %j) | ||
* @param {*} arguments | ||
* Shows a prompt question with some possible answers | ||
* @param {string} question Question to show | ||
* @param {array} options Possible answers | ||
* @param {function} callback Function to call with user response (err, response) | ||
*/ | ||
module.exports.printf = function () { | ||
module.exports.question = function (question, options, callback) { | ||
process.stdout.write(util.format.apply(this, arguments)); | ||
if (!question || !Array.isArray(options) || options.length < 2) { | ||
throw new Error('Stdio questions have to be created providing a question and two or more possible answers'); | ||
} | ||
var tries = MAX_PROMPT_TRIES; | ||
var performQuestion = function () { | ||
process.stdout.write(question + ' [' + options.join('/') + ']: '); | ||
} | ||
stdin.resume(); | ||
var listener = function (data) { | ||
var response = data.toString().toLowerCase().trim(); | ||
if (options.indexOf(response) === -1) { | ||
console.log('Unexpected answer'); | ||
tries--; | ||
if (tries === 0) { | ||
stdin.removeListener('data', listener); | ||
callback('Retries spent'); | ||
} else { | ||
performQuestion(); | ||
} | ||
return; | ||
} | ||
stdin.removeListener('data', listener); | ||
callback(false, response); | ||
}; | ||
stdin.addListener('data', listener); | ||
performQuestion(); | ||
}; |
{ | ||
"name": "stdio", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Module for standard input/output management with NodeJS", | ||
@@ -5,0 +5,0 @@ "keywords": ["input", "console", "output", "terminal", "system"], |
@@ -86,2 +86,4 @@ Module for input/output management with nodejs. | ||
This simple following code will read the whole standard input. | ||
```javascript | ||
@@ -94,17 +96,20 @@ var stdio = require('stdio'); | ||
### 2.3. Printf-like output | ||
Obviously it is not recommended for huge input files. | ||
This simple line: | ||
### 2.3. Show prompt questions and wait user's answer | ||
```javascript | ||
stdio.printf('example %d: %s is %j\n', 2, 'any', {a: 2, b: [0, 2, 8], c: 'str'}); | ||
var stdio = require('stdio'); | ||
stdio.question('This is a question?', ['y', 'n'], function (err, answer) { | ||
// Use answer here | ||
}); | ||
``` | ||
will produce the following output: | ||
The previous code will show something like the following: | ||
``` | ||
example 2: any is {"a":2,"b":[0,2,8],"c":"str"} | ||
``` | ||
```` | ||
This is a question? [y/n]: | ||
```` | ||
You can use `%s` for strings, `%d` for numbers (integer or floating-point ones), and `%j` for JSON objects. | ||
and waits until user enters an answer. There will be 3 retries before reporting an error by mean of the callback. | ||
@@ -121,2 +126,7 @@ ## 3. Testing | ||
### 0.1.4 | ||
* New fancy feature! Now you can show simple prompts to interact with users by mean of a question. | ||
* Old printf-like feature has been removed. | ||
### 0.1.3 | ||
@@ -123,0 +133,0 @@ |
@@ -8,3 +8,3 @@ /*jslint node: true, nomen: true*/ | ||
// Dependences ================================================================= | ||
// Dependencies ================================================================= | ||
@@ -170,2 +170,1 @@ var stdio = require('../main.js'), | ||
}()); | ||
# TODO | ||
- [ ] Support interactive input/output, like [Y/y/n] questions. | ||
- [ ] Support a list of many arguments for an option (without a fixed length). {..., args: '*'} | ||
@@ -17,1 +16,2 @@ - [ ] Support enumerated options. Help must say something like --whatever <something|another|last> | ||
- [X] Support large options definition using equals: --something=myvalue | ||
- [X] Support interactive input/output, like [Y/y/n] questions. |
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
2925439
12
541
156