New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example for "Questions" #367
Comments
Its almost the same code for the comment box on the website. |
If you have just 2 questions (user and github) you can do nested push, if you have more it would probably be better to do something like in comments, where you have list of prompts in array and you construct the object: var init = true;
$('body').terminal(function(command, term) {
term.echo(new Date(), {
finalize: function(node) {
if (init) {
// node is jquery object and html have data-index attribute
var index = node.data('index');
setInterval(function() {
term.update(index, new Date());
}, 1000);
init = false;
}
}
});
term.push(function(username) {
term.push(function(github) {
term.pause();
// do some async processing like ajax call
setTimeout(function() {
term.echo('Some data').resume().push(function(yn) {
if (yn.match(/^(y|n)$/i)) {
if (yn.match(/y/i)) {
// YES
} else {
// NO
}
term.pop().pop().pop(); // exit from all pushed interpreters
// if you will want to enable some different commands to github
// you can do term.push here
} else {
// invalid, you can ignore, it will ask again
// or you can do term.pop() x 3 if you don't want that
}
}, {
prompt: 'Confirm (y|n): '
});
}, 1000);
}, {
prompt: 'github: '
});
}, {
prompt: 'username: '
});
}); if you want this to be on init you can move the code from inside terminal to onInit or after terminal is created. |
If you want to ask multiple questions you can take a look at leash-src.js#L705 there is array of specs and it process all questions and options and create a single object with all the settings. |
Wow, one can really tell you love your project ! |
I came up with something like this: var init = true;
term.echo(new Date(), {
finalize: function(node) {
if (init) {
var index = node.data('index');
setInterval(function() {
term.update(index, new Date());
}, 1000);
init = false;
}
}
});
var mask = ['root_password', 'password'];
var settings = {};
var questions = [
{
name: "root_password",
text: 'Enter your administration password',
prompt: "root password: "
},
{
name: "server",
text: "Type your server name"
},
{
name: "username",
text: "Your normal username"
},
{
name: "home",
text: "Home directory"
},
{
name: 'guest',
text: 'Allow guest sessions (Y)es/(N)o',
boolean: true
},
{
name: 'sudo',
text: 'Execute sudo for user accounts (Y)es/(N)o',
boolean: true
},
{
name: "password"
}
];
function ask_questions(step) {
var question = questions[step];
if (question) {
if (question.text) {
term.echo('[[b;#fff;]' + question.text + ']');
}
var show_mask = mask.indexOf(question.name) != -1;
term.push(function(command) {
if (show_mask) {
term.set_mask(false);
}
if (question.boolean) {
var value;
if (command.match(/^Y(es)?/i)) {
value = true;
} else if (command.match(/^N(o)?/i)) {
value = false;
}
if (typeof value != 'undefined') {
settings[question.name] = value;
term.pop();
ask_questions(step+1);
}
} else {
settings[question.name] = command;
term.pop();
ask_questions(step+1);
}
}, {
prompt: question.prompt || question.name + ": "
});
// set command and mask need to called after push
// otherwise they will not work
if (show_mask) {
term.set_mask(true);
}
if (typeof settings[question.name] != 'undefined') {
if (typeof settings[question.name] == 'boolean') {
term.set_command(settings[question.name] ? 'y' : 'n');
} else {
term.set_command(settings[question.name]);
}
}
} else {
finish();
}
}
function finish() {
term.echo('Your settings:');
var str = Object.keys(settings).map(function(key) {
var value = settings[key];
if (mask.indexOf(key) != -1) {
// mask everything except first and last character
var split = value.match(/^(.)(.*)(.)$/, '*');
value = split[1] + split[2].replace(/./g, '*') + split[3];
}
return '[[b;#fff;]' + key + ']: ' + value;
}).join('\n');
term.echo(str);
term.push(function(command) {
if (command.match(/^y$/i)) {
term.echo(JSON.stringify(settings));
term.pop().history().enable();
} else if (command.match(/^n$/i)) {
term.pop();
ask_questions(0);
}
}, {
prompt: 'Are those correct (y|n): '
});
}
term.history().disable();
ask_questions(0); |
Here's my solution
|
Works fine by me. That could be added to the example page, if you are fine with that. |
My code that I've posted in previous comment show settings you have so far and allow to edit them so if I'll put it in examples it will be my updated code. Did you see it? |
Yes I saw it, but I havent tested it. I'm sorry. Sure, you can add your example as it seems its more advanced. Maybe add a link to this issue talk as a backlink? I'm gonna test your script, but I have not the time right now. |
I've added my updated code to the examples. |
Is it possible to have this example added:
Afterwards I want to use that user given information for ie. a HTTP form.
The text was updated successfully, but these errors were encountered: