choose method

Future choose()

Implementation

Future<dynamic> choose() {
  final buff = StringBuffer();
  int i = -1;
  for (final choice in choices) {
    i++;
    buff.writeln(formatter(choice, i + 1));
  }
  buff.write(message);
  final completer = Completer<T>();
  late void Function(String) process;
  process = (final String input) {
    final result = _parseInteger(input);
    if (result == null) {
      final exists = choices.map((final it) => it.toString().trim().toLowerCase()).contains(input.trim().toLowerCase());
      if (exists) {
        final val = choices.firstWhere((final it) {
          return it.toString().trim().toLowerCase() == input.trim().toLowerCase();
        });
        completer.complete(val);
        return;
      }
    }
    T choice;
    try {
      if (result == null) {
        DCPrompter(console: console, message: buff.toString(),).prompt().then(process);
      } else {
        choice = choices[result - 1];
        completer.complete(choice);
      }
      // ignore: avoid_catches_without_on_clauses
    } catch (e) {
      DCPrompter(
        console: console,
        message: buff.toString(),
      ).prompt().then(process);
    }
  };
  DCPrompter(
    console: console,
    message: buff.toString(),
  ).prompt().then(process);
  return completer.future;
}