Comparing version 0.1.0 to 0.1.1
@@ -7,8 +7,8 @@ | ||
, 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 | ||
@@ -32,8 +32,11 @@ } | ||
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 | ||
@@ -45,3 +48,3 @@ if (timeout) { | ||
raw(false) | ||
process.stdout.write("\n") | ||
stdout.write("\n") | ||
if (def) done(null, def) | ||
@@ -64,5 +67,5 @@ else done(new Error("timeout")) | ||
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) | ||
@@ -78,6 +81,9 @@ }) | ||
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() | ||
@@ -129,7 +135,10 @@ stdin.on("error", 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() | ||
@@ -145,3 +154,3 @@ stdin.on("error", cb) | ||
val = val.substr(0, val.length - 1) | ||
if (!silent) process.stdout.write('\b \b') | ||
if (!silent) stdout.write('\b \b') | ||
break | ||
@@ -151,7 +160,7 @@ | ||
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() | ||
@@ -161,3 +170,3 @@ return cb(null, val) | ||
case "\u0003": case "\0": // ^C or other signal abort | ||
raw(false) | ||
raw(stdin, false) | ||
stdin.removeListener("data", D) | ||
@@ -171,3 +180,3 @@ stdin.removeListener("error", cb) | ||
buffer = "" | ||
if (!silent) process.stdout.write(c) | ||
if (!silent) stdout.write(c) | ||
@@ -174,0 +183,0 @@ // explicitly process a delim if we have enough chars |
{ | ||
"name": "read", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"main": "lib/read.js", | ||
"dependencies": {}, | ||
"devDependencies": {"tap":"*"}, | ||
"devDependencies": { | ||
"tap": "*" | ||
}, | ||
"engines": { | ||
@@ -16,4 +18,6 @@ "node": ">=0.6" | ||
}, | ||
"license":"BSD", | ||
"scripts":{"test":"tap test/*.js"} | ||
"license": "BSD", | ||
"scripts": { | ||
"test": "tap test/*.js" | ||
} | ||
} |
@@ -23,5 +23,7 @@ For reading user input from stdin. | ||
* `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. | ||
@@ -28,0 +30,0 @@ At this time, backspace and arrow keys are not supported very well. |
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
9960
7
214
45