Skip to content
Myr

added input methods

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 fdfd9d2 · Browse files · 2 files+47 added−58 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+47 added−58 removed

  • other:+47 added−58 removed

2 files+47 added−58 removed

modified:examples/input-57 removed
4 unmodified lines
5 */67var program = require('../');89// ask('bool?')10// ask('Username: ')11// ask('Password: ')12// ask('Description:')13// ask('Birthday: ', Date)14// ask('Age: ', Number)15// ask('Fav colors: ', Array)16// askForArray('Fav colors: ')17// choose(options)1819program.askForNumber = function(str, fn){20 this.askSingleLine(str, function(val){21 val = Number(val);22 if (isNaN(val)) return program.askForNumber(str + '(must be a number) ', fn);23 fn(val);24 });25};2627program.askForDate = function(str, fn){28 this.askSingleLine(str, function(val){29 val = new Date(val);30 if (isNaN(val.getTime())) return program.askForDate(str + '(must be a date) ', fn);31 fn(val);32 });33};3435program.askSingleLine = function(str, fn){36 if ('function' == typeof arguments[2]) {37 return this['askFor' + (fn.name || fn)](str, arguments[2]);38 }3940 process.stdout.write(str);41 process.stdin.setEncoding('utf8');42 process.stdin.once('data', function(val){43 fn(val);44 }).resume();45};4647program.askMultiLine = function(str, fn){48 var buf = '';49 console.log(str);50 process.stdin.setEncoding('utf8');51 process.stdin.on('data', function(val){52 if ('\n' == val) {53 process.stdin.removeAllListeners('data');54 fn(buf);55 } else {56 buf += val;57 }58 }).resume();59};6061program.ask = function(str, fn){62 if (/ $/.test(str)) return this.askSingleLine.apply(this, arguments);63 this.askMultiLine(str, fn);64};659program.ask('Username: ', function(name){10 console.log('hi %s', name);1112 program.ask('Description:', function(desc){
12 unmodified lines
modified:lib/commander.js+47 added-1 removed
166 unmodified lines
167168 return ret;169};170171171Command.prototype.parseArgs = function(args){172 var cmds = this.commands173 , len = cmds.length;174
155 unmodified lines
330 , ''331 ].join('\n');332};333334Command.prototype.askForNumber = function(str, fn){335 this.askSingleLine(str, function(val){336 val = Number(val);337 if (isNaN(val)) return program.askForNumber(str + '(must be a number) ', fn);338 fn(val);339 });340};341342Command.prototype.askForDate = function(str, fn){343 this.askSingleLine(str, function(val){344 val = new Date(val);345 if (isNaN(val.getTime())) return program.askForDate(str + '(must be a date) ', fn);346 fn(val);347 });348};349350Command.prototype.askSingleLine = function(str, fn){351 if ('function' == typeof arguments[2]) {352 return this['askFor' + (fn.name || fn)](str, arguments[2]);353 }354355 process.stdout.write(str);356 process.stdin.setEncoding('utf8');357 process.stdin.once('data', function(val){358 fn(val);359 }).resume();360};361362Command.prototype.askMultiLine = function(str, fn){363 var buf = '';364 console.log(str);365 process.stdin.setEncoding('utf8');366 process.stdin.on('data', function(val){367 if ('\n' == val) {368 process.stdin.removeAllListeners('data');369 fn(buf);370 } else {371 buf += val;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);379};380381function pad(str, width) {382 var len = Math.max(0, width - str.length);383 return str + Array(len + 1).join(' ');384}