printProgessAnimation<T> function

Future<T> printProgessAnimation<T>(
  1. FutureOrResultCallback<T> workload, {
  2. Duration? animationSpeed,
  3. ProgressAnimationType type = ProgressAnimationType.spinningBar,
  4. StringSink? output,
})

Write a progess animation to output while waiting for workload.

workload must not write to output, otherwise the animation will be interrupted.

output defaults to stdout.

Implementation

Future<T> printProgessAnimation<T>(
  FutureOrResultCallback<T> workload, {
  Duration? animationSpeed,
  ProgressAnimationType type = ProgressAnimationType.spinningBar,
  StringSink? output,
}) async {
  final oSink = output ?? stdout;

  final timer = switch (type) {
    ProgressAnimationType.spinningBar =>
      _createSpinningBarAnimation(sink: oSink, animationSpeed: animationSpeed),
    ProgressAnimationType.dots =>
      _createDotsAnimation(sink: oSink, animationSpeed: animationSpeed),
  };

  T res;
  try {
    res = await workload();
  } finally {
    timer.cancel();
  }

  return res;
}