UNPKG

flashbacker

Version:

Claude Code state management with session continuity and AI personas

126 lines 5.38 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.InteractivePrompts = void 0; const readline_1 = __importDefault(require("readline")); const chalk_1 = __importDefault(require("chalk")); class InteractivePrompts { constructor() { this.rl = readline_1.default.createInterface({ input: process.stdin, output: process.stdout, }); } async prompt(options) { return new Promise((resolve, reject) => { const { message, default: defaultValue, required = false, choices, validate } = options; let promptMessage = chalk_1.default.cyan(`? ${message}`); if (choices) { promptMessage += chalk_1.default.gray(` (${choices.join('/')})`); } if (defaultValue) { promptMessage += chalk_1.default.gray(` (${defaultValue})`); } promptMessage += ': '; this.rl.question(promptMessage, (answer) => { const input = answer.trim() || defaultValue || ''; // Check if required if (required && !input) { console.log(chalk_1.default.red('✗ This field is required')); this.prompt(options).then(resolve).catch(reject); return; } // Check choices if (choices && input && !choices.includes(input)) { console.log(chalk_1.default.red(`✗ Please choose from: ${choices.join(', ')}`)); this.prompt(options).then(resolve).catch(reject); return; } // Validate if (validate && input) { const validation = validate(input); if (validation !== true) { console.log(chalk_1.default.red(`✗ ${typeof validation === 'string' ? validation : 'Invalid input'}`)); this.prompt(options).then(resolve).catch(reject); return; } } resolve(input); }); }); } async multilinePrompt(message, endMarker = 'END') { console.log(chalk_1.default.cyan(`? ${message}`)); console.log(chalk_1.default.gray(` (Type your response, then '${endMarker}' on a new line to finish)`)); return new Promise((resolve) => { const lines = []; const collectInput = () => { this.rl.question(' ', (line) => { if (line.trim() === endMarker) { resolve(lines.join('\n')); } else { lines.push(line); collectInput(); } }); }; collectInput(); }); } async confirm(message, defaultValue = false) { const defaultStr = defaultValue ? 'Y/n' : 'y/N'; const answer = await this.prompt({ message: `${message} (${defaultStr})`, default: defaultValue ? 'y' : 'n', validate: (input) => { const lower = input.toLowerCase(); return ['y', 'yes', 'n', 'no'].includes(lower) || 'Please answer y/yes or n/no'; }, }); const lower = answer.toLowerCase(); return lower === 'y' || lower === 'yes'; } async select(message, choices) { console.log(chalk_1.default.cyan(`? ${message}`)); choices.forEach((choice, index) => { const desc = choice.description ? chalk_1.default.gray(` - ${choice.description}`) : ''; console.log(` ${index + 1}. ${choice.name}${desc}`); }); const answer = await this.prompt({ message: 'Select option', required: true, validate: (input) => { const num = parseInt(input); return (num >= 1 && num <= choices.length) || `Please select 1-${choices.length}`; }, }); const index = parseInt(answer) - 1; return choices[index].value; } async multiSelect(message, choices) { console.log(chalk_1.default.cyan(`? ${message}`)); choices.forEach((choice, index) => { const desc = choice.description ? chalk_1.default.gray(` - ${choice.description}`) : ''; console.log(` ${index + 1}. ${choice.name}${desc}`); }); const answer = await this.prompt({ message: 'Select options (comma-separated)', required: true, validate: (input) => { const nums = input.split(',').map(s => parseInt(s.trim())); const valid = nums.every(num => num >= 1 && num <= choices.length); return valid || `Please select numbers 1-${choices.length}, comma-separated`; }, }); const indices = answer.split(',').map(s => parseInt(s.trim()) - 1); return indices.map(i => choices[i].value); } close() { this.rl.close(); } } exports.InteractivePrompts = InteractivePrompts; //# sourceMappingURL=interactive-prompts.js.map