Script for automating a large assortment of AME related actions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

603 lines
28 KiB

9 months ago
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Security;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8. using JetBrains.Annotations;
  9. namespace Ameliorated.ConsoleUtils
  10. {
  11. /// <summary>
  12. /// Represents a input prompt to be used with the Start method.
  13. /// </summary>
  14. public class ChoicePrompt : Prompt
  15. {
  16. public string Choices { get; set; } = "YN";
  17. public bool BeepSound { get; set; } = true;
  18. public bool CaseSensitive { get; set; } = false;
  19. public bool AllowEscape { get; set; } = true;
  20. public bool AnyKey { get; set; } = false;
  21. public ConsoleColor? TextForeground { get; set; }
  22. private bool _bindToOpenFrame;
  23. [CanBeNull]
  24. public new int? Start()
  25. {
  26. if (Choices.Length < 1 && !AnyKey) throw new ArgumentException("There must be at least 1 choice.");
  27. _bindToOpenFrame = ConsoleTUI.OpenFrame != null && BindToOpenFrame;
  28. if (_bindToOpenFrame && ConsoleTUI.OpenFrame.AvailableLines() < 1)
  29. ConsoleTUI.OpenFrame.Clear();
  30. if (TextForeground.HasValue)
  31. ConsoleUtils.SetColor(TextForeground);
  32. if (_bindToOpenFrame)
  33. ConsoleTUI.OpenFrame.Write(Text);
  34. else
  35. Console.Write(Text);
  36. if (TextForeground.HasValue)
  37. ConsoleUtils.ResetColor();
  38. var cursorVisibility = Console.CursorVisible;
  39. int? result;
  40. while (true)
  41. {
  42. Console.CursorVisible = true;
  43. ConsoleUtils.ClearInputBuffer();
  44. var key = Console.ReadKey(true);
  45. if (AnyKey)
  46. {
  47. Console.CursorVisible = cursorVisibility;
  48. return key.KeyChar;
  49. }
  50. if (key.Key == ConsoleKey.Escape && AllowEscape)
  51. {
  52. Console.CursorVisible = cursorVisibility;
  53. return null;
  54. }
  55. if (CaseSensitive)
  56. result = Choices.IndexOf(key.KeyChar.ToString(), StringComparison.Ordinal);
  57. else
  58. result = Choices.IndexOf(key.KeyChar.ToString(), StringComparison.OrdinalIgnoreCase);
  59. if (result >= 0)
  60. {
  61. if (InputForeground.HasValue && InputBackground.HasValue) ConsoleUtils.SetColor(InputForeground.Value, InputBackground.Value);
  62. else if (InputForeground.HasValue) ConsoleUtils.SetColor(InputForeground.Value);
  63. else if (InputBackground.HasValue) ConsoleUtils.SetColor(Console.ForegroundColor, InputBackground.Value);
  64. if (!CaseSensitive) Console.Write(key.KeyChar.ToString().ToUpper());
  65. else Console.Write(key.KeyChar.ToString());
  66. if (InputForeground.HasValue || InputBackground.HasValue) ConsoleUtils.ResetColor();
  67. break;
  68. }
  69. else if (BeepSound) Console.Beep();
  70. }
  71. Console.CursorVisible = cursorVisibility;
  72. Console.WriteLine();
  73. return result.Value;
  74. }
  75. }
  76. public class InputPrompt : Prompt
  77. {
  78. [Optional] public ConsoleColor? BoxBackground { get; set; }
  79. public bool MaskInput { get; set; } = false;
  80. public bool AlignInput { get; set; } = true;
  81. [Optional] public int? SplitWidth { get; set; }
  82. private bool _bindToOpenFrame;
  83. private int? _splitWidth;
  84. [CanBeNull]
  85. public new string Start()
  86. {
  87. if (SplitWidth.HasValue && !AlignInput) throw new ArgumentException("Property SplitWidth must not be used without property AlignInput set to true.");
  88. _bindToOpenFrame = ConsoleTUI.OpenFrame != null && BindToOpenFrame;
  89. if (_bindToOpenFrame && ConsoleTUI.OpenFrame.AvailableLines() < 1)
  90. ConsoleTUI.OpenFrame.Clear();
  91. if (!MaxLength.HasValue)
  92. MaxLength = AlignInput ? ConsoleTUI.OpenFrame.AvailableChars() - (Text.LastLine().Length * ConsoleTUI.OpenFrame.AvailableLines()) : ConsoleTUI.OpenFrame.AvailableChars();
  93. int startLeft = Console.CursorLeft + Text.LastLine().Length;
  94. _splitWidth = null;
  95. if (AlignInput && !_bindToOpenFrame)
  96. {
  97. if (Text.LastLine().Length > Console.WindowWidth - 3) throw new ArgumentException("Last line of property Text must not be within 3 characters of the available width.");
  98. _splitWidth = Console.WindowWidth - startLeft;
  99. }
  100. if (_bindToOpenFrame)
  101. {
  102. if (SplitWidth.HasValue && (SplitWidth.Value + startLeft + ConsoleTUI.OpenFrame.DisplayOffset >= Console.WindowWidth))
  103. throw new ArgumentException($"Property SplitWidth must be less than the available width.");
  104. // TODO: BAD WORDING
  105. if (Text.LastLine().Length > ConsoleTUI.OpenFrame.DisplayWidth - 3) throw new ArgumentException("Last line of property Text must not be within 3 characters of the available width.");
  106. _splitWidth = ConsoleTUI.OpenFrame.DisplayWidth - (startLeft);
  107. if (!AlignInput)
  108. _splitWidth = ConsoleTUI.OpenFrame.DisplayWidth;
  109. }
  110. if (SplitWidth.HasValue)
  111. {
  112. if (SplitWidth.Value + startLeft >= Console.WindowWidth)
  113. throw new ArgumentException($"Property SplitWidth must be less than the available width.");
  114. if (Text.LastLine().Length > Console.WindowWidth - 3) throw new ArgumentException("Last line of property Text must not be within 3 characters of the available width.");
  115. _splitWidth = SplitWidth.Value;
  116. }
  117. /*
  118. int maxLines = Console.WindowHeight - Console.CursorTop;
  119. if (_bindToOpenFrame)
  120. {
  121. maxLines = ConsoleTUI.OpenFrame.DisplayHeight - (Console.CursorTop - 6) - Text.SplitByLine().Length;
  122. if (maxLines < 2)
  123. {
  124. ConsoleTUI.OpenFrame.Clear();
  125. if (_bindToOpenFrame) maxLines = ConsoleTUI.OpenFrame.DisplayHeight - (Console.CursorTop - 6) - Text.SplitByLine().Length;
  126. }
  127. }
  128. */
  129. ConsoleUtils.ClearInputBuffer();
  130. if (_bindToOpenFrame)
  131. ConsoleTUI.OpenFrame.Write(Text);
  132. else
  133. Console.Write(Text);
  134. if (BoxBackground.HasValue && _splitWidth.HasValue)
  135. {
  136. if (AlignInput)
  137. WriteBackground(_splitWidth.Value, BoxBackground.Value);
  138. else
  139. WriteBackground(_splitWidth.Value - Text.LastLine().Length, BoxBackground.Value);
  140. }
  141. if (BoxBackground.HasValue && !_splitWidth.HasValue) WriteBackground(Console.WindowWidth - Console.CursorLeft, BoxBackground.Value);
  142. var cursorVisibility = Console.CursorVisible;
  143. var input = new StringBuilder();
  144. ConsoleKeyInfo key;
  145. do
  146. {
  147. Console.CursorVisible = true;
  148. key = Console.ReadKey(true);
  149. if (key.Key == ConsoleKey.Backspace)
  150. {
  151. if (input.Length <= 0) continue;
  152. input.Remove(input.Length - 1, 1);
  153. bool movedLines = false;
  154. if (_bindToOpenFrame)
  155. {
  156. if ((((input.Length + 1) - _splitWidth.Value + Text.LastLine().Length).IsDivisibleBy(ConsoleTUI.OpenFrame.DisplayWidth) || input.Length + 1 == _splitWidth.Value - Text.LastLine().Length) && !AlignInput)
  157. {
  158. if (BoxBackground.HasValue) WriteBackground(_splitWidth.Value - (Console.CursorLeft - ConsoleTUI.OpenFrame.DisplayOffset), Console.BackgroundColor);
  159. Console.SetCursorPosition((ConsoleTUI.OpenFrame.DisplayOffset) + _splitWidth.Value - 1, Console.CursorTop - 1);
  160. movedLines = true;
  161. }
  162. else if ((input.Length + 1).IsDivisibleBy(_splitWidth.Value) && AlignInput)
  163. {
  164. if (BoxBackground.HasValue) WriteBackground(_splitWidth.Value, Console.BackgroundColor);
  165. Console.SetCursorPosition(ConsoleTUI.OpenFrame.DisplayOffset + startLeft + _splitWidth.Value - 1, Console.CursorTop - 1);
  166. movedLines = true;
  167. }
  168. }
  169. else if (AlignInput)
  170. {
  171. if ((input.Length + 1).IsDivisibleBy(_splitWidth.Value))
  172. {
  173. if (BoxBackground.HasValue) WriteBackground(_splitWidth.Value, Console.BackgroundColor);
  174. Console.SetCursorPosition(startLeft + _splitWidth.Value, Console.CursorTop - 1);
  175. movedLines = true;
  176. }
  177. }
  178. else
  179. {
  180. if (((input.Length + 1) - (Console.WindowWidth - startLeft)).IsDivisibleBy(Console.WindowWidth) || input.Length + 1 == Console.WindowWidth - startLeft)
  181. {
  182. if (BoxBackground.HasValue) WriteBackground(Console.WindowWidth, Console.BackgroundColor);
  183. Console.SetCursorPosition(Console.WindowWidth - 1, Console.CursorTop - 1);
  184. movedLines = true;
  185. }
  186. }
  187. if (movedLines)
  188. {
  189. if (BoxBackground.HasValue) ConsoleUtils.SetColor(Console.ForegroundColor, BoxBackground.Value);
  190. Console.Write(" \b");
  191. if (BoxBackground.HasValue) ConsoleUtils.ResetColor();
  192. }
  193. else
  194. {
  195. if (BoxBackground.HasValue) ConsoleUtils.SetColor(Console.ForegroundColor, BoxBackground.Value);
  196. Console.Write("\b \b");
  197. if (BoxBackground.HasValue) ConsoleUtils.ResetColor();
  198. }
  199. if (MaxLength.HasValue && MaxLength.Value - 1 == input.Length) Console.CursorVisible = true;
  200. continue;
  201. }
  202. if (Char.IsControl(key.KeyChar)) continue;
  203. if (MaxLength.HasValue && MaxLength.Value <= input.Length)
  204. {
  205. ConsoleUtils.SetColor(ConsoleColor.DarkRed, InputBackground);
  206. Console.CursorVisible = false;
  207. Console.Write("!");
  208. Thread.Sleep(200);
  209. ConsoleUtils.ClearInputBuffer();
  210. ConsoleUtils.ResetColor();
  211. ConsoleUtils.SetColor(null, BoxBackground);
  212. Console.Write("\b \b");
  213. ConsoleUtils.ResetColor();
  214. continue;
  215. }
  216. input.Append(key.KeyChar);
  217. ConsoleUtils.SetColor(InputForeground, InputBackground);
  218. if (MaskInput) Console.Write("*");
  219. else Console.Write(key.KeyChar);
  220. ConsoleUtils.ResetColor();
  221. if (MaxLength.HasValue && MaxLength.Value <= input.Length + 1)
  222. continue;
  223. if (_bindToOpenFrame)
  224. {
  225. if (((input.Length - _splitWidth.Value + Text.LastLine().Length).IsDivisibleBy(_splitWidth.Value) || input.Length == _splitWidth.Value - Text.LastLine().Length) && !AlignInput)
  226. {
  227. Console.SetCursorPosition(ConsoleTUI.OpenFrame.DisplayOffset, Console.CursorTop + 1);
  228. if (BoxBackground.HasValue) WriteBackground(_splitWidth.Value, BoxBackground.Value);
  229. }
  230. else if (input.Length.IsDivisibleBy(_splitWidth.Value) && AlignInput)
  231. {
  232. Console.SetCursorPosition(startLeft + ConsoleTUI.OpenFrame.DisplayOffset, Console.CursorTop + 1);
  233. if (BoxBackground.HasValue) WriteBackground(_splitWidth.Value, BoxBackground.Value);
  234. }
  235. }
  236. else if (AlignInput)
  237. {
  238. if (input.Length.IsDivisibleBy(_splitWidth.Value))
  239. {
  240. if (SplitWidth.HasValue)
  241. Console.SetCursorPosition(startLeft, Console.CursorTop + 1);
  242. else
  243. // Console will have automatically moved the cursor down
  244. Console.SetCursorPosition(startLeft, Console.CursorTop);
  245. if (BoxBackground.HasValue) WriteBackground(_splitWidth.Value, BoxBackground.Value);
  246. }
  247. }
  248. else
  249. {
  250. if ((input.Length - (Console.WindowWidth - startLeft)).IsDivisibleBy(Console.WindowWidth) || input.Length == Console.WindowWidth - startLeft)
  251. {
  252. // Console will have automatically moved the cursor down
  253. Console.SetCursorPosition(startLeft - Text.LastLine().Length, Console.CursorTop);
  254. if (BoxBackground.HasValue) WriteBackground(Console.WindowWidth - (startLeft - Text.LastLine().Length), BoxBackground.Value);
  255. }
  256. }
  257. } while (key.Key != ConsoleKey.Enter && (!AllowEscape || (AllowEscape && key.Key != ConsoleKey.Escape)));
  258. if (input.Length == 0)
  259. {
  260. ConsoleUtils.SetColor(ConsoleColor.DarkGray);
  261. Console.Write("None");
  262. ConsoleUtils.ResetColor();
  263. }
  264. Console.CursorVisible = cursorVisibility;
  265. Console.WriteLine();
  266. if (key.Key == ConsoleKey.Escape && AllowEscape) return null;
  267. return input.ToString();
  268. }
  269. }
  270. public class SecureInputPrompt : Prompt
  271. {
  272. [Optional] public ConsoleColor? BoxBackground { get; set; }
  273. public bool MaskInput { get; set; } = true;
  274. public bool AlignInput { get; set; } = true;
  275. [Optional] public int? SplitWidth { get; set; }
  276. private bool _bindToOpenFrame;
  277. private int? _splitWidth;
  278. [CanBeNull]
  279. public new SecureString Start()
  280. {if (SplitWidth.HasValue && !AlignInput) throw new ArgumentException("Property SplitWidth must not be used without property AlignInput set to true.");
  281. _bindToOpenFrame = ConsoleTUI.OpenFrame != null && BindToOpenFrame;
  282. if (_bindToOpenFrame && ConsoleTUI.OpenFrame.AvailableLines() < 1)
  283. ConsoleTUI.OpenFrame.Clear();
  284. if (!MaxLength.HasValue)
  285. MaxLength = AlignInput ? ConsoleTUI.OpenFrame.AvailableChars() - (Text.LastLine().Length * ConsoleTUI.OpenFrame.AvailableLines()) : ConsoleTUI.OpenFrame.AvailableChars();
  286. int startLeft = Console.CursorLeft + Text.LastLine().Length;
  287. _splitWidth = null;
  288. if (AlignInput && !_bindToOpenFrame)
  289. {
  290. if (Text.LastLine().Length > Console.WindowWidth - 3) throw new ArgumentException("Last line of property Text must not be within 3 characters of the available width.");
  291. _splitWidth = Console.WindowWidth - startLeft;
  292. }
  293. if (_bindToOpenFrame)
  294. {
  295. if (SplitWidth.HasValue && (SplitWidth.Value + startLeft + ConsoleTUI.OpenFrame.DisplayOffset >= Console.WindowWidth))
  296. throw new ArgumentException($"Property SplitWidth must be less than the available width.");
  297. // TODO: BAD WORDING
  298. if (Text.LastLine().Length > ConsoleTUI.OpenFrame.DisplayWidth - 3) throw new ArgumentException("Last line of property Text must not be within 3 characters of the available width.");
  299. _splitWidth = ConsoleTUI.OpenFrame.DisplayWidth - (startLeft);
  300. if (!AlignInput)
  301. _splitWidth = ConsoleTUI.OpenFrame.DisplayWidth;
  302. }
  303. if (SplitWidth.HasValue)
  304. {
  305. if (SplitWidth.Value + startLeft >= Console.WindowWidth)
  306. throw new ArgumentException($"Property SplitWidth must be less than the available width.");
  307. if (Text.LastLine().Length > Console.WindowWidth - 3) throw new ArgumentException("Last line of property Text must not be within 3 characters of the available width.");
  308. _splitWidth = SplitWidth.Value;
  309. }
  310. /*
  311. int maxLines = Console.WindowHeight - Console.CursorTop;
  312. if (_bindToOpenFrame)
  313. {
  314. maxLines = ConsoleTUI.OpenFrame.DisplayHeight - (Console.CursorTop - 6) - Text.SplitByLine().Length;
  315. if (maxLines < 2)
  316. {
  317. ConsoleTUI.OpenFrame.Clear();
  318. if (_bindToOpenFrame) maxLines = ConsoleTUI.OpenFrame.DisplayHeight - (Console.CursorTop - 6) - Text.SplitByLine().Length;
  319. }
  320. }
  321. */
  322. ConsoleUtils.ClearInputBuffer();
  323. if (_bindToOpenFrame)
  324. ConsoleTUI.OpenFrame.Write(Text);
  325. else
  326. Console.Write(Text);
  327. if (BoxBackground.HasValue && _splitWidth.HasValue)
  328. {
  329. if (AlignInput)
  330. WriteBackground(_splitWidth.Value, BoxBackground.Value);
  331. else
  332. WriteBackground(_splitWidth.Value - Text.LastLine().Length, BoxBackground.Value);
  333. }
  334. if (BoxBackground.HasValue && !_splitWidth.HasValue) WriteBackground(Console.WindowWidth - Console.CursorLeft, BoxBackground.Value);
  335. var cursorVisibility = Console.CursorVisible;
  336. var input = new SecureString();
  337. ConsoleKeyInfo key;
  338. do
  339. {
  340. Console.CursorVisible = true;
  341. key = Console.ReadKey(true);
  342. if (key.Key == ConsoleKey.Backspace)
  343. {
  344. if (input.Length <= 0) continue;
  345. input.RemoveAt(input.Length - 1);
  346. bool movedLines = false;
  347. if (_bindToOpenFrame)
  348. {
  349. if ((((input.Length + 1) - _splitWidth.Value + Text.LastLine().Length).IsDivisibleBy(ConsoleTUI.OpenFrame.DisplayWidth) || input.Length + 1 == _splitWidth.Value - Text.LastLine().Length) && !AlignInput)
  350. {
  351. if (BoxBackground.HasValue) WriteBackground(_splitWidth.Value - (Console.CursorLeft - ConsoleTUI.OpenFrame.DisplayOffset), Console.BackgroundColor);
  352. Console.SetCursorPosition((ConsoleTUI.OpenFrame.DisplayOffset) + _splitWidth.Value - 1, Console.CursorTop - 1);
  353. movedLines = true;
  354. }
  355. else if ((input.Length + 1).IsDivisibleBy(_splitWidth.Value) && AlignInput)
  356. {
  357. if (BoxBackground.HasValue) WriteBackground(_splitWidth.Value, Console.BackgroundColor);
  358. Console.SetCursorPosition(ConsoleTUI.OpenFrame.DisplayOffset + startLeft + _splitWidth.Value - 1, Console.CursorTop - 1);
  359. movedLines = true;
  360. }
  361. }
  362. else if (AlignInput)
  363. {
  364. if ((input.Length + 1).IsDivisibleBy(_splitWidth.Value))
  365. {
  366. if (BoxBackground.HasValue) WriteBackground(_splitWidth.Value, Console.BackgroundColor);
  367. Console.SetCursorPosition(startLeft + _splitWidth.Value, Console.CursorTop - 1);
  368. movedLines = true;
  369. }
  370. }
  371. else
  372. {
  373. if (((input.Length + 1) - (Console.WindowWidth - startLeft)).IsDivisibleBy(Console.WindowWidth) || input.Length + 1 == Console.WindowWidth - startLeft)
  374. {
  375. if (BoxBackground.HasValue) WriteBackground(Console.WindowWidth, Console.BackgroundColor);
  376. Console.SetCursorPosition(Console.WindowWidth - 1, Console.CursorTop - 1);
  377. movedLines = true;
  378. }
  379. }
  380. if (movedLines)
  381. {
  382. if (BoxBackground.HasValue) ConsoleUtils.SetColor(Console.ForegroundColor, BoxBackground.Value);
  383. Console.Write(" \b");
  384. if (BoxBackground.HasValue) ConsoleUtils.ResetColor();
  385. }
  386. else
  387. {
  388. if (BoxBackground.HasValue) ConsoleUtils.SetColor(Console.ForegroundColor, BoxBackground.Value);
  389. Console.Write("\b \b");
  390. if (BoxBackground.HasValue) ConsoleUtils.ResetColor();
  391. }
  392. if (MaxLength.HasValue && MaxLength.Value - 1 == input.Length) Console.CursorVisible = true;
  393. }
  394. if (Char.IsControl(key.KeyChar)) continue;
  395. if (MaxLength.HasValue && MaxLength.Value <= input.Length)
  396. {
  397. ConsoleUtils.SetColor(ConsoleColor.DarkRed, InputBackground);
  398. Console.CursorVisible = false;
  399. Console.Write("!");
  400. Thread.Sleep(200);
  401. ConsoleUtils.ClearInputBuffer();
  402. ConsoleUtils.ResetColor();
  403. ConsoleUtils.SetColor(null, BoxBackground);
  404. Console.Write("\b \b");
  405. ConsoleUtils.ResetColor();
  406. continue;
  407. }
  408. input.AppendChar(key.KeyChar);
  409. ConsoleUtils.SetColor(InputForeground, InputBackground);
  410. if (MaskInput) Console.Write("*");
  411. else Console.Write(key.KeyChar);
  412. ConsoleUtils.ResetColor();
  413. if (MaxLength.HasValue && MaxLength.Value <= input.Length + 1)
  414. continue;
  415. if (_bindToOpenFrame)
  416. {
  417. if (((input.Length - _splitWidth.Value + Text.LastLine().Length).IsDivisibleBy(_splitWidth.Value) || input.Length == _splitWidth.Value - Text.LastLine().Length) && !AlignInput)
  418. {
  419. Console.SetCursorPosition(ConsoleTUI.OpenFrame.DisplayOffset, Console.CursorTop + 1);
  420. if (BoxBackground.HasValue) WriteBackground(_splitWidth.Value, BoxBackground.Value);
  421. }
  422. else if (input.Length.IsDivisibleBy(_splitWidth.Value) && AlignInput)
  423. {
  424. Console.SetCursorPosition(startLeft + ConsoleTUI.OpenFrame.DisplayOffset, Console.CursorTop + 1);
  425. if (BoxBackground.HasValue) WriteBackground(_splitWidth.Value, BoxBackground.Value);
  426. }
  427. }
  428. else if (AlignInput)
  429. {
  430. if (input.Length.IsDivisibleBy(_splitWidth.Value))
  431. {
  432. if (SplitWidth.HasValue)
  433. Console.SetCursorPosition(startLeft, Console.CursorTop + 1);
  434. else
  435. // Console will have automatically moved the cursor down
  436. Console.SetCursorPosition(startLeft, Console.CursorTop);
  437. if (BoxBackground.HasValue) WriteBackground(_splitWidth.Value, BoxBackground.Value);
  438. }
  439. }
  440. else
  441. {
  442. if ((input.Length - (Console.WindowWidth - startLeft)).IsDivisibleBy(Console.WindowWidth) || input.Length == Console.WindowWidth - startLeft)
  443. {
  444. // Console will have automatically moved the cursor down
  445. Console.SetCursorPosition(startLeft - Text.LastLine().Length, Console.CursorTop);
  446. if (BoxBackground.HasValue) WriteBackground(Console.WindowWidth - (startLeft - Text.LastLine().Length), BoxBackground.Value);
  447. }
  448. }
  449. } while (key.Key != ConsoleKey.Enter && (!AllowEscape || (AllowEscape && key.Key != ConsoleKey.Escape)));
  450. if (input.Length == 0)
  451. {
  452. ConsoleUtils.SetColor(ConsoleColor.Gray);
  453. Console.Write("None");
  454. ConsoleUtils.ResetColor();
  455. }
  456. Console.CursorVisible = cursorVisibility;
  457. Console.WriteLine();
  458. if (key.Key == ConsoleKey.Escape && AllowEscape) return null;
  459. return input;
  460. }
  461. }
  462. public abstract class Prompt
  463. {
  464. /// <summary>
  465. /// Text to be displayed before the input.
  466. /// </summary>
  467. [Optional]
  468. public string Text { get; set; } = "";
  469. [Optional] public int? MaxLength { get; set; }
  470. /// <summary>
  471. /// (Optional)
  472. /// </summary>
  473. [Optional]
  474. public ConsoleColor? InputForeground { get; set; } = null;
  475. [Optional] public ConsoleColor? InputBackground { get; set; } = null;
  476. public bool BindToOpenFrame { get; set; } = true;
  477. public bool AllowEscape { get; set; } = true;
  478. internal static void WriteBackground(int length, ConsoleColor color)
  479. {
  480. if (Console.CursorLeft + length > Console.WindowWidth) throw new Exception("Critical Error");
  481. int leftStart = Console.CursorLeft;
  482. int topStart = Console.CursorTop;
  483. ConsoleUtils.SetColor(Console.ForegroundColor, color);
  484. Console.Write(new string(' ', length));
  485. ConsoleUtils.ResetColor();
  486. Console.SetCursorPosition(leftStart, topStart);
  487. }
  488. }
  489. }