suspend<T> method

Future<T> suspend<T>(
  1. Future<T> run()
)

Hands the terminal to another program while run runs, one that reads it itself (sudo asking for a password, a package manager, a pager, an editor, started with ProcessStartMode.inheritStdio), and takes it back after.

With the inline session open (or opening), its region stays on the screen with the cursor on the line below it, where the other program writes; raw mode is off and the session's input paused, so the other program reads what is typed; a progress stops being drawn until run completes, and the session is drawn again below what was printed. Without one, run just runs. Returns what run returns, or throws what it throws, once the terminal is back.

final cli = Cli.current;
final progress = cli.progress('Installing');
final code = await cli.suspend(() async {
  final sudo = await Process.start(
    'sudo',
    ['true'],
    mode: ProcessStartMode.inheritStdio,
  );
  return sudo.exitCode;
});
progress.complete(code == 0 ? 'Installed' : null);

Implementation

Future<T> suspend<T>(Future<T> Function() run) async {
  var session = _liveSession;
  if (session == null) {
    final opening = _openingSession;
    if (opening != null) {
      try {
        session = await opening;
      } on Object {
        session = null;
      }
    }
  }
  return session != null && session.isOpen ? session.suspend(run) : run();
}