From f68bf22a795d1445aa6695b8497185ce3103a4f6 Mon Sep 17 00:00:00 2001 From: isaacs Date: Wed, 13 Jun 2012 18:13:25 -0700 Subject: [PATCH] Handle non-TTY inputs better --- lib/read.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/read.js b/lib/read.js index 394e051..eb00fd5 100644 --- a/lib/read.js +++ b/lib/read.js @@ -6,13 +6,19 @@ var buffer = "" , StringDecoder = require("string_decoder").StringDecoder function raw (mode) { + if (process.stdin.setRawMode) { + if (process.stdin.isTTY) { + process.stdin.setRawMode(mode) + } + return + } + // old style try { - process.stdin.setRawMode(mode) - } catch (e) { tty.setRawMode(mode) - } + } catch (e) {} } + function read (opts, cb) { if (!cb) cb = opts, opts = {} @@ -27,6 +33,7 @@ function read (opts, cb) { // 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 (timeout) { cb = (function (cb) { @@ -88,6 +95,11 @@ function normalRead (def, timeout, silent, num, cb) { if (i !== 0) val = val.substr(i) } + // hack. if we get the number of chars, just pretend there was a delim + if (num > 0 && val.length >= num) { + val = val.substr(0, num) + "\n" + val.substr(num) + } + // buffer whatever might have come *after* the delimter var delimIndex = val.indexOf("\n") if (delimIndex !== -1) {