diff options
author | isaacs <i@izs.me> | 2012-07-24 01:49:09 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2012-07-24 01:49:09 +0400 |
commit | 805c70b17d62a78e43b6d2c6b464158f69680419 (patch) | |
tree | 5ad50006b631fa2b784c53911c7faed0fac65436 /node_modules/read | |
parent | 72161134385ab2d98e5a99c1f85167960b81181d (diff) |
read@0.1.1
Diffstat (limited to 'node_modules/read')
-rw-r--r-- | node_modules/read/.npmignore | 2 | ||||
-rw-r--r-- | node_modules/read/README.md | 4 | ||||
-rw-r--r-- | node_modules/read/lib/read.js | 51 | ||||
-rw-r--r-- | node_modules/read/package.json | 6 |
4 files changed, 38 insertions, 25 deletions
diff --git a/node_modules/read/.npmignore b/node_modules/read/.npmignore new file mode 100644 index 000000000..0db216bfa --- /dev/null +++ b/node_modules/read/.npmignore @@ -0,0 +1,2 @@ +npm-debug.log +node_modules diff --git a/node_modules/read/README.md b/node_modules/read/README.md index 0c90e1445..2c19d1401 100644 --- a/node_modules/read/README.md +++ b/node_modules/read/README.md @@ -20,9 +20,11 @@ Every option is optional. * `num` Max number of chars to read from terminal. * `timeout` Number of ms to wait for user input before giving up. * `default` The default value if the user enters nothing. +* `stdin` Readable stream to get input data from. (default `process.stdin`) +* `stdout` Writeable stream to write prompts to. (default: `process.stdout`) If silent is true, or num is set, and the input is a TTY, -then read will set raw mode, and read character by character. +then read will set raw mode, and read character by character. At this time, backspace and arrow keys are not supported very well. It's probably not too hard to add support for this, perhaps using node's diff --git a/node_modules/read/lib/read.js b/node_modules/read/lib/read.js index eb00fd5c6..9f3edb57f 100644 --- a/node_modules/read/lib/read.js +++ b/node_modules/read/lib/read.js @@ -4,12 +4,12 @@ module.exports = read var buffer = "" , tty = require("tty") , StringDecoder = require("string_decoder").StringDecoder + , stdin + , stdout -function raw (mode) { - if (process.stdin.setRawMode) { - if (process.stdin.isTTY) { - process.stdin.setRawMode(mode) - } +function raw (stdin, mode) { + if (stdin.setRawMode && stdin.isTTY) { + stdin.setRawMode(mode) return } // old style @@ -28,19 +28,22 @@ function read (opts, cb) { , timeout = opts.timeout , num = opts.num || null + stdin = opts.stdin || process.stdin + stdout = opts.stdout || process.stdout + if (p && def) p += "("+(silent ? "<default hidden>" : def)+") " - + // switching into raw mode is a little bit painful. // avoid if possible. var r = silent || num ? rawRead : normalRead - if (r === rawRead && !process.stdin.isTTY) r = normalRead + if (r === rawRead && !stdin.isTTY) r = normalRead if (timeout) { cb = (function (cb) { var called = false var t = setTimeout(function () { raw(false) - process.stdout.write("\n") + stdout.write("\n") if (def) done(null, def) else done(new Error("timeout")) }, timeout) @@ -58,9 +61,9 @@ function read (opts, cb) { })(cb) } - if (p && !process.stdout.write(p)) { - process.stdout.on("drain", function D () { - process.stdout.removeListener("drain", D) + if (p && !stdout.write(p)) { + stdout.on("drain", function D () { + stdout.removeListener("drain", D) r(def, timeout, silent, num, cb) }) } else { @@ -71,10 +74,13 @@ function read (opts, cb) { } function normalRead (def, timeout, silent, num, cb) { - var stdin = process.openStdin() - , val = "" + var val = "" , decoder = new StringDecoder("utf8") + if (stdin === process.stdin) { + stdin = process.openStdin() + } + stdin.resume() stdin.on("error", cb) stdin.on("data", function D (chunk) { @@ -121,11 +127,14 @@ function normalRead (def, timeout, silent, num, cb) { } function rawRead (def, timeout, silent, num, cb) { - var stdin = process.openStdin() - , val = "" + var val = "" , decoder = new StringDecoder - raw(true) + if (stdin === process.stdin) { + stdin = process.openStdin() + } + + raw(stdin, true) stdin.resume() stdin.on("error", cb) stdin.on("data", function D (c) { @@ -136,21 +145,21 @@ function rawRead (def, timeout, silent, num, cb) { LOOP: while (c = s.charAt(i++)) switch (c) { case "\u007f": // backspace val = val.substr(0, val.length - 1) - if (!silent) process.stdout.write('\b \b') + if (!silent) stdout.write('\b \b') break case "\u0004": // EOF case "\n": - raw(false) + raw(stdin, false) stdin.removeListener("data", D) stdin.removeListener("error", cb) val = val.trim() || def - process.stdout.write("\n") + stdout.write("\n") stdin.pause() return cb(null, val) case "\u0003": case "\0": // ^C or other signal abort - raw(false) + raw(stdin, false) stdin.removeListener("data", D) stdin.removeListener("error", cb) stdin.pause() @@ -159,7 +168,7 @@ function rawRead (def, timeout, silent, num, cb) { default: // just a normal char val += buffer + c buffer = "" - if (!silent) process.stdout.write(c) + if (!silent) stdout.write(c) // explicitly process a delim if we have enough chars // and stop the processing. diff --git a/node_modules/read/package.json b/node_modules/read/package.json index 8057e4493..1f3e00a96 100644 --- a/node_modules/read/package.json +++ b/node_modules/read/package.json @@ -1,6 +1,6 @@ { "name": "read", - "version": "0.1.0", + "version": "0.1.1", "main": "lib/read.js", "dependencies": {}, "devDependencies": { @@ -23,7 +23,7 @@ "scripts": { "test": "tap test/*.js" }, - "readme": "For reading user input from stdin.\n\n## USAGE\n\n```javascript\nvar read = require(\"read\")\nread(options, callback)\n```\n\nThe callback gets called with either the user input, or the default\nspecified, or an error, in the traditional `callback(error, result)`\nnode style.\n\n## OPTIONS\n\nEvery option is optional.\n\n* `prompt` What to write to stdout before reading input.\n* `silent` Don't echo the output as the user types it.\n* `num` Max number of chars to read from terminal.\n* `timeout` Number of ms to wait for user input before giving up.\n* `default` The default value if the user enters nothing.\n\nIf silent is true, or num is set, and the input is a TTY,\nthen read will set raw mode, and read character by character.\n\nAt this time, backspace and arrow keys are not supported very well.\nIt's probably not too hard to add support for this, perhaps using node's\nbuilt-in readline module.\n\n## CONTRIBUTING\n\nPatches welcome.\n\n## BUGS\n\nIn node 0.6.0 through 0.6.5, you must explicitly call\n`process.stdin.destroy()` or `process.exit()` when you know that your\nprogram is done reading, or else it will keep the event loop running\nforever.\n\nSee: <https://github.com/joyent/node/issues/2257>\n", - "_id": "read@0.1.0", + "readme": "For reading user input from stdin.\n\n## USAGE\n\n```javascript\nvar read = require(\"read\")\nread(options, callback)\n```\n\nThe callback gets called with either the user input, or the default\nspecified, or an error, in the traditional `callback(error, result)`\nnode style.\n\n## OPTIONS\n\nEvery option is optional.\n\n* `prompt` What to write to stdout before reading input.\n* `silent` Don't echo the output as the user types it.\n* `num` Max number of chars to read from terminal.\n* `timeout` Number of ms to wait for user input before giving up.\n* `default` The default value if the user enters nothing.\n* `stdin` Readable stream to get input data from. (default `process.stdin`)\n* `stdout` Writeable stream to write prompts to. (default: `process.stdout`)\n\nIf silent is true, or num is set, and the input is a TTY,\nthen read will set raw mode, and read character by character.\n\nAt this time, backspace and arrow keys are not supported very well.\nIt's probably not too hard to add support for this, perhaps using node's\nbuilt-in readline module.\n\n## CONTRIBUTING\n\nPatches welcome.\n\n## BUGS\n\nIn node 0.6.0 through 0.6.5, you must explicitly call\n`process.stdin.destroy()` or `process.exit()` when you know that your\nprogram is done reading, or else it will keep the event loop running\nforever.\n\nSee: <https://github.com/joyent/node/issues/2257>\n", + "_id": "read@0.1.1", "_from": "read@0" } |