-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Version 0.0.3. Console (Standard IO) related classed forked from: htt…
- Loading branch information
Showing
3 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters