Skip to content
Myr

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

3 files+52 added−16 removed

added:examples/choice+15 added
1#!/usr/bin/env node23/**4 * Module dependencies.5 */67var program = require('../');89var list = ['tobi', 'loki', 'jane', 'manny', 'luna'];1011console.log('Choose the coolest pet:');12program.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 lines
5 */67var program = require('../');89program.ask('Username: ', function(name){9program.prompt('Username: ', function(name){10 console.log('hi %s', name);1112 program.ask('Description:', function(desc){12 program.prompt('Description:', function(desc){13 console.log('description was "%s"', desc.trim());1415 program.ask('Age: ', Number, function(age){15 program.prompt('Age: ', Number, function(age){16 console.log('age: %j', age);1718 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 lines
modified:lib/commander.js+33 added-12 removed
329 unmodified lines
330 , ''331 ].join('\n');332};333334Command.prototype.askForNumber = function(str, fn){335 this.askSingleLine(str, function(val){334Command.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};341342Command.prototype.askForDate = function(str, fn){343 this.askSingleLine(str, function(val){342Command.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};349350Command.prototype.askSingleLine = function(str, fn){350Command.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 }354355 process.stdout.write(str);356 process.stdin.setEncoding('utf8');
1 unmodified line
358 fn(val);359 }).resume();360};361362Command.prototype.askMultiLine = function(str, fn){362Command.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 lines
372 }373 }).resume();374};375376Command.prototype.ask = function(str, fn){377 if (/ $/.test(str)) return this.askSingleLine.apply(this, arguments);378 this.askMultiLine(str, fn);376Command.prototype.prompt = function(str, fn){377 if (/ $/.test(str)) return this.promptSingleLine.apply(this, arguments);378 this.promptMultiLine(str, fn);379};380381Command.prototype.choose = function(list, fn){382 var self = this;383384 list.forEach(function(item, i){385 console.log(' %d) %s', i + 1, item);386 });387388 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 }398399 again();400};401402function pad(str, width) {403 var len = Math.max(0, width - str.length);404 return str + Array(len + 1).join(' ');405}