listOut method

String listOut({
  1. StringBuffer? buffer,
  2. String? prefix,
  3. String wrapCallableKey(
    1. String
    )?,
  4. String wrapNonCallableKey(
    1. String
    )?,
  5. String wrapMeta(
    1. String
    )?,
})

Implementation

String listOut({
  StringBuffer? buffer,
  String? prefix,
  String Function(String)? wrapCallableKey,
  String Function(String)? wrapNonCallableKey,
  String Function(String)? wrapMeta,
}) {
  buffer ??= StringBuffer();
  wrapCallableKey ??= (key) => key;
  wrapNonCallableKey ??= (key) => key;
  wrapMeta ??= (meta) => meta;
  if (prefix == null) {
    buffer.writeln('scripts.yaml:');
  }

  prefix ??= '   ';

  final keys = scripts.keys.where((e) => !e.startsWith('_'));
  bool isLast(String key) => keys.last == key;

  for (final MapEntry(:key, value: script) in scripts.entries) {
    if (key.startsWith('_')) continue;

    final wrapper =
        script.commands.isEmpty ? wrapNonCallableKey : wrapCallableKey;

    final entry = isLast(key) ? '└──' : '├──';
    buffer.writeln('$prefix$entry${wrapper(key)}');
    final sub = isLast(key) ? '   ' : '│  ';
    script.listOut(
      buffer: buffer,
      prefix: prefix + sub,
      wrapCallableKey: wrapCallableKey,
      wrapNonCallableKey: wrapNonCallableKey,
      wrapMeta: wrapMeta,
    );
  }

  return buffer.toString();
}