4 using System.Threading.Tasks;
12 public bool Available => Environment.UserInteractive;
17 throw new InvalidOperationException(
"Console unavailable");
21 public Task PressAnyKeyAsync(CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
25 }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
28 public Task<string> ReadLineAsync(
bool usePasswordChar, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
33 return System.Console.ReadLine();
35 var passwordBuilder =
new StringBuilder();
38 var keyDescription =
System.Console.ReadKey(
true);
39 if (keyDescription.Key == ConsoleKey.Enter)
41 else if (keyDescription.Key == ConsoleKey.Backspace)
43 if (passwordBuilder.Length > 0)
45 --passwordBuilder.Length;
46 System.Console.Write(
"\b \b");
49 else if (keyDescription.KeyChar !=
'\u0000')
51 passwordBuilder.Append(keyDescription.KeyChar);
55 while (!cancellationToken.IsCancellationRequested);
56 cancellationToken.ThrowIfCancellationRequested();
57 System.Console.WriteLine();
58 return passwordBuilder.ToString();
59 }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
62 public Task WriteAsync(
string text,
bool newLine, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
68 throw new InvalidOperationException(
"Cannot write null text without a new line!");
69 System.Console.WriteLine();
72 System.Console.WriteLine(text);
74 System.Console.Write(text);
75 }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
Abstraction for System.Console