runSpinnerTask<T> function

Future<T> runSpinnerTask<T>({
  1. required String message,
  2. required Future<T> task(),
  3. Spinner spinner = Spinners.miniDot,
  4. required Terminal terminal,
  5. ProgramOptions? options,
})

Runs an animated spinner while executing task, returning its result.

Implementation

Future<T> runSpinnerTask<T>({
  required String message,
  required Future<T> Function() task,
  Spinner spinner = Spinners.miniDot,
  required Terminal terminal,
  ProgramOptions? options,
}) async {
  final controller = _PromptController<T>();
  // Pre-attach an error listener so that if [_SpinnerTaskModel] calls
  // [completeError] while the program is still running (e.g. on InterruptMsg),
  // Dart's async machinery does not flag the future as an unhandled error
  // before [program.run()] returns and we reach the `await controller.future`
  // below.  The separate listener does NOT swallow the error — the `await`
  // below will still throw as expected.
  final resultFuture = controller.future
    ..ignore(); // registers a no-op error listener; original future is intact
  final program = Program(
    _SpinnerTaskModel<T>(
      message: message,
      task: task,
      spinner: spinner,
      controller: controller,
    ),
    options: options ?? promptProgramOptions,
    terminal: terminal,
  );
  await program.run();
  return await resultFuture;
}