capture<R> function

Future<Progress> capture<R>(
  1. R action(), {
  2. Progress? progress,
})

Run code in a zone which traps calls to print and printerr redirecting them to the passed progress.

Implementation

Future<Progress> capture<R>(R Function() action, {Progress? progress}) async {
  progress ??= Progress.devNull();

  /// overload printerr so we can trap it.
  final zoneValues = <String, CaptureZonePrintErr>{
    'printerr': (line) {
      if (line != null) {
        progress!.addToStderr(line);
      }
    }
  };

  // ignore: flutter_style_todos
  /// TODO: we need to somehow await this.
  runZonedGuarded(
    action,
    (e, st) {},
    zoneValues: zoneValues,
    zoneSpecification: ZoneSpecification(
      print: (self, parent, zone, line) => progress!.addToStdout(line),
    ),
  );

  return progress;
}