Skip to content

Instantly share code, notes, and snippets.

@pachanka
Created March 7, 2018 10:46
Show Gist options
  • Save pachanka/82cb4d783abcac61ba9897290a0a3941 to your computer and use it in GitHub Desktop.
Save pachanka/82cb4d783abcac61ba9897290a0a3941 to your computer and use it in GitHub Desktop.
Ptty javascript terminal emulator command example with data prompt.
/**
*
* This example uses ptty to create a terminal emulator and
* a command with prompt that will ask three questions.
*
* More Info at: http://goto.pachanka.org/ptty/docs/#set_command_option
*
* To Adapt this function to your needs:
*
* - Edit the cmd_lead.command.options array
* - Edit the cmd_lead.callbefore.name and cmd_lead.command.name values.
* - Add a case to the switch or remove as needed.
* - On every switch case, edit the cmd_opt.next to match your options.
* - Change cmd_lead.command.method to a url string to use AJAX.
*
**/
// Let's create this object with a
// "callbefore" and a "command" property.
var cmd_lead = {
// The callbefore will loop over itself sending data through
// cmd_opt.next and $ptty.set_command_option(obj).
callbefore : {
name: 'lead',
method: function(cmd){
var cmd_opt = {};
var cmd_len = Object.keys(cmd).length;
// Validation
if( cmd.hasOwnProperty('-last')
&& cmd['-last'].toLowerCase() === 'potato'){
cmd_len = 4; // Error!
cmd_opt.out = "We don't like Mr Potato";
}
switch(cmd_len){
case 0:
// First question
cmd_opt.next = 'lead -first "%cmd%"';
cmd_opt.ps = "First name";
cmd_opt.out = "Please enter your first name or hit escape to exit.";
// Setting cmd to false exits the command
// so the command (where the ajax goes)
// is never called.
cmd = false;
break;
case 1:
// Second question
cmd_opt.next = 'lead -first "'+cmd['-first']+'" -last "%cmd%"';
cmd_opt.ps = "Last name";
cmd_opt.out = "Please enter your last name.";
cmd = false;
break;
case 2:
// Third question
cmd_opt.next = 'lead -first "'+cmd['-first']+'" -last "'+cmd['-last']+'" -phone "%cmd%"';
cmd_opt.ps = "Phone";
cmd_opt.out = "Please enter your phone number.";
cmd = false;
break;
case 3:
// This will be passed to the main command
cmd.data = {
first : cmd['-first'],
last : cmd['-last'],
phone : cmd['-phone']
};
// We are done so set cmd_opt to false
cmd_opt = false;
cmd.out = 'Thank you for your information.';
break;
default:
// End program in callbefore
cmd_opt.out = cmd_opt.out || 'Error!';
cmd = false;
break;
}
if(cmd_opt){
$ptty.set_command_option(cmd_opt);
}
return cmd;
}
},
// The main lead command receives all the data from
// the callbefore, that we called before... Before.
command : {
name: 'lead',
// Change the method to a URL string to do an AJAX call.
method: function(cmd){
cmd.out += '<br>first: '+cmd.data.first;
cmd.out += '<br>last: '+cmd.data.last;
cmd.out += '<br>phone: '+cmd.data.phone;
cmd.out += '<br>(AJAX) call maybe?';
return cmd;
},
options : ['-first','-last','-phone'],
help: 'Collects data by prompting for input.',
}
}
// Now we register the commands using Ptty.
$ptty.register('command', cmd_lead.command);
$ptty.register('callbefore', cmd_lead.callbefore);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment