FakeDriver.cs 11 KB

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