waitForTask function

Future waitForTask(
  1. String description,
  2. Future otherWork
)

Waits for otherWork, a long running task to complete while rendering a spinner to the screen with description.

Implementation

Future waitForTask(String description, Future otherWork) async {
  if (!description.endsWith(' ')) {
    description = '$description ';
  }
  stdout.write(description);
  try {
    stdout.hideCursor();
    int count = 0;
    bool done = false;
    stdout.write(spinner(count++));
    final tickerFuture = () async {
      while (!done) {
        stdout.write('\x1B[D${spinner(count++)}');
        await Future.delayed(const Duration(milliseconds: 100));
      }
    }();

    await otherWork;
    done = true;
    await tickerFuture;
    stdout.write('\x1B[D✔️');
    stdout.writeln();
  } finally {
    stdout.showCursor();
  }
}