menuChoice static method

Future<void> menuChoice(
  1. dynamic message,
  2. Map<String, Function> menu, {
  3. CappColors color = CappColors.none,
})

Menu

Implementation

static Future<void> menuChoice(
  message,
  Map<String, Function> menu, {
  CappColors color = CappColors.none,
}) async {
  int selectedIndex = 0;

  void render([int input = -1]) {
    // Clear the console
    clear();
    write(message, CappColors.info, true);

    for (var i = 0; i < menu.length; i++) {
      if (i == selectedIndex) {
        write(' > [${menu.keys.toList()[i]}]', CappColors.warning);
      } else {
        write('   ${menu.keys.toList()[i]}');
      }
    }
    write(
      '\n\nUse Arrow Keys to navigate, Enter to confirm.',
      color,
    );
  }

  render();

  try {
    while (true) {
      var input = console.readKey();
      if (input.controlChar == ControlCharacter.arrowUp) {
        // Arrow Up
        selectedIndex = (selectedIndex - 1) % menu.length;
        if (selectedIndex < 0) selectedIndex += menu.length;
      } else if (input.controlChar == ControlCharacter.arrowDown) {
        // Arrow Down
        selectedIndex = (selectedIndex + 1) % menu.length;
      } else if (input.controlChar == ControlCharacter.enter) {
        await menu.values.toList()[selectedIndex]();
        break;
      }
      render(int.tryParse(input.char) ?? -1);
    }
  } catch (e) {
    write("Error", CappColors.error);
  }
}