run method
PromptResult
run({
- required void render(
- RenderOutput out
- required PromptResult? onKey(
- KeyEvent event
Runs the prompt loop synchronously.
render is called with a RenderOutput to write content.
onKey handles key events and returns:
PromptResult.confirmedto exit with successPromptResult.cancelledto exit with cancellationnullto continue the loop
Implementation
PromptResult run({
required void Function(RenderOutput out) render,
required PromptResult? Function(KeyEvent event) onKey,
}) {
final session = _createSession();
final output = RenderOutput();
session.start();
// Initial render (no clearing needed - nothing written yet)
render(output);
PromptResult result = PromptResult.cancelled;
try {
while (true) {
final event = KeyEventReader.read();
final action = onKey(event);
if (action != null) {
result = action;
break;
}
// Clear only our output, then re-render
output.clear();
render(output);
}
} finally {
onBeforeCleanup?.call();
session.end();
onAfterCleanup?.call();
}
// Optionally clear our final output
if (endBehavior.clearOnEnd) {
output.clear();
}
return result;
}