humanizeListStructure function

String humanizeListStructure(
  1. List items, {
  2. String style = 'bullet',
  3. String indent = ' ',
  4. bool allowNesting = false,
})

Formats a list with bullets or numbers.

Implementation

String humanizeListStructure(List<dynamic> items, {
  String style = 'bullet', // bullet, number, letter
  String indent = '  ',
  bool allowNesting = false,
}) {
  if (items.isEmpty) return '';

  final buffer = StringBuffer();

  for (int i = 0; i < items.length; i++) {
    final item = items[i];
    final marker = _getListMarker(style, i);

    if (item is List && allowNesting) {
      buffer.writeln('$marker ${item.first}');
      if (item.length > 1) {
        final subList = item.sublist(1);
        final subFormatted = humanizeListStructure(subList, style: style, indent: indent, allowNesting: allowNesting);
        final indentedSub = subFormatted.split('\n').map((line) => '$indent$line').join('\n');
        buffer.writeln(indentedSub);
      }
    } else {
      buffer.writeln('$marker ${_formatValue(item)}');
    }
  }

  return buffer.toString().trim();
}