Replies: 1 comment 1 reply
-
I have made some progress. Learned a lot about async promises. I came with the next code: You are allowed to add this to your samples, #!/usr/bin/env node
/*
Terminal Kit
Copyright (c) 2009 - 2021 Cédric Ronvel
The MIT License (MIT)
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.
*/
"use strict";
/*jshint esversion: 6 */
/* jshint unused:false */
const termkit = require('../lib/termkit.js');
var term = termkit.terminal;
term.grabInput();
term.on('key', function (key, x, y, z) {
switch (key) {
case 'CTRL_C':
terminate();
break;
}
});
async function checkBadgeID(code) {
return true;
}
async function asyncQuestion() {
term.green("A new commBadge has requested to join this switchboard !\n");
term('Do you want to add this new device now? [Y|n] ');
if (! await term.yesOrNo({ yes: ['y', 'ENTER', 'j', 'J', 'Y'], no: ['n', 'ESCAPE'] }).promise) {
return false;
}
term.green("Yes\n\nTo continue you need the commBadge ID code shown on the display !\n");
term.green("NOTE: this code is case sensitive and exist of 8 digids.\n");
term.green("To stop: Leave the code below blank. \n");
var input1 = '';
while (true) {
term('Enter the given commBadge ID here : ');
input1 = await term.inputField({ cancelable: true }).promise;
if (typeof input1 === "undefined" || (input1.trim() === "")) return false;
let valid = await checkBadgeID(input1.trim());
if (valid) break;
term.red("\nThe given badge ID is not a valid one. try again.\n\n");
}
term.green("\n\nEnter now the call name of the the badge user . \n");
term.green("This will be used when someone call's hem uaing there own badge. \n");
term.green("To stop: Leave the name below blank. \n");
term('Enter username of this badge : ');
var input2 = await term.inputField({ cancelable: true }).promise;
if (input1 === undefined || input2.trim() === "") return false;
term.yellow("\n\nAll done, thanks. The badge will now be initialisated with the above info. \n");
return [input1, input2];
}
asyncQuestion().then(alert => { console.info(alert); term.processExit(); });
function terminate() {
term.grabInput(false);
// Add a 100ms delay, so the terminal will be ready when the process effectively exit, preventing bad escape sequences drop
setTimeout(function () { term.processExit(); }, 100);
} maybe not the best method but it works for now. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi @cronvel,
sorry to bug you, i would love to do it myself but i'm in a bit of a hurry working on other parts of my project and you know more about the terminal-kit then i do right now.
I need a temporary solution where a user will get prompt of he wants to add a new device in his network. when (s)he do's (using a yes/no question, thats the easy part). (S)he needs to ender a security code that is given by the device and it a device name chosen by that person.
The yes/no part i have already although i like to add a timer to it that when not responding on time the questions are skipped and will be requested at on other time again.
For the input-field tests, none seems to run, and look al fancy with autoComplete and history etc. and that's not what i needed.
As i said it is temporally, the main goal is to go the "Document" way. But i dont have time yet to set that up. because other task are more imported right now.
I hope you have time for me to do this. You can make an example from it as well.
Then others has also something to look at.
Thanks anyway.
Beta Was this translation helpful? Give feedback.
All reactions