cookie-ai-cli
Version:
A command-line interface tool designed to bridge the gap between natural language processing and command-line operations.
38 lines • 1.5 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.askQuestion = void 0;
const colors_1 = require("./utils/colors");
function askQuestion(rl, query, options = []) {
return new Promise((resolve) => {
let fullQuery = `${colors_1.colors.cyan}${query}${colors_1.colors.reset}`;
if (options.length > 0) {
fullQuery += "\n";
options.forEach((option, index) => {
fullQuery += `${index + 1}. ${option}\n`;
});
fullQuery += `Enter your choice (1-${options.length}): `;
}
else {
fullQuery += " ";
}
rl.question(fullQuery, (answer) => {
if (options.length > 0) {
const choice = parseInt(answer, 10);
// Check if the choice is a valid number within the options range
if (!isNaN(choice) && choice >= 1 && choice <= options.length) {
resolve(options[choice - 1]);
}
else {
console.log(`${colors_1.colors.red}Invalid choice, please try again.${colors_1.colors.reset}`);
resolve(askQuestion(rl, query, options)); // Recursively ask again
}
}
else {
// For a simple query, directly resolve the answer
resolve(answer);
}
});
});
}
exports.askQuestion = askQuestion;
//# sourceMappingURL=ask-question.js.map
;