namespace BuildTool.Interactive;
///
/// Manages cancellation for interactive mode.
/// When Ctrl+C is pressed, the current token is cancelled, causing prompts to throw OperationCanceledException.
/// The token source is then reset so the next prompt can work.
///
public sealed class InteractiveCancellation : IDisposable
{
private CancellationTokenSource _cts = new();
private readonly object _lock = new();
///
/// Gets the current cancellation token to pass to prompts.
///
public CancellationToken Token
{
get
{
lock (_lock)
{
return _cts.Token;
}
}
}
///
/// Signals cancellation (called from Ctrl+C handler).
///
public void Cancel()
{
lock (_lock)
{
_cts.Cancel();
}
}
///
/// Resets the cancellation token source so new prompts can work.
/// Call this after catching OperationCanceledException.
///
public void Reset()
{
lock (_lock)
{
_cts.Dispose();
_cts = new CancellationTokenSource();
}
}
/// Global instance for app-wide interactive cancellation.
public static InteractiveCancellation Instance { get; } = new();
public void Dispose()
{
_cts.Dispose();
}
}