Comparing version 1.0.0 to 1.1.0
@@ -7,2 +7,3 @@ /* | ||
var mod_extsprintf = require('extsprintf'); | ||
var mod_os = require('os'); | ||
var mod_path = require('path'); | ||
@@ -19,2 +20,3 @@ | ||
exports.exitOnEpipe = exitOnEpipe; | ||
exports.confirm = confirm; | ||
@@ -124,1 +126,78 @@ var usageMessage = null; | ||
} | ||
/* | ||
* See README.md. | ||
*/ | ||
function confirm(args, callback) | ||
{ | ||
var cstate; | ||
mod_assertplus.object(args, 'args'); | ||
mod_assertplus.string(args.message, 'args.message'); | ||
/* | ||
* This object encapsulates the state of this asynchronous operation. | ||
*/ | ||
cstate = { | ||
/* prompt message */ | ||
'cf_message': args.message, | ||
/* streams used for output and input */ | ||
'cf_outstream': process.stdout, | ||
'cf_instream': process.stdin, | ||
/* input is a tty */ | ||
'cf_intty': null, | ||
'cf_inraw': null, | ||
/* data read from input */ | ||
'cf_read': null, | ||
/* user callback to be invoked */ | ||
'cf_callback': callback | ||
}; | ||
cstate.cf_intty = cstate.cf_instream.isTTY; | ||
if (cstate.cf_intty) { | ||
cstate.cf_inraw = cstate.cf_instream.isRaw; | ||
if (!cstate.cf_inraw) { | ||
cstate.cf_instream.setRawMode(true); | ||
} | ||
} | ||
cstate.cf_outstream.write(cstate.cf_message); | ||
cstate.cf_read = cstate.cf_instream.read(1); | ||
if (cstate.cf_read === null) { | ||
cstate.cf_instream.once('readable', function () { | ||
cstate.cf_read = cstate.cf_instream.read(1); | ||
confirmFini(cstate); | ||
}); | ||
} else { | ||
setImmediate(confirmFini, cstate); | ||
} | ||
} | ||
function confirmFini(cstate) | ||
{ | ||
var result; | ||
cstate.cf_outstream.write(mod_os.EOL); | ||
if (cstate.cf_intty && !cstate.cf_inraw) { | ||
cstate.cf_instream.setRawMode(false); | ||
} | ||
/* | ||
* We assume that a "null" return from read() in the context of the | ||
* 'readable' event indicates end-of-stream, which we count as a | ||
* non-affirmative answer. There doesn't appear to be a programmatic | ||
* way to tell that this condition really means end-of-stream, though. | ||
* (Worse, it's not clear if there's any documented way to know if the | ||
* end-of-stream had been reached before we even started reading from | ||
* it. In that case, we may hang waiting for "readable" earlier. We | ||
* explicitly call this case out in the README.) | ||
*/ | ||
result = cstate.cf_read !== null && | ||
cstate.cf_read.toString('utf8').toLowerCase() == 'y'; | ||
cstate.cf_callback(result); | ||
} |
{ | ||
"name": "cmdutil", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "common command-line program library functions", | ||
@@ -5,0 +5,0 @@ "main": "./lib/cmdutil.js", |
@@ -12,2 +12,4 @@ # node-cmdutil: common command-line program functions | ||
default on most Unix-like systems. See details below. | ||
* [confirm](#confirm): print a message to stdout, read one byte of input from | ||
stdin, and test whether it appears affirmative | ||
@@ -31,8 +33,10 @@ You should also check out: | ||
The interfaces here follow [Joyent's Best Practices for Error Handling in | ||
Node.js](https://www.joyent.com/developers/node/design/errors). All of these | ||
interfaces are synchronous and there are no operational errors. The only | ||
possible errors are invalid arguments, which are programmer errors. These are | ||
thrown and should not be handled. | ||
Node.js](https://www.joyent.com/developers/node/design/errors). | ||
All of the functions here except for `confirm()` are synchronous. None of the | ||
functions in this module emit operational errors. The only possible errors are | ||
invalid arguments, which are programmer errors. These are thrown and should not | ||
be handled. | ||
## warn(...) | ||
@@ -373,2 +377,24 @@ | ||
## confirm(args, callback) | ||
`confirm(args, callback)` emits a message to stdout, waits for the user to input | ||
a single byte (in raw mode, if it's a TTY), and invokes `callback` with a | ||
boolean value indicating whether the confirmation was either "y" or "Y" (for | ||
"yes"). Any other response (including end-of-stream or a blank line) is | ||
considered false. | ||
The only supported argument inside `args` is: | ||
* `message`: the message to print to stdout (verbatim) | ||
`callback` is invoked as `callback(result)`. `result` is a boolean indicating | ||
whether the user input was affirmative. There are no operational errors for | ||
this function. Callers are expected to handle errors on stdin if desired. | ||
This function uses stdin and stdout directly, so callers should take care to | ||
avoid using it in contexts where that's not appropriate (e.g., in a program that | ||
reads data on stdin or produces formatted data on stdout). The behavior is | ||
undefined if stdin has already emitted 'end' when this function is called. | ||
# Contributions | ||
@@ -375,0 +401,0 @@ |
Sorry, the diff of this file is not supported yet
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
54451
27
232
407