/
Terminal.Interaction.cs
255 lines (219 loc) · 8.58 KB
/
Terminal.Interaction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using System;
using System.Collections.Generic;
using Swan.Lite.Logging;
using System.Globalization;
using Swan.Logging;
namespace Swan
{
/// <summary>
/// A console terminal helper to create nicer output and receive input from the user
/// This class is thread-safe :).
/// </summary>
public static partial class Terminal
{
#region ReadKey
/// <summary>
/// Reads a key from the Terminal. This is the closest equivalent to Console.ReadKey.
/// </summary>
/// <param name="intercept">if set to <c>true</c> the pressed key will not be rendered to the output.</param>
/// <param name="disableLocking">if set to <c>true</c> the output will continue to be shown.
/// This is useful for services and daemons that are running as console applications and wait for a key to exit the program.</param>
/// <returns>The console key information.</returns>
public static ConsoleKeyInfo ReadKey(bool intercept, bool disableLocking = false)
{
if (!IsConsolePresent) return default;
if (disableLocking) return Console.ReadKey(intercept);
lock (SyncLock)
{
Flush();
try
{
Console.CursorVisible = true;
return Console.ReadKey(intercept);
}
finally
{
Console.CursorVisible = false;
}
}
}
/// <summary>
/// Reads a key from the Terminal.
/// </summary>
/// <param name="prompt">The prompt.</param>
/// <param name="preventEcho">if set to <c>true</c> [prevent echo].</param>
/// <returns>The console key information.</returns>
public static ConsoleKeyInfo ReadKey(string prompt, bool preventEcho = true)
{
if (!IsConsolePresent) return default;
lock (SyncLock)
{
if (prompt != null)
Write($"{GetNowFormatted()}{Settings.UserInputPrefix} << {prompt} ", ConsoleColor.White);
var input = ReadKey(true);
var echo = preventEcho ? string.Empty : input.Key.ToString();
WriteLine(echo);
return input;
}
}
#endregion
#region Other Terminal Read Methods
/// <summary>
/// Clears the screen.
/// </summary>
public static void Clear()
{
Flush();
Console.Clear();
}
/// <summary>
/// Reads a line of text from the console.
/// </summary>
/// <returns>The read line.</returns>
public static string? ReadLine()
{
if (!IsConsolePresent) return default;
lock (SyncLock)
{
Flush();
try
{
Console.CursorVisible = true;
return Console.ReadLine();
}
finally
{
Console.CursorVisible = false;
}
}
}
/// <summary>
/// Reads a line from the input.
/// </summary>
/// <param name="prompt">The prompt.</param>
/// <returns>The read line.</returns>
public static string? ReadLine(string prompt)
{
if (!IsConsolePresent) return null;
lock (SyncLock)
{
Write($"{GetNowFormatted()}{Settings.UserInputPrefix} << {prompt}: ", ConsoleColor.White);
return ReadLine();
}
}
/// <summary>
/// Reads a number from the input. If unable to parse, it returns the default number.
/// </summary>
/// <param name="prompt">The prompt.</param>
/// <param name="defaultNumber">The default number.</param>
/// <returns>
/// Conversions of string representation of a number to its 32-bit signed integer equivalent.
/// </returns>
public static int ReadNumber(string prompt, int defaultNumber)
{
if (!IsConsolePresent) return defaultNumber;
lock (SyncLock)
{
Write($"{GetNowFormatted()}{Settings.UserInputPrefix} << {prompt} (default is {defaultNumber}): ",
ConsoleColor.White);
var input = ReadLine();
return int.TryParse(input, out var parsedInt) ? parsedInt : defaultNumber;
}
}
/// <summary>
/// Creates a table prompt where the user can enter an option based on the options dictionary provided.
/// </summary>
/// <param name="title">The title.</param>
/// <param name="options">The options.</param>
/// <param name="anyKeyOption">Any key option.</param>
/// <returns>
/// A value that identifies the console key that was pressed.
/// </returns>
/// <exception cref="ArgumentNullException">options.</exception>
public static ConsoleKeyInfo ReadPrompt(
string title,
IDictionary<ConsoleKey, string> options,
string anyKeyOption)
{
if (!IsConsolePresent) return default;
if (options == null)
throw new ArgumentNullException(nameof(options));
const ConsoleColor textColor = ConsoleColor.White;
var lineLength = Console.WindowWidth;
var lineAlign = -(lineLength - 2);
var textFormat = "{0," + lineAlign + "}";
// lock the output as an atomic operation
lock (SyncLock)
{
{
// Top border
Table.TopLeft();
Table.Horizontal(-lineAlign);
Table.TopRight();
}
{
// Title
Table.Vertical();
var titleText = string.Format(CultureInfo.CurrentCulture,
textFormat,
string.IsNullOrWhiteSpace(title) ? " Select an option from the list below." : $" {title}");
Write(titleText, textColor);
Table.Vertical();
}
{
// Title Bottom
Table.LeftTee();
Table.Horizontal(lineLength - 2);
Table.RightTee();
}
// Options
foreach (var kvp in options)
{
Table.Vertical();
Write(string.Format(
CultureInfo.CurrentCulture,
textFormat,
$" {"[ " + kvp.Key + " ]",-10} {kvp.Value}"),
textColor);
Table.Vertical();
}
// Any Key Options
if (string.IsNullOrWhiteSpace(anyKeyOption) == false)
{
Table.Vertical();
Write(string.Format(CultureInfo.CurrentCulture, textFormat, " "), ConsoleColor.Gray);
Table.Vertical();
Table.Vertical();
Write(string.Format(
CultureInfo.CurrentCulture,
textFormat,
$" {" ",-10} {anyKeyOption}"),
ConsoleColor.Gray);
Table.Vertical();
}
{
// Input section
Table.LeftTee();
Table.Horizontal(lineLength - 2);
Table.RightTee();
Table.Vertical();
Write(string.Format(CultureInfo.CurrentCulture, textFormat, Settings.UserOptionText),
ConsoleColor.Green);
Table.Vertical();
Table.BottomLeft();
Table.Horizontal(lineLength - 2);
Table.BottomRight();
}
}
var inputLeft = Settings.UserOptionText.Length + 3;
SetCursorPosition(inputLeft, CursorTop - 1);
var userInput = ReadKey(true);
Write(userInput.Key.ToString(), ConsoleColor.Gray);
SetCursorPosition(0, CursorTop + 2);
return userInput;
}
#endregion
private static string GetNowFormatted() =>
$" {(string.IsNullOrWhiteSpace(TextLogger.LoggingTimeFormat) ? string.Empty : DateTime.Now.ToString(TextLogger.LoggingTimeFormat) + " ")}";
}
}