Skip to content

Instantly share code, notes, and snippets.

@jbavari
Created March 10, 2015 22:59
Show Gist options
  • Save jbavari/116c777117d511d51d5e to your computer and use it in GitHub Desktop.
Save jbavari/116c777117d511d51d5e to your computer and use it in GitHub Desktop.
Quick prompt example
var q = require('q');
var askPrompt = function() {
var deferred = q.defer();
var prompt = require('prompt');
var promptProperties = {
selection: {
name: 'selection',
description: 'Address Selection: '.yellow.bold,
required: true
}
};
// prompt.override = argv;
prompt.message = '';
prompt.delimiter = '';
prompt.start();
prompt.get({properties: promptProperties}, function (err, promptResult) {
if(err) {
return console.log(err);
}
var selection = promptResult.selection;
console.log('selection:', selection)
// for(var x=0; x<addresses.length; x++) {
// if(selection == (x + 1) || selection == addresses[x].address || selection == addresses[x].dev) {
// self.address = addresses[x].address;
// if(!self.isAddressCmd) {
// console.log('Selected address: '.green.bold + self.address);
// }
// ionicConfig.set(addressConfigKey, self.address);
// cb();
prompt.resume();
console.log('\n\n\n\nResuming prompt\n\n')
console.log('Stdin after prompt')
// console.log(process.stdin)
// process.stdin.resume();
deferred.resolve();
// return q.promise;
// }
// }
// self.ionic.fail('Invalid address selection');
});
return deferred.promise;
}
var listenForServerCommands = function listenForServerCommands() {
// var self = this;
console.log('listenForServerCommands - stdin.on(readable)')
// console.log(process.stdin)
process.stdin.resume();
process.stdin.on('data', function() {
console.log('listenForServerCommands - now listening on readable')
var input = process.stdin.read();
if (input === null) return;
input = (input + '').trim();
if(input == 'restart' || input == 'r') {
// self._goToUrl('/?restart=' + Math.floor((Math.random() * 899999) + 100000));
} else if(input.indexOf('goto ') === 0 || input.indexOf('g ') === 0) {
var url = input.replace('goto ', '').replace('g ', '');
// self._goToUrl(url);
} else if(input == 'consolelogs' || input == 'c') {
// self.printConsoleLogs = !self.printConsoleLogs;
// console.log('Console log output: '.green + (self.printConsoleLogs ? 'enabled' : 'disabled'));
// self._goToUrl('/?restart=' + Math.floor((Math.random() * 899999) + 100000));
} else if(input == 'serverlogs' || input == 's') {
// self.printServerLogs = !self.printServerLogs;
// console.log('Server log output: '.green + (self.printServerLogs ? 'enabled' : 'disabled'));
} else if(input.match(/^go\([+\-]?[0-9]{1,9}\)$/)) {
// self._goToHistory(input);
} else if(input == 'help' || input == 'h') {
// self.printCommandTips();
} else if(input == 'quit' || input == 'q') {
if(self.childProcess) {
self.childProcess.kill('SIGTERM');
}
process.exit();
} else if(input == 'clear' || input == 'clr') {
process.stdout.write("\u001b[2J\u001b[0;0H");
} else {
console.log('\nInvalid ionic server command'.error.bold);
// self.printCommandTips();
}
});
}
var listen2 = function listen2() {
var readline = require('readline');
var rl = readline.createInterface(process.stdin, process.stdout);
rl.setPrompt('guess> ');
rl.prompt();
rl.on('line', function(line) {
if (line === "right") rl.close();
console.log('entry: ', line, '\n')
rl.prompt();
}).on('close',function(){
process.exit(0);
});
}
askPrompt().then(function() {
listenForServerCommands();
// listen2();
console.log('Done')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment