tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
Console.cs
Go to the documentation of this file.
1using System;
2using System.Text;
3using System.Threading;
4using System.Threading.Tasks;
5
7
9{
14 {
16 public string? Title => platformIdentifier.IsWindows
17 ? global::System.Console.Title
18 : null;
19
21 public bool Available => Environment.UserInteractive;
22
24 public CancellationToken CancelKeyPress => cancelKeyCts.Token;
25
30
34 readonly CancellationTokenSource cancelKeyCts;
35
40
46 {
47 this.platformIdentifier = platformIdentifier ?? throw new ArgumentNullException(nameof(platformIdentifier));
48
49 cancelKeyCts = new CancellationTokenSource();
50 global::System.Console.CancelKeyPress += (sender, e) =>
51 {
52 lock (cancelKeyCts)
53 {
54 if (!disposed)
55 cancelKeyCts.Cancel();
56 }
57 };
58 }
59
61 public void Dispose()
62 {
63 lock (cancelKeyCts)
64 {
65 cancelKeyCts.Dispose();
66 disposed = true;
67 }
68 }
69
71 public Task PressAnyKeyAsync(CancellationToken cancellationToken) => Task.Factory.StartNew(
72 () =>
73 {
75 global::System.Console.Read();
76 },
77 cancellationToken,
79 TaskScheduler.Current);
80
82 public Task<string> ReadLineAsync(bool usePasswordChar, CancellationToken cancellationToken) => Task.Factory.StartNew(
83 () =>
84 {
85 // TODO: Make this better: https://stackoverflow.com/questions/9479573/how-to-interrupt-console-readline
87 if (!usePasswordChar)
88 return global::System.Console.ReadLine()
89 ?? throw new InvalidOperationException("Console input has been closed!");
90
91 var passwordBuilder = new StringBuilder();
92 do
93 {
94 var keyDescription = global::System.Console.ReadKey(true);
95 if (keyDescription.Key == ConsoleKey.Enter)
96 break;
97 else if (keyDescription.Key == ConsoleKey.Backspace)
98 {
99 if (passwordBuilder.Length > 0)
100 {
101 --passwordBuilder.Length;
102 global::System.Console.Write("\b \b");
103 }
104 }
105 else if (keyDescription.KeyChar != '\u0000')
106 {
107 // KeyChar == '\u0000' if the key pressed does not correspond to a printable character, e.g. F1, Pause-Break, etc
108 passwordBuilder.Append(keyDescription.KeyChar);
109 global::System.Console.Write('*');
110 }
111 }
112 while (!cancellationToken.IsCancellationRequested);
113
114 cancellationToken.ThrowIfCancellationRequested();
115 global::System.Console.WriteLine();
116 return passwordBuilder.ToString();
117 },
118 cancellationToken,
120 TaskScheduler.Current)
121 .WaitAsync(cancellationToken);
122
124 public Task WriteAsync(string? text, bool newLine, CancellationToken cancellationToken) => Task.Factory.StartNew(
125 () =>
126 {
128 if (text == null)
129 {
130 if (!newLine)
131 throw new InvalidOperationException("Cannot write null text without a new line!");
132 global::System.Console.WriteLine();
133 }
134 else if (newLine)
135 global::System.Console.WriteLine(text);
136 else
137 global::System.Console.Write(text);
138 },
139 cancellationToken,
141 TaskScheduler.Current);
142
144 public void SetTitle(string newTitle)
145 {
146 ArgumentNullException.ThrowIfNull(newTitle);
147 global::System.Console.Title = newTitle;
148 }
149
154 {
155 if (!Available)
156 throw new InvalidOperationException("Console unavailable");
157 }
158 }
159}
Implementation of IConsole, uses global::System.Console.
Definition Console.cs:14
Console(IPlatformIdentifier platformIdentifier)
Initializes a new instance of the Console class.
Definition Console.cs:45
readonly CancellationTokenSource cancelKeyCts
The CancellationTokenSource for CancelKeyPress.
Definition Console.cs:34
void SetTitle(string newTitle)
Sets a newTitle console window.
Definition Console.cs:144
Task WriteAsync(string? text, bool newLine, CancellationToken cancellationToken)
Write some text to the IConsole.A Task representing the running operation.
void CheckAvailable()
Assert that the Console is available, throwing an InvalidOperationException otherwise.
Definition Console.cs:153
bool Available
If the IConsole is visible to the user.
Definition Console.cs:21
readonly IPlatformIdentifier platformIdentifier
The IPlatformIdentifier for the Console.
Definition Console.cs:29
string? Title
Gets or sets the IConsole window's title. Can return null if getting the console title is not support...
Definition Console.cs:16
bool disposed
If the Console was disposed.
Definition Console.cs:39
CancellationToken CancelKeyPress
Gets a CancellationToken that triggers if Crtl+C or an equivalent is pressed.
Definition Console.cs:24
Task PressAnyKeyAsync(CancellationToken cancellationToken)
Wait for a key press on the IConsole.A Task representing the running operation.
Task< string > ReadLineAsync(bool usePasswordChar, CancellationToken cancellationToken)
Read a line from the IConsole.A Task<TResult> resulting in the string read by the IConsole.
IIOManager that resolves paths to Environment.CurrentDirectory.
const TaskCreationOptions BlockingTaskCreationOptions
The TaskCreationOptions used to spawn Tasks for potentially long running, blocking operations.
Abstraction for global::System.Console.
Definition IConsole.cs:10
For identifying the current platform.