Skip to content

Commit

Permalink
Version 0.0.3. Console (Standard IO) related classed forked from: htt…
Browse files Browse the repository at this point in the history
  • Loading branch information
Konard committed Jul 21, 2019
1 parent 6420706 commit af42b04
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
55 changes: 55 additions & 0 deletions ConsoleCancellationHandler.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
35 changes: 35 additions & 0 deletions ConsoleHelpers.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
5 changes: 3 additions & 2 deletions Platform.IO.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<Description>LinksPlatform's Platform.IO Class Library</Description>
<Copyright>Konstantin Diachenko</Copyright>
<AssemblyTitle>Platform.IO</AssemblyTitle>
<VersionPrefix>0.0.2</VersionPrefix>
<VersionPrefix>0.0.3</VersionPrefix>
<Authors>Konstantin Diachenko</Authors>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>Platform.IO</AssemblyName>
<PackageId>Platform.IO</PackageId>
<PackageTags>LinksPlatform;IO;FileHelpers;StreamExtensions</PackageTags>
<PackageTags>LinksPlatform;IO;FileHelpers;StreamExtensions;ConsoleHelpers;ConsoleCancellationHandler</PackageTags>
<PackageIconUrl>https://raw.githubusercontent.com/linksplatform/Documentation/0a9177c11892ebb2fe467dc3ea9eaf5a5588f610/doc/Avatar-rainbow.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/linksplatform/IO</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/linksplatform/IO/blob/master/LICENSE</PackageLicenseUrl>
Expand All @@ -24,6 +24,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Platform.Threading" Version="0.0.1" />
<PackageReference Include="Platform.Unsafe" Version="0.0.1" />
</ItemGroup>

Expand Down

0 comments on commit af42b04

Please sign in to comment.