Skip to content

Commit

Permalink
[refactor] Added stdin and stdout to options passable to read, …
Browse files Browse the repository at this point in the history
…defaulting to `process.stdin` and `process.stdout`. Update references to `process.stdin` and `process.stdout` to the locally scoped objects.
  • Loading branch information
indexzero committed Jul 7, 2012
1 parent 05c809b commit 1ae98df
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions lib/read.js
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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()
Expand All @@ -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.
Expand Down

0 comments on commit 1ae98df

Please sign in to comment.