run method

PromptResult run({
  1. required void render(
    1. RenderOutput out
    ),
  2. required PromptResult? onKey(
    1. KeyEvent event
    ),
})

Runs the prompt loop synchronously.

render is called with a RenderOutput to write content. onKey handles key events and returns:

  • PromptResult.confirmed to exit with success
  • PromptResult.cancelled to exit with cancellation
  • null to 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;
}