runGuarded<T> static method
Runs a function with a raw terminal, ensuring raw mode is disabled and the terminal is restored if the program encounters an uncaught exception.
Implementation
static Future<T> runGuarded<T>(
FutureOr<T> Function(Terminal terminal) body, {
TerminalBackend? backend,
}) async {
final terminal = Terminal(backend);
final completer = Completer<T>();
runZonedGuarded(
() async {
try {
final result = await body(terminal);
if (!completer.isCompleted) {
completer.complete(result);
}
} catch (e, st) {
if (!completer.isCompleted) {
completer.completeError(e, st);
}
}
},
(error, stackTrace) {
terminal.dispose();
terminal.showCursor(); // Re-enable cursor
if (!completer.isCompleted) {
completer.completeError(error, stackTrace);
}
},
);
try {
return await completer.future;
} finally {
terminal.dispose();
terminal.showCursor(); // Re-enable cursor
}
}