4 using System.Threading.Tasks;
12 public bool Available => Environment.UserInteractive;
15 public CancellationToken CancelKeyPress => cancelKeyCts.Token;
32 cancelKeyCts =
new CancellationTokenSource();
33 global::System.Console.CancelKeyPress += (sender, e) =>
38 cancelKeyCts.Cancel();
48 cancelKeyCts.Dispose();
56 throw new InvalidOperationException(
"Console unavailable");
60 public Task PressAnyKeyAsync(CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
63 global::System.Console.Read();
64 }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
67 public Task<string> ReadLineAsync(
bool usePasswordChar, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
72 return global::System.Console.ReadLine();
74 var passwordBuilder =
new StringBuilder();
77 var keyDescription = global::System.Console.ReadKey(
true);
78 if (keyDescription.Key == ConsoleKey.Enter)
80 else if (keyDescription.Key == ConsoleKey.Backspace)
82 if (passwordBuilder.Length > 0)
84 --passwordBuilder.Length;
85 global::System.Console.Write(
"\b \b");
88 else if (keyDescription.KeyChar !=
'\u0000')
91 passwordBuilder.Append(keyDescription.KeyChar);
92 global::System.Console.Write(
'*');
95 while (!cancellationToken.IsCancellationRequested);
97 cancellationToken.ThrowIfCancellationRequested();
98 global::System.Console.WriteLine();
99 return passwordBuilder.ToString();
100 }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
103 public Task WriteAsync(
string text,
bool newLine, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
109 throw new InvalidOperationException(
"Cannot write null text without a new line!");
110 global::System.Console.WriteLine();
113 global::System.Console.WriteLine(text);
115 global::System.Console.Write(text);
116 }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
readonly CancellationTokenSource cancelKeyCts
The CancellationTokenSource for CancelKeyPress.
Abstraction for global::System.Console
bool disposed
If the Console was disposed;
Console()
Initializes a new instance of the Console .