Repository navigation
Add CancellationToken support to prompts - #404
Merged
Merged
Conversation
- Add CancellationToken overloads for the options and configure based Input / Password / Confirm / Select / MultiSelect / List APIs and for Bind; existing signatures are unchanged - While a cancelable token is supplied, poll for available keys instead of blocking in ReadKey, and close the prompt and throw OperationCanceledException once the token is canceled - Pass the token through the binders emitted by the source generator via a new ModelBinderRegistry.Register overload - Document cancellation from code and correct the Ctrl+C default behavior description Closes #208 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The cancellation test relies on a short wall-clock delay and can fail under scheduling stalls.
Review effort: Balanced
Findings: 1
What changed in this PR
Adds CancellationToken support throughout prompt execution and generated model binders.
Changes:
- Adds cancelable prompt and binding overloads.
- Polls console input while observing cancellation.
- Adds tests and English/Japanese documentation.
| File | Description |
|---|---|
README.md |
Adds cancellation example. |
docs/en/configuration.md |
Documents cancellation behavior. |
docs/ja/configuration.md |
Adds Japanese cancellation documentation. |
src/Sharprompt/Forms/FormBase.cs |
Implements cancellation-aware input polling. |
src/Sharprompt/ModelBinderRegistry.cs |
Supports token-aware binders. |
src/Sharprompt/Prompt.Basic.cs |
Adds token overloads for prompts. |
src/Sharprompt/Prompt.Bind.cs |
Passes tokens to model binders. |
src/Sharprompt.SourceGenerator/PromptBindableGenerator.cs |
Propagates tokens in generated binders. |
tests/Sharprompt.Tests/Binding/ModelBinderRegistryTests.cs |
Tests binder token propagation. |
tests/Sharprompt.Tests/Forms/FormInteractionTests.cs |
Tests form cancellation behavior. |
tests/Sharprompt.Tests/Sharprompt.Tests.csproj |
Suppresses the cancellation-token analyzer warning. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Cancel the token from FakeConsoleDriver once every queued key has been consumed instead of relying on a wall-clock delay. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Closes #208
Summary
Prompts can now be canceled from code with a
CancellationToken, e.g. to close a pending prompt when the host hides its console, when a request arrives from a server, or on a timeout.API (additive, no breaking changes)
CancellationTokenfor the options-object and configure-delegate forms ofInput/Password/Confirm/Select/MultiSelect/List, plusBind<T>(CancellationToken)andBind<T>(T, CancellationToken).Input<T>(string message, ...)) were intentionally left alone: adding an optional token there would make the existing overloads ambiguous or break binary compatibility.ModelBinderRegistry.Register<T>(Action<T, CancellationToken>); the existingRegister<T>(Action<T>)still works and ignores the token.Behavior
OperationCanceledException(carrying the token) is thrown, independent ofThrowExceptionOnCancel.Console.ReadKeycannot be interrupted, so only when a cancelable token is supplied the form pollsKeyAvailable(waiting on the token's wait handle for 16 ms between checks). Without a token, the blockingReadKeypath is unchanged.IConsoleDriveris unchanged.Source generator
The generated binders now register
(model, cancellationToken) => ...and pass the token to each prompt call.Docs
Added a "Canceling from code" section (en / ja) and a README snippet. Also corrected the existing text that said Ctrl+C returns the default value; it actually exits the process (via
Environment.Exit(1)) unlessThrowExceptionOnCancelis set.Tests
Registeroverloads, null checks, andBindpassing the token to the binder.xUnit1051is suppressed in the test project: forms under test read fromFakeConsoleDriver, which never blocks.DefaultConsoleDriverin a real console window: cancellation fires within ~10 ms of the deadline across consecutive prompts, the next prompt renders below the canceled one,TreatControlCAsInputis restored, and the generated binder for a[PromptBindable]model receives the token.🤖 Generated with Claude Code