Added `Command#choose()`
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 007ab33 · Browse files · 3 files·+52 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+52 added−16 removed
- other:+52 added−16 removed
Provenance
- Commit
e7cdf10- Tree
769e5be- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
007ab33
3 files changed+52 added−16 removed
added:examples/choice+15 added
1+#!/usr/bin/env node2+3+/**4+ * Module dependencies.5+ */6+7+var program = require('../');8+9+var list = ['tobi', 'loki', 'jane', 'manny', 'luna'];10+11+console.log('Choose the coolest pet:');12+program.choose(list, function(i){13+ console.log('you chose %d "%s"', i, list[i]);14+ process.stdin.destroy();15+});modified:examples/input+4 added-4 removed
4 unmodified lines5 */6 7 var program = require('../');8 9-program.ask('Username: ', function(name){9+program.prompt('Username: ', function(name){10 console.log('hi %s', name);11 12- program.ask('Description:', function(desc){12+ program.prompt('Description:', function(desc){13 console.log('description was "%s"', desc.trim());14 15- program.ask('Age: ', Number, function(age){15+ program.prompt('Age: ', Number, function(age){16 console.log('age: %j', age);17 18- program.ask('Birthdate: ', Date, function(date){18+ program.prompt('Birthdate: ', Date, function(date){19 console.log('date: %s', date);20 process.stdin.destroy();21 });22 });2 unmodified linesmodified:lib/commander.js+33 added-12 removed
329 unmodified lines330 , ''331 ].join('\n');332 };333 334-Command.prototype.askForNumber = function(str, fn){335- this.askSingleLine(str, function(val){334+Command.prototype.promptForNumber = function(str, fn){335+ this.promptSingleLine(str, function(val){336 val = Number(val);337- if (isNaN(val)) return program.askForNumber(str + '(must be a number) ', fn);337+ if (isNaN(val)) return program.promptForNumber(str + '(must be a number) ', fn);338 fn(val);339 });340 };341 342-Command.prototype.askForDate = function(str, fn){343- this.askSingleLine(str, function(val){342+Command.prototype.promptForDate = function(str, fn){343+ this.promptSingleLine(str, function(val){344 val = new Date(val);345- if (isNaN(val.getTime())) return program.askForDate(str + '(must be a date) ', fn);345+ if (isNaN(val.getTime())) return program.promptForDate(str + '(must be a date) ', fn);346 fn(val);347 });348 };349 350-Command.prototype.askSingleLine = function(str, fn){350+Command.prototype.promptSingleLine = function(str, fn){351 if ('function' == typeof arguments[2]) {352- return this['askFor' + (fn.name || fn)](str, arguments[2]);352+ return this['promptFor' + (fn.name || fn)](str, arguments[2]);353 }354 355 process.stdout.write(str);356 process.stdin.setEncoding('utf8');1 unmodified line358 fn(val);359 }).resume();360 };361 362-Command.prototype.askMultiLine = function(str, fn){362+Command.prototype.promptMultiLine = function(str, fn){363 var buf = '';364 console.log(str);365 process.stdin.setEncoding('utf8');366 process.stdin.on('data', function(val){5 unmodified lines372 }373 }).resume();374 };375 376-Command.prototype.ask = function(str, fn){377- if (/ $/.test(str)) return this.askSingleLine.apply(this, arguments);378- this.askMultiLine(str, fn);376+Command.prototype.prompt = function(str, fn){377+ if (/ $/.test(str)) return this.promptSingleLine.apply(this, arguments);378+ this.promptMultiLine(str, fn);379 };380 381+Command.prototype.choose = function(list, fn){382+ var self = this;383+384+ list.forEach(function(item, i){385+ console.log(' %d) %s', i + 1, item);386+ });387+388+ function again() {389+ self.prompt(' : ', function(val){390+ val = parseInt(val, 10) - 1;391+ if (null == list[val]) {392+ again();393+ } else {394+ fn(val, list[val]);395+ }396+ });397+ }398+399+ again();400+};401+402 function pad(str, width) {403 var len = Math.max(0, width - str.length);404 return str + Array(len + 1).join(' ');405 }