FakeDriver.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. //
  2. // FakeDriver.cs: A fake ConsoleDriver for unit tests.
  3. //
  4. // Authors:
  5. // Charlie Kindel (github.com/tig)
  6. //
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Threading;
  11. using NStack;
  12. namespace Terminal.Gui {
  13. /// <summary>
  14. /// Implements a mock ConsoleDriver for unit testing
  15. /// </summary>
  16. public class FakeDriver : ConsoleDriver {
  17. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  18. int cols, rows;
  19. public override int Cols => cols;
  20. public override int Rows => rows;
  21. public override int Top => 0;
  22. public override bool HeightAsBuffer { get; set; }
  23. // The format is rows, columns and 3 values on the last column: Rune, Attribute and Dirty Flag
  24. int [,,] contents;
  25. bool [] dirtyLine;
  26. /// <summary>
  27. /// Assists with testing, the format is rows, columns and 3 values on the last column: Rune, Attribute and Dirty Flag
  28. /// </summary>
  29. public int [,,] Contents => contents;
  30. void UpdateOffscreen ()
  31. {
  32. int cols = Cols;
  33. int rows = Rows;
  34. contents = new int [rows, cols, 3];
  35. for (int r = 0; r < rows; r++) {
  36. for (int c = 0; c < cols; c++) {
  37. contents [r, c, 0] = ' ';
  38. contents [r, c, 1] = MakeColor (ConsoleColor.Gray, ConsoleColor.Black);
  39. contents [r, c, 2] = 0;
  40. }
  41. }
  42. dirtyLine = new bool [rows];
  43. for (int row = 0; row < rows; row++)
  44. dirtyLine [row] = true;
  45. }
  46. static bool sync = false;
  47. public FakeDriver ()
  48. {
  49. cols = FakeConsole.WindowWidth;
  50. rows = FakeConsole.WindowHeight; // - 1;
  51. UpdateOffscreen ();
  52. }
  53. bool needMove;
  54. // Current row, and current col, tracked by Move/AddCh only
  55. int ccol, crow;
  56. public override void Move (int col, int row)
  57. {
  58. ccol = col;
  59. crow = row;
  60. if (Clip.Contains (col, row)) {
  61. FakeConsole.CursorTop = row;
  62. FakeConsole.CursorLeft = col;
  63. needMove = false;
  64. } else {
  65. FakeConsole.CursorTop = Clip.Y;
  66. FakeConsole.CursorLeft = Clip.X;
  67. needMove = true;
  68. }
  69. }
  70. public override void AddRune (Rune rune)
  71. {
  72. rune = MakePrintable (rune);
  73. if (Clip.Contains (ccol, crow)) {
  74. if (needMove) {
  75. //MockConsole.CursorLeft = ccol;
  76. //MockConsole.CursorTop = crow;
  77. needMove = false;
  78. }
  79. contents [crow, ccol, 0] = (int)(uint)rune;
  80. contents [crow, ccol, 1] = currentAttribute;
  81. contents [crow, ccol, 2] = 1;
  82. dirtyLine [crow] = true;
  83. } else
  84. needMove = true;
  85. ccol++;
  86. //if (ccol == Cols) {
  87. // ccol = 0;
  88. // if (crow + 1 < Rows)
  89. // crow++;
  90. //}
  91. if (sync)
  92. UpdateScreen ();
  93. }
  94. public override void AddStr (ustring str)
  95. {
  96. foreach (var rune in str)
  97. AddRune (rune);
  98. }
  99. public override void End ()
  100. {
  101. FakeConsole.ResetColor ();
  102. FakeConsole.Clear ();
  103. }
  104. static Attribute MakeColor (ConsoleColor f, ConsoleColor b)
  105. {
  106. // Encode the colors into the int value.
  107. return new Attribute (
  108. value: ((((int)f) & 0xffff) << 16) | (((int)b) & 0xffff),
  109. foreground: (Color)f,
  110. background: (Color)b
  111. );
  112. }
  113. public override void Init (Action terminalResized)
  114. {
  115. Colors.TopLevel = new ColorScheme ();
  116. Colors.Base = new ColorScheme ();
  117. Colors.Dialog = new ColorScheme ();
  118. Colors.Menu = new ColorScheme ();
  119. Colors.Error = new ColorScheme ();
  120. Clip = new Rect (0, 0, Cols, Rows);
  121. Colors.TopLevel.Normal = MakeColor (ConsoleColor.Green, ConsoleColor.Black);
  122. Colors.TopLevel.Focus = MakeColor (ConsoleColor.White, ConsoleColor.DarkCyan);
  123. Colors.TopLevel.HotNormal = MakeColor (ConsoleColor.DarkYellow, ConsoleColor.Black);
  124. Colors.TopLevel.HotFocus = MakeColor (ConsoleColor.DarkBlue, ConsoleColor.DarkCyan);
  125. Colors.Base.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Blue);
  126. Colors.Base.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Cyan);
  127. Colors.Base.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Blue);
  128. Colors.Base.HotFocus = MakeColor (ConsoleColor.Yellow, ConsoleColor.Cyan);
  129. // Focused,
  130. // Selected, Hot: Yellow on Black
  131. // Selected, text: white on black
  132. // Unselected, hot: yellow on cyan
  133. // unselected, text: same as unfocused
  134. Colors.Menu.HotFocus = MakeColor (ConsoleColor.Yellow, ConsoleColor.Black);
  135. Colors.Menu.Focus = MakeColor (ConsoleColor.White, ConsoleColor.Black);
  136. Colors.Menu.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Cyan);
  137. Colors.Menu.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Cyan);
  138. Colors.Menu.Disabled = MakeColor (ConsoleColor.DarkGray, ConsoleColor.Cyan);
  139. Colors.Dialog.Normal = MakeColor (ConsoleColor.Black, ConsoleColor.Gray);
  140. Colors.Dialog.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Cyan);
  141. Colors.Dialog.HotNormal = MakeColor (ConsoleColor.Blue, ConsoleColor.Gray);
  142. Colors.Dialog.HotFocus = MakeColor (ConsoleColor.Blue, ConsoleColor.Cyan);
  143. Colors.Error.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Red);
  144. Colors.Error.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Gray);
  145. Colors.Error.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Red);
  146. Colors.Error.HotFocus = Colors.Error.HotNormal;
  147. //MockConsole.Clear ();
  148. }
  149. public override Attribute MakeAttribute (Color fore, Color back)
  150. {
  151. return MakeColor ((ConsoleColor)fore, (ConsoleColor)back);
  152. }
  153. int redrawColor = -1;
  154. void SetColor (int color)
  155. {
  156. redrawColor = color;
  157. IEnumerable<int> values = Enum.GetValues (typeof (ConsoleColor))
  158. .OfType<ConsoleColor> ()
  159. .Select (s => (int)s);
  160. if (values.Contains (color & 0xffff)) {
  161. FakeConsole.BackgroundColor = (ConsoleColor)(color & 0xffff);
  162. }
  163. if (values.Contains ((color >> 16) & 0xffff)) {
  164. FakeConsole.ForegroundColor = (ConsoleColor)((color >> 16) & 0xffff);
  165. }
  166. }
  167. public override void UpdateScreen ()
  168. {
  169. int rows = Rows;
  170. int cols = Cols;
  171. FakeConsole.CursorTop = 0;
  172. FakeConsole.CursorLeft = 0;
  173. for (int row = 0; row < rows; row++) {
  174. dirtyLine [row] = false;
  175. for (int col = 0; col < cols; col++) {
  176. contents [row, col, 2] = 0;
  177. var color = contents [row, col, 1];
  178. if (color != redrawColor)
  179. SetColor (color);
  180. FakeConsole.Write ((char)contents [row, col, 0]);
  181. }
  182. }
  183. }
  184. public override void Refresh ()
  185. {
  186. int rows = Rows;
  187. int cols = Cols;
  188. var savedRow = FakeConsole.CursorTop;
  189. var savedCol = FakeConsole.CursorLeft;
  190. for (int row = 0; row < rows; row++) {
  191. if (!dirtyLine [row])
  192. continue;
  193. dirtyLine [row] = false;
  194. for (int col = 0; col < cols; col++) {
  195. if (contents [row, col, 2] != 1)
  196. continue;
  197. FakeConsole.CursorTop = row;
  198. FakeConsole.CursorLeft = col;
  199. for (; col < cols && contents [row, col, 2] == 1; col++) {
  200. var color = contents [row, col, 1];
  201. if (color != redrawColor)
  202. SetColor (color);
  203. FakeConsole.Write ((char)contents [row, col, 0]);
  204. contents [row, col, 2] = 0;
  205. }
  206. }
  207. }
  208. FakeConsole.CursorTop = savedRow;
  209. FakeConsole.CursorLeft = savedCol;
  210. }
  211. public override void UpdateCursor ()
  212. {
  213. //
  214. }
  215. public override void StartReportingMouseMoves ()
  216. {
  217. }
  218. public override void StopReportingMouseMoves ()
  219. {
  220. }
  221. public override void Suspend ()
  222. {
  223. }
  224. Attribute currentAttribute;
  225. public override void SetAttribute (Attribute c)
  226. {
  227. currentAttribute = c;
  228. }
  229. Key MapKey (ConsoleKeyInfo keyInfo)
  230. {
  231. switch (keyInfo.Key) {
  232. case ConsoleKey.Escape:
  233. return MapKeyModifiers (keyInfo, Key.Esc);
  234. case ConsoleKey.Tab:
  235. return keyInfo.Modifiers == ConsoleModifiers.Shift ? Key.BackTab : Key.Tab;
  236. case ConsoleKey.Home:
  237. return MapKeyModifiers (keyInfo, Key.Home);
  238. case ConsoleKey.End:
  239. return MapKeyModifiers (keyInfo, Key.End);
  240. case ConsoleKey.LeftArrow:
  241. return MapKeyModifiers (keyInfo, Key.CursorLeft);
  242. case ConsoleKey.RightArrow:
  243. return MapKeyModifiers (keyInfo, Key.CursorRight);
  244. case ConsoleKey.UpArrow:
  245. return MapKeyModifiers (keyInfo, Key.CursorUp);
  246. case ConsoleKey.DownArrow:
  247. return MapKeyModifiers (keyInfo, Key.CursorDown);
  248. case ConsoleKey.PageUp:
  249. return MapKeyModifiers (keyInfo, Key.PageUp);
  250. case ConsoleKey.PageDown:
  251. return MapKeyModifiers (keyInfo, Key.PageDown);
  252. case ConsoleKey.Enter:
  253. return MapKeyModifiers (keyInfo, Key.Enter);
  254. case ConsoleKey.Spacebar:
  255. return MapKeyModifiers (keyInfo, keyInfo.KeyChar == 0 ? Key.Space : (Key)keyInfo.KeyChar);
  256. case ConsoleKey.Backspace:
  257. return MapKeyModifiers (keyInfo, Key.Backspace);
  258. case ConsoleKey.Delete:
  259. return MapKeyModifiers (keyInfo, Key.DeleteChar);
  260. case ConsoleKey.Insert:
  261. return MapKeyModifiers (keyInfo, Key.InsertChar);
  262. case ConsoleKey.Oem1:
  263. case ConsoleKey.Oem2:
  264. case ConsoleKey.Oem3:
  265. case ConsoleKey.Oem4:
  266. case ConsoleKey.Oem5:
  267. case ConsoleKey.Oem6:
  268. case ConsoleKey.Oem7:
  269. case ConsoleKey.Oem8:
  270. case ConsoleKey.Oem102:
  271. case ConsoleKey.OemPeriod:
  272. case ConsoleKey.OemComma:
  273. case ConsoleKey.OemPlus:
  274. case ConsoleKey.OemMinus:
  275. if (keyInfo.KeyChar == 0)
  276. return Key.Unknown;
  277. return (Key)((uint)keyInfo.KeyChar);
  278. }
  279. var key = keyInfo.Key;
  280. if (key >= ConsoleKey.A && key <= ConsoleKey.Z) {
  281. var delta = key - ConsoleKey.A;
  282. if (keyInfo.Modifiers == ConsoleModifiers.Control) {
  283. return (Key)(((uint)Key.CtrlMask) | ((uint)Key.A + delta));
  284. }
  285. if (keyInfo.Modifiers == ConsoleModifiers.Alt) {
  286. return (Key)(((uint)Key.AltMask) | ((uint)Key.A + delta));
  287. }
  288. if ((keyInfo.Modifiers & (ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
  289. if (keyInfo.KeyChar == 0) {
  290. return (Key)(((uint)Key.AltMask | (uint)Key.CtrlMask) | ((uint)Key.A + delta));
  291. } else {
  292. return (Key)((uint)keyInfo.KeyChar);
  293. }
  294. }
  295. return (Key)((uint)keyInfo.KeyChar);
  296. }
  297. if (key >= ConsoleKey.D0 && key <= ConsoleKey.D9) {
  298. var delta = key - ConsoleKey.D0;
  299. if (keyInfo.Modifiers == ConsoleModifiers.Alt) {
  300. return (Key)(((uint)Key.AltMask) | ((uint)Key.D0 + delta));
  301. }
  302. if (keyInfo.Modifiers == ConsoleModifiers.Control) {
  303. return (Key)(((uint)Key.CtrlMask) | ((uint)Key.D0 + delta));
  304. }
  305. if (keyInfo.KeyChar == 0 || keyInfo.KeyChar == 30) {
  306. return MapKeyModifiers (keyInfo, (Key)((uint)Key.D0 + delta));
  307. }
  308. return (Key)((uint)keyInfo.KeyChar);
  309. }
  310. if (key >= ConsoleKey.F1 && key <= ConsoleKey.F12) {
  311. var delta = key - ConsoleKey.F1;
  312. if ((keyInfo.Modifiers & (ConsoleModifiers.Shift | ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
  313. return MapKeyModifiers (keyInfo, (Key)((uint)Key.F1 + delta));
  314. }
  315. return (Key)((uint)Key.F1 + delta);
  316. }
  317. if (keyInfo.KeyChar != 0) {
  318. return MapKeyModifiers (keyInfo, (Key)((uint)keyInfo.KeyChar));
  319. }
  320. return (Key)(0xffffffff);
  321. }
  322. KeyModifiers keyModifiers;
  323. private Key MapKeyModifiers (ConsoleKeyInfo keyInfo, Key key)
  324. {
  325. Key keyMod = new Key ();
  326. if ((keyInfo.Modifiers & ConsoleModifiers.Shift) != 0)
  327. keyMod = Key.ShiftMask;
  328. if ((keyInfo.Modifiers & ConsoleModifiers.Control) != 0)
  329. keyMod |= Key.CtrlMask;
  330. if ((keyInfo.Modifiers & ConsoleModifiers.Alt) != 0)
  331. keyMod |= Key.AltMask;
  332. return keyMod != Key.Null ? keyMod | key : key;
  333. }
  334. Action<KeyEvent> keyHandler;
  335. Action<KeyEvent> keyUpHandler;
  336. public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler)
  337. {
  338. this.keyHandler = keyHandler;
  339. this.keyUpHandler = keyUpHandler;
  340. // Note: Net doesn't support keydown/up events and thus any passed keyDown/UpHandlers will never be called
  341. (mainLoop.Driver as FakeMainLoop).KeyPressed += (consoleKey) => ProcessInput (consoleKey);
  342. }
  343. void ProcessInput (ConsoleKeyInfo consoleKey)
  344. {
  345. var map = MapKey (consoleKey);
  346. if (map == (Key)0xffffffff)
  347. return;
  348. if (keyModifiers == null) {
  349. keyModifiers = new KeyModifiers ();
  350. }
  351. if (consoleKey.Modifiers.HasFlag (ConsoleModifiers.Alt)) {
  352. keyModifiers.Alt = true;
  353. }
  354. if (consoleKey.Modifiers.HasFlag (ConsoleModifiers.Shift)) {
  355. keyModifiers.Shift = true;
  356. }
  357. if (consoleKey.Modifiers.HasFlag (ConsoleModifiers.Control)) {
  358. keyModifiers.Ctrl = true;
  359. }
  360. keyHandler (new KeyEvent (map, keyModifiers));
  361. keyUpHandler (new KeyEvent (map, keyModifiers));
  362. keyModifiers = new KeyModifiers ();
  363. }
  364. public override Attribute GetAttribute ()
  365. {
  366. return currentAttribute;
  367. }
  368. /// <inheritdoc/>
  369. public override bool GetCursorVisibility (out CursorVisibility visibility)
  370. {
  371. visibility = CursorVisibility.Default;
  372. return false;
  373. }
  374. /// <inheritdoc/>
  375. public override bool SetCursorVisibility (CursorVisibility visibility)
  376. {
  377. return false;
  378. }
  379. /// <inheritdoc/>
  380. public override bool EnsureCursorVisibility ()
  381. {
  382. return false;
  383. }
  384. public override void SendKeys (char keyChar, ConsoleKey key, bool shift, bool alt, bool control)
  385. {
  386. ProcessInput (new ConsoleKeyInfo (keyChar, key, shift, alt, control));
  387. }
  388. #region Unused
  389. public override void SetColors (ConsoleColor foreground, ConsoleColor background)
  390. {
  391. }
  392. public override void SetColors (short foregroundColorId, short backgroundColorId)
  393. {
  394. throw new NotImplementedException ();
  395. }
  396. public override void CookMouse ()
  397. {
  398. }
  399. public override void UncookMouse ()
  400. {
  401. }
  402. #endregion
  403. #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
  404. }
  405. }