diff --git a/index.js b/index.js index a58cffa..20b974a 100644 --- a/index.js +++ b/index.js @@ -37,6 +37,7 @@ var form = { , _prompt: '' , _input: [] , _inputIndex: 0 + , isTTY: rws.isTTY , cursorTo: function (x, y) { if ('number' !== typeof x || (0 !== x && !x)) { throw new Error('cursorTo(x[, y]): x is not optional and must be a number'); @@ -169,15 +170,6 @@ var form = { return new PromiseA(function (resolve) { var ch; - - rrs.setRawMode(true); - rrs.setEncoding('utf8'); - rrs.resume(); - - ws.cursorTo(0); - ws.write(ws._prompt); - //ws.cursorTo(0, ws._prompt.length); - var debouncer = { set: function () { if (!(cbs.onDebounceAsync||cbs.onDebounce)) { @@ -224,10 +216,12 @@ var form = { rrs.pause(); form.PromiseA.resolve((cbs.onReturnAsync||cbs.onReturn)(rrs, ws, ws._input.join(''), ch)).then(function (normalInput) { - ws.write('\n'); - ws.clearLine(); // person just hit enter, they are on the next line - // (and this will clear the status, if any) - // TODO count lines used below and clear all of them + if (!cbs.value) { + ws.write('\n'); + ws.clearLine(); // person just hit enter, they are on the next line + // (and this will clear the status, if any) + // TODO count lines used below and clear all of them + } rrs.setRawMode(false); var input = ws._input.join(''); @@ -235,11 +229,14 @@ var form = { ws._inputIndex = 0; resolve({ input: input, result: normalInput }); }, function (err) { - rrs.on('data', onData); - var errmsg = colors.red(err.message); form.setStatus(rrs, ws, errmsg); + if (!rrs.isTTY) { + return PromiseA.reject(err); + } + rrs.on('data', onData); + rrs.setRawMode(true); rrs.resume(); }); } @@ -249,8 +246,6 @@ var form = { var x; if (CTRL_C === ch) { - console.log(""); - console.log("received CTRL+C and quit"); process.exit(0); callback(new Error("cancelled")); } @@ -315,6 +310,26 @@ var form = { // will come in and we have to figure out what to do about that } + if (cbs.value) { + ws._input = require('spliddit')(cbs.value); + ws._inputIndex = ws._input.length; + callback(); + return; + } + + if (!rrs.isTTY) { + return PromiseA.reject("User input is required but stdio is not a TTY"); + } + + rrs.setRawMode(true); + rrs.setEncoding('utf8'); + rrs.resume(); + + if (ws.isTTY) { + ws.cursorTo(0); + ws.write(ws._prompt); + //ws.cursorTo(0, ws._prompt.length); + } rrs.on('data', onData); }); } @@ -326,13 +341,17 @@ var form = { // TODO write newline? //ws.moveCursor(0, 1); ws.write('\n'); - ws.clearLine(); - ws.cursorTo(0); + if (ws.isTTY) { + ws.clearLine(); + ws.cursorTo(0); + } // write from beginning of line ws.write(msg); // restore position - ws.cursorTo(x); - ws.moveCursor(0, -1); + if (ws.isTTY) { + ws.cursorTo(x); + ws.moveCursor(0, -1); + } } , println: function (rrs, ws, msg) {