FakeDriver.cs 11 KB

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