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.

249 lines
7.6 KiB

9 months ago
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using JetBrains.Annotations;
  5. namespace Ameliorated.ConsoleUtils
  6. {
  7. [AttributeUsage(AttributeTargets.Property)]
  8. internal sealed class OptionalAttribute : Attribute
  9. {
  10. }
  11. public static class ConsoleUtils
  12. {
  13. public class LoadingIndicator : IDisposable
  14. {
  15. public LoadingIndicator() {}
  16. public LoadingIndicator(bool start)
  17. {
  18. if (start)
  19. StartAsync();
  20. }
  21. private CancellationTokenSource cts = new CancellationTokenSource();
  22. Task currentTask = Task.CompletedTask;
  23. public async Task StartAsync()
  24. {
  25. Console.CursorVisible = false;
  26. cts = new CancellationTokenSource();
  27. currentTask = Task.Run(() =>
  28. {
  29. if (cts.IsCancellationRequested)
  30. {
  31. Console.WriteLine("...");
  32. return;
  33. }
  34. Console.Write('.');
  35. while (true)
  36. {
  37. Thread.Sleep(500);
  38. Console.Write('.');
  39. Thread.Sleep(500);
  40. Console.Write('.');
  41. Thread.Sleep(500);
  42. if (cts.IsCancellationRequested)
  43. {
  44. Console.WriteLine();
  45. return;
  46. }
  47. Console.Write("\b \b\b \b");
  48. }
  49. });
  50. await currentTask;
  51. }
  52. public void Stop()
  53. {
  54. cts.Cancel();
  55. currentTask.Wait();
  56. }
  57. public void Dispose()
  58. {
  59. Stop();
  60. this.currentTask = null;
  61. this.cts = null;
  62. }
  63. }
  64. public static bool DisplayErrors { get; set; } = true;
  65. public static void ClearInputBuffer()
  66. {
  67. while (Console.KeyAvailable) Console.ReadKey(true);
  68. }
  69. internal static void WriteError(string text)
  70. {
  71. if (DisplayErrors) Console.WriteLine("ConsoleUtils: " + text);
  72. }
  73. private static ConsoleColor foregroundCache;
  74. private static ConsoleColor backgroundCache;
  75. private static bool? foregroundOnly;
  76. internal static void SetColor([CanBeNull] ConsoleColor? foreground)
  77. {
  78. foregroundCache = Console.ForegroundColor;
  79. if (foreground.HasValue)
  80. Console.ForegroundColor = foreground.Value;
  81. foregroundOnly = true;
  82. }
  83. internal static void SetColor([CanBeNull] ConsoleColor? foreground, [CanBeNull] ConsoleColor? background)
  84. {
  85. foregroundCache = Console.ForegroundColor;
  86. backgroundCache = Console.BackgroundColor;
  87. if (foreground.HasValue)
  88. Console.ForegroundColor = foreground.Value;
  89. if (background.HasValue)
  90. Console.BackgroundColor = background.Value;
  91. foregroundOnly = false;
  92. }
  93. internal static void ResetColor()
  94. {
  95. if (!foregroundOnly.HasValue) throw new MethodAccessException("SetColor must be used before calling ResetColor.");
  96. if (foregroundOnly.Value)
  97. {
  98. Console.ForegroundColor = foregroundCache;
  99. } else
  100. {
  101. Console.ForegroundColor = foregroundCache;
  102. Console.BackgroundColor = backgroundCache;
  103. }
  104. foregroundOnly = null;
  105. }
  106. public static void WriteLine(string text, ConsoleColor? foreground)
  107. {
  108. foreach (var line in text.SplitByLine())
  109. {
  110. ConsoleUtils.SetColor(foreground);
  111. Console.WriteLine(line);
  112. ConsoleUtils.ResetColor();
  113. }
  114. }
  115. public static void WriteLine(string text, ConsoleColor? foreground, ConsoleColor? background)
  116. {
  117. foreach (var line in text.SplitByLine())
  118. {
  119. ConsoleUtils.SetColor(foreground);
  120. Console.WriteLine(line);
  121. ConsoleUtils.ResetColor();
  122. }
  123. }
  124. public static void WriteLine(string text, int offset)
  125. {
  126. foreach (var line in text.SplitByLine())
  127. {
  128. Console.WriteLine(line.Insert(0, new string(' ', offset)));
  129. }
  130. }
  131. public static void WriteLine(string text, int offset, ConsoleColor? foreground)
  132. {
  133. foreach (var line in text.SplitByLine())
  134. {
  135. Console.Write(new string(' ', offset));
  136. ConsoleUtils.SetColor(foreground);
  137. Console.WriteLine(line);
  138. ConsoleUtils.ResetColor();
  139. }
  140. }
  141. public static void WriteLine(string text, int offset, ConsoleColor? foreground, ConsoleColor? background)
  142. {
  143. foreach (var line in text.SplitByLine())
  144. {
  145. Console.Write(new string(' ', offset));
  146. ConsoleUtils.SetColor(foreground, background);
  147. Console.WriteLine(line);
  148. ConsoleUtils.ResetColor();
  149. }
  150. }
  151. public static void Write(string text, ConsoleColor? foreground)
  152. {
  153. foreach (var line in text.SplitByLine())
  154. {
  155. ConsoleUtils.SetColor(foreground);
  156. Console.Write(line);
  157. ConsoleUtils.ResetColor();
  158. }
  159. }
  160. public static void Write(string text, ConsoleColor? foreground, ConsoleColor? background)
  161. {
  162. foreach (var line in text.SplitByLine())
  163. {
  164. ConsoleUtils.SetColor(foreground);
  165. Console.Write(line);
  166. ConsoleUtils.ResetColor();
  167. }
  168. }
  169. public static void Write(string text, int offset)
  170. {
  171. foreach (var line in text.SplitByLine())
  172. {
  173. Console.Write(line.Insert(0, new string(' ', offset)));
  174. }
  175. }
  176. public static void Write(string text, int offset, ConsoleColor? foreground)
  177. {
  178. foreach (var line in text.SplitByLine())
  179. {
  180. Console.Write(new string(' ', offset));
  181. ConsoleUtils.SetColor(foreground);
  182. Console.Write(line);
  183. ConsoleUtils.ResetColor();
  184. }
  185. }
  186. public static void Write(string text, int offset, ConsoleColor? foreground, ConsoleColor? background)
  187. {
  188. foreach (var line in text.SplitByLine())
  189. {
  190. Console.Write(new string(' ', offset));
  191. ConsoleUtils.SetColor(foreground, background);
  192. Console.Write(line);
  193. ConsoleUtils.ResetColor();
  194. }
  195. }
  196. }
  197. }