search<T> static method

T search<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 by typing to filter them, like a command palette.

Matching is fuzzy (see Fuzzy): gco finds git checkout. The best matches come first, with the matched letters underlined. note text is searched too, so it suits descriptions.

final command = Prompts.search(
  'Run',
  commands,
  format: (c) => c.name,
  note: (c) => c.description,
);

Implementation

static T search<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(
    SearchPrompt<T>(
      prompt,
      options,
      format: format,
      note: note,
      defaultIndex: start < 0 ? null : start,
      pageSize: pageSize,
    ),
  );
}