Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,32 @@ 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.
# 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: |
code:
- '**'
- '!**/*.md'
- '!docs/**'
- '**/*.cs'

# Single job, OS matrix only (net10.0 tests, not the full net8/9/10 TFM axis).
build-and-test:
Expand Down Expand Up @@ -71,8 +72,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: >
Expand Down
8 changes: 4 additions & 4 deletions docs/global-behaviors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 |
Expand Down
23 changes: 17 additions & 6 deletions src/Controls/Common/BaseControlPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ public ResultPrompt<T> 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;
Expand Down Expand Up @@ -658,14 +664,19 @@ public ResultPrompt<T> 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);
Expand Down
6 changes: 3 additions & 3 deletions src/PromptPlus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
<PackageId>PromptPlus</PackageId>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageProjectUrl>https://github.com/FRACerqueira/PromptPlus</PackageProjectUrl>
<Version>6.0.0-rc1</Version>
<Version>6.0.0-rc2</Version>
<PackageIcon>icon.png</PackageIcon>
<Copyright>© 2026 - Fernando Cerqueira</Copyright>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>PromptPlus</Title>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
Expand Down Expand Up @@ -69,7 +69,7 @@
<ItemGroup>
<!-- Enables FileHistory to be tested against a MockFileSystem instead of the real user profile. -->
<PackageReference Include="System.IO.Abstractions" Version="*" />
<PackageReference Include="ConsolePlus.net" Version="1.0.0-rc1" />
<PackageReference Include="ConsolePlus.net" Version="1.0.0-rc2" />
</ItemGroup>

<PropertyGroup Condition="'$(Configuration)' == 'ReleaseDoc' and '$(TargetFramework)' == 'net10.0'">
Expand Down
57 changes: 57 additions & 0 deletions tests/PromptPlus.Tests/Controls/CancelAbortRenderTests.cs
Original file line number Diff line number Diff line change
@@ -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");
}
}
}