renderReadmeSection function

String renderReadmeSection(
  1. ReadmeContent content, {
  2. GoldenStore? store,
  3. DateTime? now,
})

Renders the whole README section for content: markers, heading, intro, the metric table (one row per publishable def, cells read from the store via store, default benchmarks.json in the cwd), footnote, image and stamp.

Rows are the publishable defs that have a recorded value anywhere in the store — a consumer that does not record size metrics gets no size rows, and a metric with no golden under any column renders n/a (never a fabricated number).

Implementation

String renderReadmeSection(
  ReadmeContent content, {
  GoldenStore? store,
  DateTime? now,
}) {
  final s = store ?? GoldenStore();
  final ts = now ?? DateTime.now().toUtc();
  String two(int n) => n.toString().padLeft(2, '0');
  final stamp = '${ts.year}-${two(ts.month)}-${two(ts.day)} '
      '${two(ts.hour)}:${two(ts.minute)} UTC';

  // One row per publishable def that any column has a value for; cells are
  // the def's canonical unit + formatTableValue (shared with the card).
  final rows = <String>[];
  for (final def in kPublishableMetricDefs) {
    final hasAny = content.columns.any((c) =>
        s.load(def.key, preferRefs: c.refs, fallbackAny: c.fallbackAny) !=
        null);
    if (!hasAny) continue;
    final cells = StringBuffer('| ${def.label} |');
    for (final column in content.columns) {
      final value =
          s.load(def.key, preferRefs: column.refs, fallbackAny: column.fallbackAny);
      cells.write(' ${formatTableValue(value, def.unit)} |');
    }
    rows.add(cells.toString());
  }

  final header =
      '| Metric | ${content.columns.map((c) => c.label).join(' | ')} |';
  final divider =
      '|${List.filled(content.columns.length + 1, '---').join('|')}|';

  final buffer = StringBuffer()
    ..writeln(kReadmeStart)
    ..writeln('## ${content.title}')
    ..writeln()
    ..writeln(content.intro.trim())
    ..writeln()
    ..writeln(header)
    ..writeln(divider)
    ..writeln(rows.join('\n'))
    ..writeln()
    ..writeln(content.footnote.trim());
  if (content.chartsUrl != null) {
    buffer
      ..writeln()
      ..writeln('**Trend history:** [charts](${content.chartsUrl})');
  }
  if (content.image != null) {
    buffer
      ..writeln()
      ..writeln('![${content.imageAlt ?? ''}](${content.image})');
  }
  if (content.stamp != null) {
    buffer
      ..writeln()
      ..writeln(content.stamp!.replaceAll('{ts}', stamp));
  }
  buffer.write(kReadmeEnd);
  return buffer.toString();
}