chooseAny<T extends Object?> method

Future<List<T>> chooseAny<T extends Object?>(
  1. String? message, {
  2. required List<T> choices,
  3. List<T>? defaultValues,
  4. String display(
    1. T choice
    )?,
})

Asks for any number of choices, shown with display. The ones in defaultValues start checked.

Implementation

Future<List<T>> chooseAny<T extends Object?>(
  String? message, {
  required List<T> choices,
  List<T>? defaultValues,
  String Function(T choice)? display,
}) async {
  final labels = [for (final c in choices) display?.call(c) ?? '$c'];
  final initial = {
    for (final v in defaultValues ?? <T>[])
      if (choices.contains(v)) choices.indexOf(v),
  };
  List<int>? selectedIndices;
  await _ask(
    () => promptMultiSelect(labels,
        title: '$message', initial: initial, options: _options),
    onSelection: (indices) => selectedIndices = indices,
  );
  final indices = selectedIndices ?? (initial.toList()..sort());
  return [for (final i in indices) choices[i]];
}