Permalink
Browse files

Remove delim argument

  • Loading branch information...
1 parent 6f8101c commit f9b61afe3e15a82ca44bf270a00fe9c1865f9781 @isaacs isaacs committed Jun 14, 2012
Showing with 14 additions and 19 deletions.
  1. +3 −4 README.md
  2. +11 −15 lib/read.js
View
@@ -18,14 +18,13 @@ Every option is optional.
* `prompt` What to write to stdout before reading input.
* `silent` Don't echo the output as the user types it.
* `num` Max number of chars to read from terminal.
-* `delim` The char that means we're done. Default: `"\n"`
* `timeout` Number of ms to wait for user input before giving up.
* `default` The default value if the user enters nothing.
-If silent is true, or num is set, or delim is something other than
-`"\n"`, then read will set raw mode, and read character by character.
+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.
-At this time, backspace and arrow keys are not supported in raw mode.
+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
built-in readline module.
View
@@ -21,13 +21,12 @@ function read (opts, cb) {
, silent = opts.silent
, timeout = opts.timeout
, num = opts.num || null
- , delim = opts.delim || "\n"
if (p && def) p += "("+(silent ? "<default hidden>" : def)+") "
// switching into raw mode is a little bit painful.
// avoid if possible.
- var r = silent || num || delim !== "\n" ? rawRead : normalRead
+ var r = silent || num ? rawRead : normalRead
if (timeout) {
cb = (function (cb) {
@@ -55,16 +54,16 @@ function read (opts, cb) {
if (p && !process.stdout.write(p)) {
process.stdout.on("drain", function D () {
process.stdout.removeListener("drain", D)
- r(def, timeout, delim, silent, num, cb)
+ r(def, timeout, silent, num, cb)
})
} else {
process.nextTick(function () {
- r(def, timeout, delim, silent, num, cb)
+ r(def, timeout, silent, num, cb)
})
}
}
-function normalRead (def, timeout, delim, silent, num, cb) {
+function normalRead (def, timeout, silent, num, cb) {
var stdin = process.openStdin()
, val = ""
, decoder = new StringDecoder("utf8")
@@ -77,23 +76,20 @@ function normalRead (def, timeout, delim, silent, num, cb) {
buffer = ""
// \r has no place here.
- // XXX But what if \r is the delim or something dumb like that?
- // Meh. If anyone complains about this, deal with it.
val = val.replace(/\r/g, "")
- // TODO Make delim configurable
- if (val.indexOf(delim) !== -1) {
+ if (val.indexOf("\n") !== -1) {
// pluck off any delims at the beginning.
- if (val !== delim) {
+ if (val !== "\n") {
var i, l
for (i = 0, l = val.length; i < l; i ++) {
- if (val.charAt(i) !== delim) break
+ if (val.charAt(i) !== "\n") break
}
if (i !== 0) val = val.substr(i)
}
// buffer whatever might have come *after* the delimter
- var delimIndex = val.indexOf(delim)
+ var delimIndex = val.indexOf("\n")
if (delimIndex !== -1) {
buffer = val.substr(delimIndex)
val = val.substr(0, delimIndex)
@@ -112,7 +108,7 @@ function normalRead (def, timeout, delim, silent, num, cb) {
})
}
-function rawRead (def, timeout, delim, silent, num, cb) {
+function rawRead (def, timeout, silent, num, cb) {
var stdin = process.openStdin()
, val = ""
, decoder = new StringDecoder
@@ -132,7 +128,7 @@ function rawRead (def, timeout, delim, silent, num, cb) {
break
case "\u0004": // EOF
- case delim:
+ case "\n":
raw(false)
stdin.removeListener("data", D)
stdin.removeListener("error", cb)
@@ -155,7 +151,7 @@ function rawRead (def, timeout, delim, silent, num, cb) {
// explicitly process a delim if we have enough chars
// and stop the processing.
- if (num && val.length >= num) D(delim)
+ if (num && val.length >= num) D("\n")
break LOOP
}
})

0 comments on commit f9b61af

Please sign in to comment.