-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoPrompt.cs
More file actions
469 lines (402 loc) · 18.8 KB
/
AutoPrompt.cs
File metadata and controls
469 lines (402 loc) · 18.8 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
//Original: https://github.com/rohankapoor/AutoPrompt
namespace VinaCarbResults
{
public class AutoPrompt
{
#region Public APIs
/// <summary>
/// Used to get path from command prompt. Starts by showing available directories
/// Press:
/// Up Arrow to go to next file or directory
/// Down Arrow to go to previous file or directory
/// '\' to navigate into current directory
/// Any key to list matching files and directory. The search criteria is 'begins with'. Eg, When 'W' is pressed in C:\, the prompt will auto fill with 'Windows' as the closest match. The search is case sensitive
/// Enter to return with the current value
/// </summary>
/// <param name="message">Message that appears on the prompt. For eg, Enter Value:</param>
/// <returns>Full path of the file or directory selected</returns>
public static string GetPath(string message)
{
string[] options = Environment.GetLogicalDrives();
for (int optionsIndex = 0; optionsIndex < options.Length; optionsIndex++)
{
options[optionsIndex] = options[optionsIndex].Remove(options[optionsIndex].Length - 1);
}
StringBuilder result = new StringBuilder(options[0]);
int currentOptionDisplayedIndex = 0;
ConsoleKeyInfo KeyInfo;
Console.Write(message + options[0]);
KeyInfo = Console.ReadKey(true);
while (KeyInfo.Key != ConsoleKey.Enter)
{
if (Char.IsLetterOrDigit(KeyInfo.KeyChar) || Char.IsSymbol(KeyInfo.KeyChar)
|| (Char.IsPunctuation(KeyInfo.KeyChar) && KeyInfo.KeyChar != '\\')
|| (Char.IsPunctuation(KeyInfo.KeyChar) && KeyInfo.KeyChar != '\\')
|| Char.IsWhiteSpace(KeyInfo.KeyChar))
{
Console.Write(KeyInfo.KeyChar);
result.Append(KeyInfo.KeyChar);
// Performing Closest Search
int foundIndex = -1;
int optionIndex = 0;
foreach (string Option in options)
{
if (Option.ToLower().StartsWith(result.ToString().ToLower()))
{
foundIndex = optionIndex;
// Erase old option and display new
EraseInput(result.Length);
// Display new
result.Clear();
result.Append(options[foundIndex]);
currentOptionDisplayedIndex = foundIndex;
Console.Write(options[currentOptionDisplayedIndex]);
break;
}
optionIndex++;
}
}
else if (result.Length>0 && KeyInfo.Key == ConsoleKey.Backspace)
{
// User hit back space. Erase console and input string by 1 char
EraseInput(1);
result.Remove(result.Length - 1, 1);
}
else if (result.Length>0 && KeyInfo.KeyChar == '\\')
{
options = GetFilesDirs(result + @"\").ToArray();
if (options.Length>0)
{
EraseInput(result.Length);
Console.Write(options[0]);
result.Clear();
result.Append(options[0]);
currentOptionDisplayedIndex = 0;
}
else
{
// Not a directory or no files in directory. No action required
}
}
else if (result.Length>0 && KeyInfo.Key == ConsoleKey.Delete)
{
// Erase everything on console and clear user input
EraseInput(result.Length);
// Clear user input
result.Clear();
}
else if (KeyInfo.Key == ConsoleKey.UpArrow || KeyInfo.Key == ConsoleKey.DownArrow)
{
// Need to display next option
EraseInput(result.Length);
// Display new option
if (KeyInfo.Key == ConsoleKey.UpArrow)
{
if (currentOptionDisplayedIndex > 0)
{
currentOptionDisplayedIndex--;
}
else
{
currentOptionDisplayedIndex = options.Length - 1;
}
}
else if (KeyInfo.Key == ConsoleKey.DownArrow)
{
if (currentOptionDisplayedIndex < options.Length - 1)
{
currentOptionDisplayedIndex++;
}
else
{
currentOptionDisplayedIndex = 0;
}
}
// Show new option
result.Clear();
if (options.Length > 0)
{
Console.Write(options[currentOptionDisplayedIndex]);
// Set new user input
result.Append(options[currentOptionDisplayedIndex]);
}
}
KeyInfo = Console.ReadKey(true);
}
Console.WriteLine();
return result.ToString();
}
/// <summary>
/// Asks the user for input, with a default input value already set
/// </summary>
/// <param name="message">Message that appears on the prompt. For eg, Enter Value:</param>
/// <param name="initialInput">The value prefilled. Eg, "22 Jump Street" prefilled if prompt is to accept address</param>
/// <returns>User entered string or 'initialInput' if no edits were made</returns>
public static string PromptForInput(string message, string initialInput)
{
StringBuilder result = new StringBuilder(initialInput);
ConsoleKeyInfo KeyInfo;
Console.Write(message);
Console.Write(initialInput);
KeyInfo = Console.ReadKey(true);
while (KeyInfo.Key != ConsoleKey.Enter)
{
if (Char.IsLetterOrDigit(KeyInfo.KeyChar) || Char.IsSymbol(KeyInfo.KeyChar)
|| Char.IsPunctuation(KeyInfo.KeyChar) || Char.IsPunctuation(KeyInfo.KeyChar)
|| Char.IsWhiteSpace(KeyInfo.KeyChar))
{
// If its a Letter or Digit, echo to screen, add to Input String
Console.Write(KeyInfo.KeyChar);
result.Append(KeyInfo.KeyChar);
}
else if (result.Length>0 && KeyInfo.Key == ConsoleKey.Backspace)
{
// User hit back space. Erase console and input string by 1 char
EraseInput(1);
result.Remove(result.Length - 1, 1);
}
// Getting a next key from console
KeyInfo = Console.ReadKey(true);
}
Console.WriteLine();
return result.ToString().Trim();
}
/// <summary>
/// Accepts user input by displaying options. The input is editable as well
/// Press:
/// Up Arrow to go to next option
/// Down Arrow to go to previous option
/// Any character to edit the current option
/// Enter to return with the current value
/// </summary>
/// <param name="message">Message that appears on the prompt. For eg, Enter Value:</param>
/// <param name="options">List of values. Eg {Mr, Mrs, Ms} can be passed if prompt if for salutation. User can switch between these values by pressing up/down arrow OR enter something like Dr or Er and hit enter</param>
/// <returns>User entered string or one of the string in 'options' choosen by the user</returns>
public static string PromptForInput(string message, string[] options)
{
return PromptForInput(message, options, true);
}
/// <summary>
/// Accepts user input by displaying options. The input is not editable and the user must choose among the options available
/// Press:
/// Up Arrow to go to next option
/// Down Arrow to go to previous option
/// Enter to return with the current value
/// </summary>
/// <param name="message">Message that appears on the prompt. For eg, Enter Value:</param>
/// <param name="options">List of values. Eg {Mr, Mrs, Ms} can be passed if prompt is for salutation. User can switch between these values by pressing up/down arrow or type another value if edits are allowed</param>
/// <param name="allowEdits">If set to false, keys for characters, backspace, delete will not work. Only Up down can be used to select the options</param>
/// <returns>User entered string or one of the string in 'options' choosen by the user</returns>
public static string PromptForInput(string message, string[] options, bool allowEdits)
{
int CurrentOptionDisplayedIndex = 0;
StringBuilder result = new StringBuilder(options[0]);
ConsoleKeyInfo KeyInfo;
Console.Write(message + options[0]);
KeyInfo = Console.ReadKey(true);
while (KeyInfo.Key != ConsoleKey.Enter)
{
if ((Char.IsLetterOrDigit(KeyInfo.KeyChar) || Char.IsSymbol(KeyInfo.KeyChar)
|| Char.IsPunctuation(KeyInfo.KeyChar) || Char.IsPunctuation(KeyInfo.KeyChar)
|| Char.IsWhiteSpace(KeyInfo.KeyChar))
&& allowEdits)
{
Console.Write(KeyInfo.KeyChar);
result.Append(KeyInfo.KeyChar);
}
else if (result.Length > 0 && KeyInfo.Key == ConsoleKey.Backspace && allowEdits)
{
// User hit back space. Erase console and input string by 1 char
EraseInput(1);
result.Remove(result.Length - 1, 1);
}
else if (KeyInfo.Key == ConsoleKey.UpArrow || KeyInfo.Key == ConsoleKey.DownArrow)
{
// We have to display another option
// Erase on screen and in UserInput string
EraseInput(result.Length);
// Display new option
if (KeyInfo.Key == ConsoleKey.UpArrow)
{
if (CurrentOptionDisplayedIndex > 0)
{
CurrentOptionDisplayedIndex--;
}
else
{
CurrentOptionDisplayedIndex = options.Length - 1;
}
}
else if (KeyInfo.Key == ConsoleKey.DownArrow)
{
if (CurrentOptionDisplayedIndex < options.Length - 1)
{
CurrentOptionDisplayedIndex++;
}
else
{
CurrentOptionDisplayedIndex = 0;
}
}
// Show the new option
Console.Write(options[CurrentOptionDisplayedIndex]);
// Set new user input
result.Clear();
result.Append(options[CurrentOptionDisplayedIndex]);
}
KeyInfo = Console.ReadKey(true);
}
Console.WriteLine();
return result.ToString().Trim();
}
/// <summary>
/// Accepts user input by displaying options. The options are autofilled with matches from key press. This is like auto suggest for a text box
/// Press:
/// Up Arrow to go to next option
/// Down Arrow to go to previous option
/// Any key to list closest matching option. The search criteria is 'begins with'. Eg, When 'C' is pressed and options is a list of US states, then 'California' is set to as choosen option. The search is case sensitive
/// Enter to return with the current value
/// </summary>
/// <param name="message">Message that appears on the prompt. For eg, Enter Value:</param>
/// <param name="options">Input options that the user can choose from using up-down arrow or type to get to</param>
/// <returns>User entered string or one of the string in 'options' choosen by the user</returns>
public static string PromptForInput_Searchable(string message, string[] options)
{
StringBuilder result = new StringBuilder(options[0]);
int CurrentOptionDisplayedIndex = 0;
ConsoleKeyInfo KeyInfo;
Console.Write(message + options[0]);
KeyInfo = Console.ReadKey(true);
while (KeyInfo.Key != ConsoleKey.Enter)
{
if (Char.IsLetterOrDigit(KeyInfo.KeyChar) || Char.IsSymbol(KeyInfo.KeyChar)
|| Char.IsPunctuation(KeyInfo.KeyChar) || Char.IsPunctuation(KeyInfo.KeyChar)
|| Char.IsWhiteSpace(KeyInfo.KeyChar))
{
Console.Write(KeyInfo.KeyChar);
result.Append(KeyInfo.KeyChar);
// Performing Closest Search
int foundIndex = -1;
int OptionIndex = 0;
foreach (string Option in options)
{
if (Option.ToLower().StartsWith(result.ToString().ToLower()))
{
foundIndex = OptionIndex;
// Erase displayed option and show new option
EraseInput(result.Length);
result.Clear();
result.Append(options[foundIndex]);
CurrentOptionDisplayedIndex = foundIndex;
Console.Write(options[CurrentOptionDisplayedIndex]);
break;
}
OptionIndex++;
}
// Closest search ends
}
else if (result.Length>0 && KeyInfo.Key == ConsoleKey.Backspace)
{
// User hit back space. Erase console and input string by 1 char
EraseInput(1);
result.Remove(result.Length - 1, 1);
}
else if (result.Length > 0 && KeyInfo.Key == ConsoleKey.Delete)
{
// Erase everything and set user input to empty
EraseInput(result.Length);
result.Clear();
}
else if (KeyInfo.Key == ConsoleKey.UpArrow || KeyInfo.Key == ConsoleKey.DownArrow)
{
// Remove current option and Display another option
EraseInput(result.Length);
// Display new option
if (KeyInfo.Key == ConsoleKey.UpArrow)
{
if (CurrentOptionDisplayedIndex > 0)
CurrentOptionDisplayedIndex--;
else
CurrentOptionDisplayedIndex = options.Length - 1;
}
else if (KeyInfo.Key == ConsoleKey.DownArrow)
{
if (CurrentOptionDisplayedIndex < options.Length - 1)
CurrentOptionDisplayedIndex++;
else
CurrentOptionDisplayedIndex = 0;
}
// Show new option
Console.Write(options[CurrentOptionDisplayedIndex]);
// Set new user input
result.Clear();
result.Append(options[CurrentOptionDisplayedIndex]);
}
KeyInfo = Console.ReadKey(true);
}
Console.WriteLine();
return result.ToString();
}
/// <summary>
/// Gets a password without displaying it on screen. Input is masked by *s
/// </summary>
/// <param name="message">Message that appears on the prompt. For eg, Enter Value:</param>
/// <returns>The password entered by the user</returns>
public static string GetPassword(string message)
{
StringBuilder result = new StringBuilder();
Console.Write(message);
ConsoleKeyInfo KeyInfo;
KeyInfo = Console.ReadKey(true);
while (KeyInfo.Key != ConsoleKey.Enter)
{
if (Char.IsLetterOrDigit(KeyInfo.KeyChar))
{
// If its a Letter or Digit, echo * to screen, add to Input String
Console.Write("*");
result.Append(KeyInfo.KeyChar);
}
else if (result.Length>0 && KeyInfo.Key == ConsoleKey.Backspace)
{
// User hit back space. Erase console and input string by 1 char
EraseInput(1);
result.Remove(result.Length - 1, 1);
}
KeyInfo = Console.ReadKey(true);
}
Console.WriteLine();
return result.ToString();
}
#endregion
#region Utils
private static void EraseInput(int lengthToErase)
{
for (int i = 0; i<lengthToErase; i++)
{
Console.Write("\b" + " " + "\b");
}
}
private static HashSet<string> GetFilesDirs(string userInput)
{
var result = new HashSet<string>();
if (Directory.Exists(userInput))
{
try
{
var filesAndDirs = Directory.EnumerateFileSystemEntries(userInput);
result.UnionWith(filesAndDirs);
}
catch (Exception)
{
}
}
return result;
}
#endregion
}
}