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.

396 lines
16 KiB

9 months ago
9 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.Contracts;
  4. using System.Linq;
  5. namespace Ameliorated.ConsoleUtils
  6. {
  7. public partial class ConsoleTUI
  8. {
  9. public partial class Frame
  10. {
  11. /// <summary>
  12. /// Centered Line
  13. /// </summary>
  14. public enum LineCenterOptions
  15. {
  16. Word = 0,
  17. Character = 1
  18. }
  19. /// <summary>
  20. /// Normal (Indented) Line
  21. /// </summary>
  22. public void WriteLine()
  23. {
  24. Console.WriteLine();
  25. }
  26. public void Write(string text)
  27. {
  28. var lines = text.SplitByLine();
  29. if (lines.Length > AvailableLines())
  30. Clear();
  31. foreach (var line in lines.Take(lines.Length - 1))
  32. {
  33. Console.WriteLine(line.Insert(0, new string(' ', DisplayOffset)));
  34. }
  35. Console.Write(lines.Last().Insert(0, new string(' ', DisplayOffset)));
  36. }
  37. public void WriteLine(string text)
  38. {
  39. var lines = text.SplitByLine();
  40. if (lines.Length > AvailableLines())
  41. Clear();
  42. foreach (var line in lines)
  43. {
  44. Console.WriteLine(line.Insert(0, new string(' ', DisplayOffset)));
  45. }
  46. }
  47. public void WriteLine(string text, ConsoleColor? foreground)
  48. {
  49. var lines = text.SplitByLine();
  50. if (lines.Length > AvailableLines())
  51. Clear();
  52. foreach (var line in lines)
  53. {
  54. Console.Write(new string(' ', DisplayOffset));
  55. ConsoleUtils.SetColor(foreground);
  56. Console.WriteLine(line);
  57. ConsoleUtils.ResetColor();
  58. }
  59. }
  60. public void WriteLine(string text, ConsoleColor? foreground, ConsoleColor? background)
  61. {
  62. var lines = text.SplitByLine();
  63. if (lines.Length > AvailableLines())
  64. Clear();
  65. foreach (var line in lines)
  66. {
  67. Console.Write(new string(' ', DisplayOffset));
  68. ConsoleUtils.SetColor(foreground, background);
  69. Console.WriteLine(text);
  70. ConsoleUtils.ResetColor();
  71. }
  72. }
  73. public void WriteLine(string text, int offset)
  74. {
  75. var lines = text.SplitByLine();
  76. if (lines.Length > AvailableLines())
  77. Clear();
  78. foreach (var line in lines)
  79. {
  80. Console.WriteLine(line.Insert(0, new string(' ', DisplayOffset + offset)));
  81. }
  82. }
  83. public void WriteLine(string text, int offset, ConsoleColor? foreground)
  84. {
  85. var lines = text.SplitByLine();
  86. if (lines.Length > AvailableLines())
  87. Clear();
  88. foreach (var line in lines)
  89. {
  90. Console.Write(new string(' ', DisplayOffset + offset));
  91. ConsoleUtils.SetColor(foreground);
  92. Console.WriteLine(line);
  93. ConsoleUtils.ResetColor();
  94. }
  95. }
  96. public void WriteLine(string text, int offset, ConsoleColor? foreground, ConsoleColor? background)
  97. {
  98. var lines = text.SplitByLine();
  99. if (lines.Length > AvailableLines())
  100. Clear();
  101. foreach (var line in lines)
  102. {
  103. Console.Write(new string(' ', DisplayOffset + offset));
  104. ConsoleUtils.SetColor(foreground, background);
  105. Console.WriteLine(line);
  106. ConsoleUtils.ResetColor();
  107. }
  108. }
  109. public void WriteCenteredLine(string text, bool ignoreDisplayHeight = false, LineCenterOptions options = LineCenterOptions.Word)
  110. {
  111. var centeredLines = CenterLines(text, options);
  112. if (centeredLines.Count > AvailableLines() && !ignoreDisplayHeight)
  113. {
  114. Clear();
  115. centeredLines = CenterLines(text.TrimStart('\r').TrimStart('\n'), options);
  116. }
  117. foreach (var line in centeredLines) Console.WriteLine(line.Value);
  118. }
  119. public void WriteCenteredLine(string text, int maxWidth, bool ignoreDisplayHeight = false, LineCenterOptions options = LineCenterOptions.Word)
  120. {
  121. Contract.Requires<ArgumentException>(maxWidth > 1);
  122. var centeredLines = CenterLines(text, options, maxWidth);
  123. if (centeredLines.Count >AvailableLines() && !ignoreDisplayHeight)
  124. Clear();
  125. foreach (var line in centeredLines) Console.WriteLine(line.Value);
  126. }
  127. public void WriteCenteredLine(string text, ConsoleColor? foreground, bool ignoreDisplayHeight = false, LineCenterOptions options = LineCenterOptions.Word)
  128. {
  129. var centeredLines = CenterLines(text, options);
  130. if (centeredLines.Count >AvailableLines() && !ignoreDisplayHeight)
  131. Clear();
  132. foreach (var line in centeredLines)
  133. {
  134. Console.Write(new string(' ', line.LeaderLength));
  135. ConsoleUtils.SetColor(foreground);
  136. Console.WriteLine(line.RawString);
  137. ConsoleUtils.ResetColor();
  138. }
  139. }
  140. public void WriteCenteredLine(string text, int maxWidth, ConsoleColor? foreground, bool ignoreDisplayHeight = false, LineCenterOptions options = LineCenterOptions.Word)
  141. {
  142. Contract.Requires<ArgumentException>(maxWidth > 1);
  143. var centeredLines = CenterLines(text, options, maxWidth);
  144. if (centeredLines.Count >AvailableLines() && !ignoreDisplayHeight)
  145. Clear();
  146. foreach (var line in centeredLines)
  147. {
  148. Console.Write(new string(' ', line.LeaderLength));
  149. ConsoleUtils.SetColor(foreground);
  150. Console.WriteLine(line.RawString);
  151. ConsoleUtils.ResetColor();
  152. }
  153. }
  154. public void WriteCenteredLine(string text, ConsoleColor? foreground, ConsoleColor? background, bool ignoreDisplayHeight = false, LineCenterOptions options = LineCenterOptions.Word)
  155. {
  156. var centeredLines = CenterLines(text, options);
  157. if (centeredLines.Count > AvailableLines() && !ignoreDisplayHeight)
  158. {
  159. Clear();
  160. centeredLines = CenterLines(text.TrimStart('\r').TrimStart('\n'), options);
  161. }
  162. foreach (var line in centeredLines)
  163. {
  164. Console.Write(new string(' ', line.LeaderLength));
  165. ConsoleUtils.SetColor(foreground, background);
  166. Console.WriteLine(line.RawString);
  167. ConsoleUtils.ResetColor();
  168. }
  169. }
  170. public void WriteCenteredLine(string text, int maxWidth, ConsoleColor? foreground, ConsoleColor? background, bool ignoreDisplayHeight = false, LineCenterOptions options = LineCenterOptions.Word)
  171. {
  172. Contract.Requires<ArgumentException>(maxWidth > 1);
  173. var centeredLines = CenterLines(text, options, maxWidth);
  174. if (centeredLines.Count >AvailableLines() && !ignoreDisplayHeight)
  175. Clear();
  176. foreach (var line in centeredLines)
  177. {
  178. Console.Write(new string(' ', line.LeaderLength));
  179. ConsoleUtils.SetColor(foreground, background);
  180. Console.WriteLine(line.RawString);
  181. ConsoleUtils.ResetColor();
  182. }
  183. }
  184. public void WriteCentered(string text, bool ignoreDisplayHeight = false, LineCenterOptions options = LineCenterOptions.Word)
  185. {
  186. var centeredLines = CenterLines(text, options);
  187. if (centeredLines.Count > AvailableLines() && !ignoreDisplayHeight)
  188. {
  189. Clear();
  190. centeredLines = CenterLines(text.TrimStart('\r').TrimStart('\n'), options);
  191. }
  192. foreach (var line in centeredLines.Take(centeredLines.Count - 1)) Console.WriteLine(line.Value);
  193. Console.Write(centeredLines.Last().Value);
  194. }
  195. public void WriteCentered(string text, int maxWidth, bool ignoreDisplayHeight = false, LineCenterOptions options = LineCenterOptions.Word)
  196. {
  197. Contract.Requires<ArgumentException>(maxWidth > 1);
  198. var centeredLines = CenterLines(text, options, maxWidth);
  199. if (centeredLines.Count >AvailableLines() && !ignoreDisplayHeight)
  200. Clear();
  201. foreach (var line in centeredLines.Take(centeredLines.Count - 1)) Console.WriteLine(line.Value);
  202. Console.Write(centeredLines.Last().Value);
  203. }
  204. public void WriteCentered(string text, ConsoleColor? foreground, bool ignoreDisplayHeight = false, LineCenterOptions options = LineCenterOptions.Word)
  205. {
  206. var centeredLines = CenterLines(text, options);
  207. if (centeredLines.Count >AvailableLines() && !ignoreDisplayHeight)
  208. Clear();
  209. foreach (var line in centeredLines.Take(centeredLines.Count - 1))
  210. {
  211. Console.Write(new string(' ', line.LeaderLength));
  212. ConsoleUtils.SetColor(foreground);
  213. Console.WriteLine(line.RawString);
  214. ConsoleUtils.ResetColor();
  215. }
  216. Console.Write(new string(' ', centeredLines.Last().LeaderLength));
  217. ConsoleUtils.SetColor(foreground);
  218. Console.WriteLine(centeredLines.Last().RawString);
  219. ConsoleUtils.ResetColor();
  220. }
  221. public void WriteCentered(string text, int maxWidth, ConsoleColor? foreground, bool ignoreDisplayHeight = false, LineCenterOptions options = LineCenterOptions.Word)
  222. {
  223. Contract.Requires<ArgumentException>(maxWidth > 1);
  224. var centeredLines = CenterLines(text, options, maxWidth);
  225. if (centeredLines.Count >AvailableLines() && !ignoreDisplayHeight)
  226. Clear();
  227. foreach (var line in centeredLines.Take(centeredLines.Count - 1))
  228. {
  229. Console.Write(new string(' ', line.LeaderLength));
  230. ConsoleUtils.SetColor(foreground);
  231. Console.WriteLine(line.RawString);
  232. ConsoleUtils.ResetColor();
  233. }
  234. Console.Write(new string(' ', centeredLines.Last().LeaderLength));
  235. ConsoleUtils.SetColor(foreground);
  236. Console.WriteLine(centeredLines.Last().RawString);
  237. ConsoleUtils.ResetColor();
  238. }
  239. public void WriteCentered(string text, ConsoleColor? foreground, ConsoleColor? background, bool ignoreDisplayHeight = false, LineCenterOptions options = LineCenterOptions.Word)
  240. {
  241. var centeredLines = CenterLines(text, options);
  242. if (centeredLines.Count >AvailableLines() && !ignoreDisplayHeight)
  243. Clear();
  244. foreach (var line in centeredLines.Take(centeredLines.Count - 1))
  245. {
  246. Console.Write(new string(' ', line.LeaderLength));
  247. ConsoleUtils.SetColor(foreground, background);
  248. Console.WriteLine(line.RawString);
  249. ConsoleUtils.ResetColor();
  250. }
  251. Console.Write(new string(' ', centeredLines.Last().LeaderLength));
  252. ConsoleUtils.SetColor(foreground, background);
  253. Console.WriteLine(centeredLines.Last().RawString);
  254. ConsoleUtils.ResetColor();
  255. }
  256. public void WriteCentered(string text, int maxWidth, ConsoleColor? foreground, ConsoleColor? background, bool ignoreDisplayHeight = false, LineCenterOptions options = LineCenterOptions.Word)
  257. {
  258. Contract.Requires<ArgumentException>(maxWidth > 1);
  259. var centeredLines = CenterLines(text, options, maxWidth);
  260. if (centeredLines.Count >AvailableLines() && !ignoreDisplayHeight)
  261. Clear();
  262. foreach (var line in centeredLines.Take(centeredLines.Count - 1))
  263. {
  264. Console.Write(new string(' ', line.LeaderLength));
  265. ConsoleUtils.SetColor(foreground, background);
  266. Console.WriteLine(line.RawString);
  267. ConsoleUtils.ResetColor();
  268. }
  269. Console.Write(new string(' ', centeredLines.Last().LeaderLength));
  270. ConsoleUtils.SetColor(foreground, background);
  271. Console.WriteLine(centeredLines.Last().RawString);
  272. ConsoleUtils.ResetColor();
  273. }
  274. // TODO: Fix this splitting lines when a piece of text can fit on one line
  275. private List<CenteredString> CenterLines(string text, LineCenterOptions options, int maxWidth = 0)
  276. {
  277. var _maxWidth = DisplayWidth;
  278. if (maxWidth != 0) _maxWidth = maxWidth;
  279. var list = new List<CenteredString>();
  280. var lines = new List<string>();
  281. foreach (var line in text.SplitByLine())
  282. {
  283. if (line == "")
  284. {
  285. list.Add(new CenteredString
  286. { Value = "", LeaderLength = 0 });
  287. continue;
  288. }
  289. if (line.Length > _maxWidth)
  290. {
  291. for (var index = 0; index < line.Length;)
  292. {
  293. var splitLine = line.Substring(index, Math.Min(_maxWidth, line.Length - index));
  294. var trimmedLength = splitLine.Length - splitLine.Trim(' ').Length;
  295. splitLine = splitLine.Trim(' ');
  296. var wordIndex = splitLine.LastIndexOf(' ');
  297. if (wordIndex != -1 && options == LineCenterOptions.Word) splitLine = splitLine.Substring(0, wordIndex);
  298. index += splitLine.Length + trimmedLength;
  299. list.Add(CenterLine(splitLine));
  300. }
  301. continue;
  302. }
  303. list.Add(CenterLine(line));
  304. }
  305. return list;
  306. }
  307. private CenteredString CenterLine(string text, int maxWidth = 0)
  308. {
  309. var _maxWidth = DisplayWidth;
  310. if (maxWidth != 0) _maxWidth = maxWidth;
  311. var centeredString = new CenteredString();
  312. var space = "";
  313. if (!(text.Length % 2).Equals(0) && text.Length != _maxWidth) space = " ";
  314. var leadingLength = (_maxWidth - text.Length) / 2;
  315. centeredString.Value = space + text.PadLeft(text.Length + leadingLength, ' ').Insert(0, new string(' ', DisplayOffset));
  316. centeredString.LeaderLength = leadingLength + DisplayOffset + space.Length;
  317. centeredString.RawString = text;
  318. return centeredString;
  319. }
  320. private class CenteredString
  321. {
  322. public int LeaderLength;
  323. public string RawString;
  324. public string Value;
  325. }
  326. }
  327. }
  328. }