diff --git a/ConsoleCancellationHandler.cs b/ConsoleCancellationHandler.cs new file mode 100644 index 0000000..88ece09 --- /dev/null +++ b/ConsoleCancellationHandler.cs @@ -0,0 +1,55 @@ +using System; +using System.Threading; +using Platform.Disposables; +using Platform.Threading; + +namespace Platform.IO +{ + public class ConsoleCancellationHandler : DisposableBase + { + public CancellationTokenSource Source { get; private set; } + public CancellationToken Token { get; private set; } + + public bool IsCancellationRequested => Source.IsCancellationRequested; + + public bool NoCancellationRequested => !Source.IsCancellationRequested; + + public ConsoleCancellationHandler(bool showDefaultIntroMessage = true) + { + if (showDefaultIntroMessage) + Console.WriteLine("Press CTRL+C to stop."); + + Source = new CancellationTokenSource(); + Token = Source.Token; + + Console.CancelKeyPress += OnCancelKeyPress; + } + + public void ForceCancellation() => Source.Cancel(); + + private void OnCancelKeyPress(object sender, ConsoleCancelEventArgs e) + { + e.Cancel = true; + + if (!Source.IsCancellationRequested) + { + Source.Cancel(); + Console.WriteLine("Stopping..."); + } + } + + public void Wait() + { + while (NoCancellationRequested) + ThreadHelpers.Sleep(); + } + + protected override void DisposeCore(bool manual, bool wasDisposed) + { + if (!wasDisposed) + Console.CancelKeyPress -= OnCancelKeyPress; + + Disposable.TryDispose(Source); + } + } +} diff --git a/ConsoleHelpers.cs b/ConsoleHelpers.cs new file mode 100644 index 0000000..d63c2ff --- /dev/null +++ b/ConsoleHelpers.cs @@ -0,0 +1,35 @@ +using System; +using System.Diagnostics; + +namespace Platform.IO +{ + public static class ConsoleHelpers + { + public static void PressAnyKeyToContinue() + { + // TODO: Internationalization + Console.WriteLine("Press any key to continue."); + Console.ReadKey(); + } + + public static string GetOrReadArgument(int index, params string[] args) => GetOrReadArgument(index, $"{index + 1} argument", args); + + public static string GetOrReadArgument(int index, string readMessage, params string[] args) + { + string result; + + if (args != null && args.Length > index) + result = args[index]; + else + { + Console.Write($"{readMessage}: "); + result = Console.ReadLine(); + } + + return (result ?? "").Trim().Trim('"').Trim(); + } + + [Conditional("DEBUG")] + public static void Debug(string format, params object[] args) => Console.WriteLine(format, args); + } +} diff --git a/Platform.IO.csproj b/Platform.IO.csproj index de8e008..94cad28 100644 --- a/Platform.IO.csproj +++ b/Platform.IO.csproj @@ -4,12 +4,12 @@ LinksPlatform's Platform.IO Class Library Konstantin Diachenko Platform.IO - 0.0.2 + 0.0.3 Konstantin Diachenko netstandard2.0 Platform.IO Platform.IO - LinksPlatform;IO;FileHelpers;StreamExtensions + LinksPlatform;IO;FileHelpers;StreamExtensions;ConsoleHelpers;ConsoleCancellationHandler https://raw.githubusercontent.com/linksplatform/Documentation/0a9177c11892ebb2fe467dc3ea9eaf5a5588f610/doc/Avatar-rainbow.png https://github.com/linksplatform/IO https://github.com/linksplatform/IO/blob/master/LICENSE @@ -24,6 +24,7 @@ +