You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<motd>
Current time <current datetime> (maybe not just print, but self updating)
> Name: <wait for input>
(next)
> Github: <wait for input>
(next)
//> ...
> Information valid?
> <output information from above>
> Confirm (y|n): <wait for input>
Afterwards I want to use that user given information for ie. a HTTP form.
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:
varinit=true;$('body').terminal(function(command,term){term.echo(newDate(),{finalize: function(node){if(init){// node is jquery object and html have data-index attributevarindex=node.data('index');setInterval(function(){term.update(index,newDate());},1000);init=false;}}});term.push(function(username){term.push(function(github){term.pause();// do some async processing like ajax callsetTimeout(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 !
One thing I dont get is, how am I suppose to restart the process if everything is in anonymous functions?
Like the given data is wrong and in the confirmation step the user declines the confirmation.
varinit=true;term.echo(newDate(),{finalize: function(node){if(init){varindex=node.data('index');setInterval(function(){term.update(index,newDate());},1000);init=false;}}});varmask=['root_password','password'];varsettings={};varquestions=[{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"}];functionask_questions(step){varquestion=questions[step];if(question){if(question.text){term.echo('[[b;#fff;]'+question.text+']');}varshow_mask=mask.indexOf(question.name)!=-1;term.push(function(command){if(show_mask){term.set_mask(false);}if(question.boolean){varvalue;if(command.match(/^Y(es)?/i)){value=true;}elseif(command.match(/^N(o)?/i)){value=false;}if(typeofvalue!='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 workif(show_mask){term.set_mask(true);}if(typeofsettings[question.name]!='undefined'){if(typeofsettings[question.name]=='boolean'){term.set_command(settings[question.name] ? 'y' : 'n');}else{term.set_command(settings[question.name]);}}}else{finish();}}functionfinish(){term.echo('Your settings:');varstr=Object.keys(settings).map(function(key){varvalue=settings[key];if(mask.indexOf(key)!=-1){// mask everything except first and last charactervarsplit=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();}elseif(command.match(/^n$/i)){term.pop();ask_questions(0);}},{prompt: 'Are those correct (y|n): '});}term.history().disable();ask_questions(0);
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.
Activity
pquerner commentedon Feb 15, 2018
Its almost the same code for the comment box on the website.
jcubic commentedon Feb 15, 2018
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:
if you want this to be on init you can move the code from inside terminal to onInit or after terminal is created.
jcubic commentedon Feb 15, 2018
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.
pquerner commentedon Feb 16, 2018
Wow, one can really tell you love your project !
One thing I dont get is, how am I suppose to restart the process if everything is in anonymous functions?
Like the given data is wrong and in the confirmation step the user declines the confirmation.
jcubic commentedon Feb 17, 2018
I came up with something like this:
pquerner commentedon Feb 20, 2018
Here's my solution
settingsandquestionsare build like you proposed, so I removed them from the example.pquerner commentedon Feb 20, 2018
Works fine by me. That could be added to the example page, if you are fine with that.
jcubic commentedon Feb 20, 2018
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?
pquerner commentedon Feb 20, 2018
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.
jcubic commentedon Feb 24, 2018
I've added my updated code to the examples.