runWithConsoleMarkdown<T> function
Future<T>
runWithConsoleMarkdown<T>(
- Future<
T> body(), { - void onError(
- Object error,
- StackTrace stack
Run body inside a Dart zone that automatically renders console_markdown
syntax in every print() call.
If the current zone already has console_markdown active (detected via
kConsoleMarkdownZoneKey), body runs directly without adding another
rendering layer — this prevents double-rendering when tools are invoked
from contexts that already set up the zone (e.g. tom_d4rt_dcli).
The optional onError callback handles uncaught errors. By default,
errors are printed to stderr with console_markdown formatting and the
process exits with code 1.
Returns the value returned by body.
Implementation
Future<T> runWithConsoleMarkdown<T>(
Future<T> Function() body, {
void Function(Object error, StackTrace stack)? onError,
}) async {
// Already inside a console_markdown zone → pass through.
if (isConsoleMarkdownActive) {
return body();
}
final completer = Completer<T>();
runZonedGuarded(
() async {
try {
final result = await body();
if (!completer.isCompleted) completer.complete(result);
} catch (error, stack) {
if (!completer.isCompleted) completer.completeError(error, stack);
}
},
(error, stack) {
if (onError != null) {
onError(error, stack);
} else {
stderr.writeln('<red>**Uncaught error:**</red> $error'.toConsole());
stderr.writeln(stack.toString());
exit(1);
}
if (!completer.isCompleted) {
completer.completeError(error, stack);
}
},
zoneSpecification: ZoneSpecification(
print: (Zone self, ZoneDelegate parent, Zone zone, String line) {
parent.print(zone, line.toConsole());
},
),
zoneValues: {kConsoleMarkdownZoneKey: true},
);
return completer.future;
}