findLinks function

List<LogLink> findLinks(
  1. String line, {
  2. String? baseUrl,
})

Every clickable run in line, left to right and never overlapping.

line is expected to be ANSI-stripped already — the caller has to strip it anyway to paint it, and doing it twice would let the two disagree about where a run starts.

baseUrl is what the service announced it is listening on, or null while it has not announced anything yet. Without one a bare route path has no base and is not returned: a click that cannot be resolved must do nothing, because opening a guess is worse than opening nothing.

Implementation

List<LogLink> findLinks(String line, {String? baseUrl}) {
  final links = <LogLink>[
    ..._absoluteUrls(line),
    ...?_routePath(line, baseUrl: baseUrl),
  ]..sort((a, b) => a.start.compareTo(b.start));

  // A route row cannot also contain an absolute URL, so this only ever drops
  // something if a pattern is widened later. Cheap insurance against two
  // overlapping ranges reaching the renderer, which would paint one run twice.
  final resolved = <LogLink>[];
  for (final link in links) {
    if (resolved.isNotEmpty && link.start < resolved.last.end) continue;
    resolved.add(link);
  }

  return resolved;
}