select<T> static method

T select<T>(
  1. String prompt,
  2. List<T> options, {
  3. String format(
    1. T option
    )?,
  4. String note(
    1. T option
    )?,
  5. T? defaultOption,
  6. int? pageSize,
})

Lets the user pick one of options and gives back the one they picked.

The options are a list moved through with the arrow keys (or j/k), Home/End and Page Up/Down, and picked with Enter. Typing a number jumps to that option. Long lists scroll.

  • format: how each option is shown. Defaults to toString().
  • note: dim text after each option, like a description.
  • defaultOption: the option highlighted at the start, and the answer in PromptMode.unattended.
  • pageSize: how many options show at once. Fits the screen when left out.
final colour = Prompts.select('Pick a colour', ['Red', 'Green', 'Blue']);
final file = Prompts.select('Open which file?', files, format: basename);

Implementation

static T select<T>(
  String prompt,
  List<T> options, {
  String Function(T option)? format,
  String Function(T option)? note,
  T? defaultOption,
  int? pageSize,
}) {
  final start = defaultOption == null ? -1 : options.indexOf(defaultOption);

  return PromptRunner.run(
    SelectPrompt<T>(
      prompt,
      options,
      format: format,
      note: note,
      start: start < 0 ? 0 : start,
      pageSize: pageSize,
      hasDefault: start >= 0,
    ),
  );
}