customCommandsMenu static method

String customCommandsMenu(
  1. List<NyCommand> commands
)

Renders the menu section: project commands under [Custom Commands], each package under [<package> Commands], descriptions aligned. Empty if none.

Implementation

static String customCommandsMenu(List<NyCommand> commands) {
  if (commands.isEmpty) return '';

  final List<NyCommand> projectCommands = commands
      .where((command) => command.package == null)
      .toList();
  final Map<String, List<NyCommand>> packageCommands = {};
  for (final NyCommand command in commands) {
    if (command.package == null) continue;
    packageCommands.putIfAbsent(command.package!, () => []).add(command);
  }

  final StringBuffer buffer = StringBuffer();
  void writeSection(String title, List<NyCommand> items) {
    if (buffer.isNotEmpty) buffer.writeln();
    buffer.writeln('[$title]');
    int width = 0;
    for (final NyCommand item in items) {
      if (item.fullName.length > width) width = item.fullName.length;
    }
    for (final NyCommand item in items) {
      buffer.write('  ${item.fullName}');
      final String? description = item.description;
      if (description != null && description.isNotEmpty) {
        buffer.write(' ' * (width - item.fullName.length + 4));
        buffer.write(description);
      }
      buffer.writeln();
    }
  }

  if (projectCommands.isNotEmpty) {
    writeSection('Custom Commands', projectCommands);
  }
  for (final MapEntry<String, List<NyCommand>> entry
      in packageCommands.entries) {
    writeSection('${entry.key} Commands', entry.value);
  }

  return buffer.toString();
}