From af42b0468c4536d5cbd2afaf9c25d94690ea86e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BE=D0=BD=D1=81=D1=82=D0=B0=D0=BD=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=20=D0=94=D1=8C=D1=8F=D1=87=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sun, 21 Jul 2019 13:30:21 +0300 Subject: [PATCH] Version 0.0.3. Console (Standard IO) related classed forked from: https://github.com/Konard/LinksPlatform --- ConsoleCancellationHandler.cs | 55 +++++++++++++++++++++++++++++++++++ ConsoleHelpers.cs | 35 ++++++++++++++++++++++ Platform.IO.csproj | 5 ++-- 3 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 ConsoleCancellationHandler.cs create mode 100644 ConsoleHelpers.cs 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 @@ +