From 6763d727f5081ae1642c643504058a0830099ba3 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sat, 25 Mar 2017 20:17:51 -0600 Subject: [PATCH 1/2] accept and use a preset value --- index.js | 58 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index a58cffa..ad5c29f 100644 --- a/index.js +++ b/index.js @@ -169,15 +169,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 +215,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 +228,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(); }); } @@ -315,6 +311,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 +342,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) { From 91efb1bbf2ab8f4db86f3e93a5c82bf541f1cc67 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sun, 26 Mar 2017 01:38:23 -0600 Subject: [PATCH 2/2] bugfix for non-TTY case --- index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index ad5c29f..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'); @@ -245,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")); } @@ -313,7 +312,7 @@ var form = { if (cbs.value) { ws._input = require('spliddit')(cbs.value); - ws._inputIndex = ws.input.length; + ws._inputIndex = ws._input.length; callback(); return; }