Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c8ea14c239 | ||
|
|
1399a3ca9f | ||
|
|
079d94cbf3 | ||
|
|
4ec8aa630e | ||
|
|
91efb1bbf2 | ||
|
|
6763d727f5 |
4
CHANGELOG
Normal file
4
CHANGELOG
Normal file
@ -0,0 +1,4 @@
|
||||
v1.0.4 Handle cli forms in node.js
|
||||
* prompt user for input
|
||||
* async validation (email, url, etc)
|
||||
* status messages
|
||||
41
LICENSE
Normal file
41
LICENSE
Normal file
@ -0,0 +1,41 @@
|
||||
Copyright 2017 AJ ONeal
|
||||
|
||||
This is open source software; you can redistribute it and/or modify it under the
|
||||
terms of either:
|
||||
|
||||
a) the "MIT License"
|
||||
b) the "Apache-2.0 License"
|
||||
|
||||
MIT License
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
Apache-2.0 License Summary
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
61
index.js
61
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) {
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "terminal-forms.js",
|
||||
"version": "1.0.2",
|
||||
"version": "1.0.4",
|
||||
"description": "A terminal input library for node.js JavaScript",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
@ -8,7 +8,7 @@
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git@git.daplie.com:OAuth3/terminal-forms.js.git"
|
||||
"url": "git@git.oauth3.org:OAuth3/terminal-forms.js.git"
|
||||
},
|
||||
"keywords": [
|
||||
"terminal",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user