cli-interact
Advanced tools
Comparing version 0.1.6 to 0.1.7
55
index.js
@@ -33,2 +33,56 @@ //============================== | ||
function getChoice (title, choices, opts) { | ||
var answer; | ||
var flagAllowNoAnswer; | ||
var flagReturnNumber; | ||
var i; | ||
var numChoices = choices.length; | ||
var parts = ['Select ', title, ' (']; | ||
var queryText; | ||
var str; | ||
var temp; | ||
var value; | ||
if (opts) { | ||
if (opts.allowNoAnswer) { | ||
flagAllowNoAnswer = true; | ||
} | ||
if (opts.returnNumber) { | ||
flagReturnNumber = true; | ||
} | ||
} | ||
for (i = 1; i <= numChoices; i += 1) { // display 1 based to user | ||
parts.push(i); | ||
parts.push('='); | ||
parts.push(choices[i - 1]); // choices array is 0-based | ||
parts.push('; '); | ||
} | ||
parts.pop(); // get rid of trailing ';' | ||
parts.push(') ?'); | ||
queryText = parts.join(''); | ||
while (true) { | ||
answer = readlineSync.question(queryText); | ||
if (answer === "\u0003") { | ||
// Ctl-C | ||
process.exit(); | ||
} else if (flagAllowNoAnswer && (answer === '')) { | ||
return answer; | ||
} else { | ||
value = parseInt(answer, 10); | ||
if ((value >= 1) && (value <= numChoices)) { | ||
value -= 1; // user input is 1-based; choices array is 0-based | ||
if (flagReturnNumber) { | ||
return value; | ||
} else { | ||
return choices[value]; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
function getChoiceByChar (title, choices, flagAllowNoAnswer) { | ||
@@ -140,2 +194,3 @@ var answer; | ||
getChar : getChar | ||
, getChoice : getChoice | ||
, getChoiceByChar : getChoiceByChar | ||
@@ -142,0 +197,0 @@ , getInteger : getInteger |
{ | ||
"name": "cli-interact", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"title": "cli interact", | ||
@@ -5,0 +5,0 @@ "description": "Simple helper tools for interacting synchronously with user at command line", |
10519
273