chooseSync method

T chooseSync()

Implementation

T chooseSync() {
  var buff = StringBuffer();
  var i = -1;

  for (var choice in choices) {
    i++;
    buff.writeln(formatter(choice, i + 1));
  }

  buff.write(message);

  while (true) {
    var input = Prompter(buff.toString()).promptSync();
    var result = _parseInteger(input ?? '');

    if (result == null && input != null) {
      var exists = choices
          .map((it) => it.toString().trim().toLowerCase())
          .contains(input.trim().toLowerCase());
      if (exists) {
        var val = choices.firstWhere((it) {
          return it.toString().trim().toLowerCase() ==
              input.trim().toLowerCase();
        });

        return val;
      }
    }

    var choice;

    try {
      if (result != null) {
        choice = choices[result - 1];
        return choice;
      }
      // ignore: empty_catches
    } catch (e) {}
  }
}