multiselect static method
Display a multi-select prompt
Implementation
static List<String> multiselect(String question, List<String> options,
{List<int> defaultIndices = const []}) {
stdout.writeln('\x1B[36m?\x1B[0m $question');
stdout.writeln('');
stdout.writeln('Select items by typing numbers separated by commas (e.g., 1,3,5)');
stdout.writeln('Press Enter to confirm');
stdout.writeln('');
for (var i = 0; i < options.length; i++) {
final isSelected = defaultIndices.contains(i + 1);
final mark = isSelected ? '☑' : '☐';
stdout.writeln(' $mark [${i + 1}] ${options[i]}');
}
stdout.writeln('');
while (true) {
stdout.write('\x1B[36m?\x1B[0m Selection [${defaultIndices.join(',')}] ');
final response = stdin.readLineSync()?.trim();
// Use default if empty
if (response == null || response.isEmpty) {
return defaultIndices.map((i) => options[i - 1]).toList();
}
// Parse selection
final indices = response.split(',').map((s) => int.tryParse(s.trim())).whereType<int>().toList();
// Validate
final valid = indices.every((i) => i > 0 && i <= options.length);
if (valid && indices.isNotEmpty) {
return indices.map((i) => options[i - 1]).toList();
}
error('Invalid selection. Please try again.');
}
}