From 98dfe9e421de6d0e08101a395e0056de2ef6c905 Mon Sep 17 00:00:00 2001 From: Fernando Cerqueira Date: Mon, 10 Aug 2026 11:28:02 -0300 Subject: [PATCH 1/5] Honor HideOnAbort on external cancellation (Ctrl+C) BaseControlPrompt.Run()'s external-cancellation branch only cleared the frame for Live auto-render controls, so Ctrl+C (or a caller-supplied CancellationToken) never honored HideOnAbort for interactive controls regardless of how it was configured. Clear now when the control is Live OR HideOnAbort is set, relying on ConsolePlus's new bounded grace period to give the render loop a real chance to finish before the process exits. Also correct global-behaviors.md, which documented Ctrl+C as an internal abort gated by RemoveHandlerCtrlC -- that was never implemented; Ctrl+C has always hard-exited the process. RemoveHandlerCtrlC is now noted as reserved/currently unused. Co-Authored-By: Claude Sonnet 5 --- docs/global-behaviors.md | 8 +-- src/Controls/Common/BaseControlPrompt.cs | 23 ++++++-- .../Controls/CancelAbortRenderTests.cs | 57 +++++++++++++++++++ 3 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 tests/PromptPlus.Tests/Controls/CancelAbortRenderTests.cs diff --git a/docs/global-behaviors.md b/docs/global-behaviors.md index 14e3ce48..39933171 100644 --- a/docs/global-behaviors.md +++ b/docs/global-behaviors.md @@ -55,8 +55,8 @@ Set these on `PromptPlus.Config` before running any controls. Changes take effec | `ShowMessageAbortKey` | `bool` | `true` | Includes the abort-key name in tooltips | | `ShowTooltip` | `bool` | `true` | Shows keyboard hints below prompts | | `HideAfterFinish` | `bool` | `false` | Erases control UI after the user confirms | -| `HideOnAbort` | `bool` | `false` | Erases control UI when the user presses Esc | -| `RemoveHandlerCtrlC` | `bool` | `false` | When `true`, Ctrl+C is passed to the OS instead of triggering abort | +| `HideOnAbort` | `bool` | `false` | Erases control UI when the user presses Esc, or when the run is cancelled externally (Ctrl+C, or a caller-supplied `CancellationToken`) | +| `RemoveHandlerCtrlC` | `bool` | `false` | Reserved for future use; currently has no effect | ### Calendar @@ -198,8 +198,8 @@ PromptPlus.Controls | Single-line rendering | Newlines stripped from display; sliding window with `…` when value is too wide; `result.Content` always holds the original full value | | History persistence | Last confirmed value saved to disk; pre-loaded on next run; configurable via `IHistoryOptions` | | HideAfterFinish | Control UI erased after confirmation; only the final answer line remains | -| HideOnAbort | Control UI erased when user presses Esc | -| Ctrl+C handling | Intercepted by default → triggers abort; `RemoveHandlerCtrlC = true` passes to OS | +| HideOnAbort | Control UI erased when the user presses Esc, or when the run is cancelled externally (Ctrl+C, or a caller-supplied `CancellationToken`) | +| Ctrl+C handling | Always terminates the process (via ConsolePlus's `Console.CancelKeyPress` handler) — it is not an internal abort and the control's `Run()` call never returns. The current control gets a short, bounded grace period to run its own cancellation cleanup first (honoring `HideOnAbort` for interactive controls; Live controls — ProgressBar, Task, MultiTasks, Timer — always clear their frame). `RemoveHandlerCtrlC` is reserved for future use and currently has no effect | | Tooltip visibility | `ShowTooltip = true` shows keyboard hints below the prompt | | Abort key hint | `ShowMessageAbortKey = true` includes the abort-key name in the tooltip | | Auto-initialization | PromptPlus initializes on first access: detects terminal, loads `PromptPlus.config` if present, registers error log hook | diff --git a/src/Controls/Common/BaseControlPrompt.cs b/src/Controls/Common/BaseControlPrompt.cs index 4c94c2a4..1d8a83fd 100644 --- a/src/Controls/Common/BaseControlPrompt.cs +++ b/src/Controls/Common/BaseControlPrompt.cs @@ -214,6 +214,12 @@ public ResultPrompt Run(CancellationToken stoptoken = default) using CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(stoptoken, console.CancelToken); + // Gives Ctrl+C a short, bounded grace period (see ConsolePlus.BeginCriticalRender) + // to let the abort cleanup below (the HideOnAbortValue-driven clear, cursor/color + // restoration) finish instead of the process exiting mid-write. Scoped to the whole + // method body via a using-declaration so it also covers the finally block's restore. + using IDisposable criticalRenderScope = ConsolePlus.BeginCriticalRender(); + bool oldcursor = console.CursorVisible; Color oldforecolor = console.ForegroundColor; Color oldbackcolor = console.BackgroundColor; @@ -658,14 +664,19 @@ public ResultPrompt Run(CancellationToken stoptoken = default) } } } - else if (cts.Token.IsCancellationRequested && !isWidget && IsLiveAutoRenderControl) + else if (cts.Token.IsCancellationRequested && !isWidget + && (IsLiveAutoRenderControl || OptionsControl.HideOnAbortValue)) { // The run was cancelled externally (CancellationToken/Ctrl+C) so the finish - // block above is skipped. Live controls can render a tall multi-line frame - // (e.g. a paginated task list); leaving that footprint on screen produces - // leftover rows and interferes with terminal auto-scroll. Clear the control's - // own rendered area and park the cursor at its top so subsequent output starts - // cleanly. Interactive controls keep their previous cancel behavior. + // block above (which normally honors HideOnAbortValue for an internal Esc-abort) + // is skipped entirely. Without this branch, HideOnAbortValue was silently + // ignored on Ctrl+C for interactive controls, leaving the frame on screen no + // matter how it was configured. Live controls always clear here regardless of + // HideOnAbortValue: they can render a tall multi-line frame (e.g. a paginated + // task list), and leaving that footprint on screen produces leftover rows and + // interferes with terminal auto-scroll — clearing it is required for the + // terminal to stay usable, not just a presentation preference like + // HideOnAbortValue is for interactive controls. for (int row = 0; row <= _bufferScreen.OriginalLineCount; row++) { console.SetCursorPosition(0, _screenPosition.StartTop + row); diff --git a/tests/PromptPlus.Tests/Controls/CancelAbortRenderTests.cs b/tests/PromptPlus.Tests/Controls/CancelAbortRenderTests.cs new file mode 100644 index 00000000..712cbfd6 --- /dev/null +++ b/tests/PromptPlus.Tests/Controls/CancelAbortRenderTests.cs @@ -0,0 +1,57 @@ +using ConsolePlusLibrary.Testing; +using FluentAssertions; +using PromptPlusLibrary; +using PromptPlusLibrary.Core; +using System; +using System.Threading; +using Xunit; + +namespace PromptPlus.Tests.Controls +{ + // BaseControlPrompt's external-cancellation cleanup path (the `else if + // (cts.Token.IsCancellationRequested ...)` branch in Run(), Controls/Common/ + // BaseControlPrompt.cs) that reacts to Ctrl+C / an external CancellationToken (as opposed to + // an internal Escape-abort, which goes through the finish-block branch instead and is covered + // by each control's own tests). There is no real Console.CancelKeyPress in a headless test, so + // cancellation is simulated with the same "safety-net CancellationTokenSource" technique + // already used across the suite (e.g. InputControlTests): no terminal key is queued, so + // WaitKeypress spins until the token fires. Input is used as the vehicle; the cleanup logic + // itself lives entirely in the base class and applies to every interactive (non-Live) control. + public class CancelAbortRenderTests + { + private static VirtualTerminal MakeTerminal() => VirtualTerminal.Create(o => { o.SupportsUnicode = false; }); + + [Fact] + public void External_cancellation_clears_the_frame_when_HideOnAbort_is_true() + { + var vt = MakeTerminal(); + var control = new PromptPlusControls(vt, new PromptConfig()).Input("Name") + .Options(o => o.HideOnAbort()); + _ = vt.Keys.Type("Joe"); + + using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(1000)); + var result = control.Run(cts.Token); + + _ = result.IsAborted.Should().BeTrue(); + _ = vt.Find("Joe").Should().BeNull(); + _ = vt.Find("Name").Should().BeNull(); + } + + [Fact] + public void External_cancellation_leaves_the_frame_when_HideOnAbort_is_false() + { + var vt = MakeTerminal(); + // Default HideOnAbortValue (no .Options call at all) is already false + // (PromptConfig.HideOnAbort defaults to false) — set explicitly here for clarity. + var control = new PromptPlusControls(vt, new PromptConfig()).Input("Name") + .Options(o => o.HideOnAbort(false)); + _ = vt.Keys.Type("Joe"); + + using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(1000)); + var result = control.Run(cts.Token); + + _ = result.IsAborted.Should().BeTrue(); + _ = vt.TextAt(0, 0, 9).Should().Be("Name: Joe"); + } + } +} From cec81282a6fb48e7550ab56ba459ec87a9daa15a Mon Sep 17 00:00:00 2001 From: Fernando Cerqueira Date: Mon, 10 Aug 2026 11:28:07 -0300 Subject: [PATCH 2/5] Bump version to 6.0.0-rc2 and update ConsolePlus.net dependency to 1.0.0-rc2 Co-Authored-By: Claude Sonnet 5 --- src/PromptPlus.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PromptPlus.csproj b/src/PromptPlus.csproj index d6f4a53c..bb928519 100644 --- a/src/PromptPlus.csproj +++ b/src/PromptPlus.csproj @@ -34,7 +34,7 @@ PromptPlus README.md https://github.com/FRACerqueira/PromptPlus - 6.0.0-rc1 + 6.0.0-rc2 icon.png © 2026 - Fernando Cerqueira false @@ -69,7 +69,7 @@ - + From 281ace4cd6a1a9f4331dddf5f5c4156106937356 Mon Sep 17 00:00:00 2001 From: Fernando Cerqueira Date: Mon, 10 Aug 2026 11:59:39 -0300 Subject: [PATCH 3/5] Enable automatic NuGet package generation on build Changed GeneratePackageOnBuild in PromptPlus.csproj from false to true, so building the project now automatically creates a NuGet package. This streamlines the packaging process and ensures packages are always up to date with each build. --- src/PromptPlus.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PromptPlus.csproj b/src/PromptPlus.csproj index bb928519..e139e888 100644 --- a/src/PromptPlus.csproj +++ b/src/PromptPlus.csproj @@ -37,7 +37,7 @@ 6.0.0-rc2 icon.png © 2026 - Fernando Cerqueira - false + True PromptPlus MIT True From 03817a52ba666db4ce6ac05916bb87c8b65a224f Mon Sep 17 00:00:00 2001 From: Fernando Cerqueira Date: Mon, 10 Aug 2026 13:24:29 -0300 Subject: [PATCH 4/5] ci: gate tests on .cs changes only and avoid full-history clone Only run the test job when the diff touches a .cs file, skipping it for doc/config-only changes. Also drop the full-history checkout in the changes job (this repo's .git carries ~800MB of since-removed docs/images/*.gif) in favor of resolving changed files via the GitHub API, which was making every CI run needlessly slow. Co-Authored-By: Claude Sonnet 5 --- .github/workflows/ci.yml | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 36821c3d..aa02ef1a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,31 +12,26 @@ permissions: pull-requests: read jobs: - # Detects whether the push/PR touches anything besides docs/*.md. Doc-only changes - # still get a build (below) but skip the test run entirely. + # Detects whether the push/PR touches any .cs file. Changes that don't + # (docs, yml, csproj, etc.) still get a build (below) but skip the test run. changes: name: Detect changed paths runs-on: ubuntu-latest outputs: code: ${{ steps.filter.outputs.code }} steps: - - name: Checkout - uses: actions/checkout@v4 - with: - # dorny/paths-filter needs the pre-push commit to diff against on push - # events; a shallow (depth-1) clone doesn't have it, so it falls back to - # treating everything as changed. Full history avoids that. - fetch-depth: 0 - + # No checkout here on purpose: this repo's .git history carries ~800MB of + # since-removed docs/images/*.gif, so a full-history clone (fetch-depth: 0) + # made every run slow. dorny/paths-filter can resolve changed files via the + # GitHub API instead, as long as there's no local checkout to diff against. - name: Filter changed paths uses: dorny/paths-filter@v3 id: filter with: + token: ${{ secrets.GITHUB_TOKEN }} filters: | code: - - '**' - - '!**/*.md' - - '!docs/**' + - '**/*.cs' # Single job, OS matrix only (net10.0 tests, not the full net8/9/10 TFM axis). build-and-test: @@ -71,8 +66,8 @@ jobs: - name: Build library (Release) run: dotnet build src/PromptPlus.csproj --configuration Release - # Skipped for doc-only changes (docs/** and *.md) — those only need a build to - # verify nothing broke, not a full test pass. See ADR0022. + # Skipped when the change doesn't touch any .cs file — those only need a + # build to verify nothing broke, not a full test pass. See ADR0022. - name: Test (net10.0 only) if: needs.changes.outputs.code == 'true' run: > From de2f821d23ddceab7b512f0e0cd6eccc892f0724 Mon Sep 17 00:00:00 2001 From: Fernando Cerqueira Date: Mon, 10 Aug 2026 13:32:55 -0300 Subject: [PATCH 5/5] ci: don't fail the changes job when path-filter can't detect changes on push dorny/paths-filter's API mode only works for pull_request events; on push events (e.g. a merge commit to main) there's no local checkout to diff against and the step errors out, failing the whole job and skipping the build entirely. continue-on-error lets it degrade to "skip tests" instead. --- .github/workflows/ci.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index aa02ef1a..0c69d6aa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,9 +24,15 @@ jobs: # since-removed docs/images/*.gif, so a full-history clone (fetch-depth: 0) # made every run slow. dorny/paths-filter can resolve changed files via the # GitHub API instead, as long as there's no local checkout to diff against. + # That API mode only works for pull_request events though — on push + # events (e.g. a merge commit landing on main) it has no local repo to + # diff against and fails. continue-on-error keeps that failure from + # failing the whole job: outputs.code just stays unset, which the + # Test step below already treats as "skip". - name: Filter changed paths uses: dorny/paths-filter@v3 id: filter + continue-on-error: true with: token: ${{ secrets.GITHUB_TOKEN }} filters: |