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
Provenance
- Commit
007ab33- Tree
68269e6- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
fdfd9d2
2 files changed+47 added−58 removed
modified:examples/input-57 removed
4 unmodified lines5 */6 7 var program = require('../');8 9-// 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)18-19-program.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-};26-27-program.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-};34-35-program.askSingleLine = function(str, fn){36- if ('function' == typeof arguments[2]) {37- return this['askFor' + (fn.name || fn)](str, arguments[2]);38- }39-40- process.stdout.write(str);41- process.stdin.setEncoding('utf8');42- process.stdin.once('data', function(val){43- fn(val);44- }).resume();45-};46-47-program.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-};60-61-program.ask = function(str, fn){62- if (/ $/.test(str)) return this.askSingleLine.apply(this, arguments);63- this.askMultiLine(str, fn);64-};65-9 program.ask('Username: ', function(name){10 console.log('hi %s', name);11 12 program.ask('Description:', function(desc){12 unmodified linesmodified:lib/commander.js+47 added-1 removed
166 unmodified lines167 168 return ret;169 };170 171-171 Command.prototype.parseArgs = function(args){172 var cmds = this.commands173 , len = cmds.length;174 155 unmodified lines330 , ''331 ].join('\n');332 };333 334+Command.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+};341+342+Command.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+};349+350+Command.prototype.askSingleLine = function(str, fn){351+ if ('function' == typeof arguments[2]) {352+ return this['askFor' + (fn.name || fn)](str, arguments[2]);353+ }354+355+ process.stdout.write(str);356+ process.stdin.setEncoding('utf8');357+ process.stdin.once('data', function(val){358+ fn(val);359+ }).resume();360+};361+362+Command.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+};375+376+Command.prototype.ask = function(str, fn){377+ if (/ $/.test(str)) return this.askSingleLine.apply(this, arguments);378+ this.askMultiLine(str, fn);379+};380+381 function pad(str, width) {382 var len = Math.max(0, width - str.length);383 return str + Array(len + 1).join(' ');384 }