FakeDriver.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 MapKeyModifiers (keyInfo, Key.Esc);
  280. case ConsoleKey.Tab:
  281. return keyInfo.Modifiers == ConsoleModifiers.Shift ? Key.BackTab : Key.Tab;
  282. case ConsoleKey.Home:
  283. return MapKeyModifiers (keyInfo, Key.Home);
  284. case ConsoleKey.End:
  285. return MapKeyModifiers (keyInfo, Key.End);
  286. case ConsoleKey.LeftArrow:
  287. return MapKeyModifiers (keyInfo, Key.CursorLeft);
  288. case ConsoleKey.RightArrow:
  289. return MapKeyModifiers (keyInfo, Key.CursorRight);
  290. case ConsoleKey.UpArrow:
  291. return MapKeyModifiers (keyInfo, Key.CursorUp);
  292. case ConsoleKey.DownArrow:
  293. return MapKeyModifiers (keyInfo, Key.CursorDown);
  294. case ConsoleKey.PageUp:
  295. return MapKeyModifiers (keyInfo, Key.PageUp);
  296. case ConsoleKey.PageDown:
  297. return MapKeyModifiers (keyInfo, Key.PageDown);
  298. case ConsoleKey.Enter:
  299. return MapKeyModifiers (keyInfo, Key.Enter);
  300. case ConsoleKey.Spacebar:
  301. return MapKeyModifiers (keyInfo, Key.Space);
  302. case ConsoleKey.Backspace:
  303. return MapKeyModifiers (keyInfo, Key.Backspace);
  304. case ConsoleKey.Delete:
  305. return MapKeyModifiers (keyInfo, Key.DeleteChar);
  306. case ConsoleKey.Insert:
  307. return MapKeyModifiers (keyInfo, Key.InsertChar);
  308. case ConsoleKey.Oem1:
  309. case ConsoleKey.Oem2:
  310. case ConsoleKey.Oem3:
  311. case ConsoleKey.Oem4:
  312. case ConsoleKey.Oem5:
  313. case ConsoleKey.Oem6:
  314. case ConsoleKey.Oem7:
  315. case ConsoleKey.Oem8:
  316. case ConsoleKey.Oem102:
  317. case ConsoleKey.OemPeriod:
  318. case ConsoleKey.OemComma:
  319. case ConsoleKey.OemPlus:
  320. case ConsoleKey.OemMinus:
  321. if (keyInfo.KeyChar == 0)
  322. return Key.Unknown;
  323. return (Key)((uint)keyInfo.KeyChar);
  324. }
  325. var key = keyInfo.Key;
  326. if (key >= ConsoleKey.A && key <= ConsoleKey.Z) {
  327. var delta = key - ConsoleKey.A;
  328. if (keyInfo.Modifiers == ConsoleModifiers.Control) {
  329. return (Key)(((uint)Key.CtrlMask) | ((uint)Key.A + delta));
  330. }
  331. if (keyInfo.Modifiers == ConsoleModifiers.Alt) {
  332. return (Key)(((uint)Key.AltMask) | ((uint)Key.A + delta));
  333. }
  334. if ((keyInfo.Modifiers & (ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
  335. if (keyInfo.KeyChar == 0) {
  336. return (Key)(((uint)Key.AltMask | (uint)Key.CtrlMask) | ((uint)Key.A + delta));
  337. } else {
  338. return (Key)((uint)keyInfo.KeyChar);
  339. }
  340. }
  341. return (Key)((uint)keyInfo.KeyChar);
  342. }
  343. if (key >= ConsoleKey.D0 && key <= ConsoleKey.D9) {
  344. var delta = key - ConsoleKey.D0;
  345. if (keyInfo.Modifiers == ConsoleModifiers.Alt) {
  346. return (Key)(((uint)Key.AltMask) | ((uint)Key.D0 + delta));
  347. }
  348. if (keyInfo.Modifiers == ConsoleModifiers.Control) {
  349. return (Key)(((uint)Key.CtrlMask) | ((uint)Key.D0 + delta));
  350. }
  351. if (keyInfo.KeyChar == 0 || keyInfo.KeyChar == 30) {
  352. return MapKeyModifiers (keyInfo, (Key)((uint)Key.D0 + delta));
  353. }
  354. return (Key)((uint)keyInfo.KeyChar);
  355. }
  356. if (key >= ConsoleKey.F1 && key <= ConsoleKey.F12) {
  357. var delta = key - ConsoleKey.F1;
  358. if ((keyInfo.Modifiers & (ConsoleModifiers.Shift | ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
  359. return MapKeyModifiers (keyInfo, (Key)((uint)Key.F1 + delta));
  360. }
  361. return (Key)((uint)Key.F1 + delta);
  362. }
  363. return (Key)(0xffffffff);
  364. }
  365. KeyModifiers keyModifiers;
  366. private Key MapKeyModifiers (ConsoleKeyInfo keyInfo, Key key)
  367. {
  368. Key keyMod = new Key ();
  369. if ((keyInfo.Modifiers & ConsoleModifiers.Shift) != 0)
  370. keyMod = Key.ShiftMask;
  371. if ((keyInfo.Modifiers & ConsoleModifiers.Control) != 0)
  372. keyMod |= Key.CtrlMask;
  373. if ((keyInfo.Modifiers & ConsoleModifiers.Alt) != 0)
  374. keyMod |= Key.AltMask;
  375. return keyMod != Key.Null ? keyMod | key : key;
  376. }
  377. /// <summary>
  378. ///
  379. /// </summary>
  380. /// <param name="mainLoop"></param>
  381. /// <param name="keyHandler"></param>
  382. /// <param name="keyDownHandler"></param>
  383. /// <param name="keyUpHandler"></param>
  384. /// <param name="mouseHandler"></param>
  385. public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler)
  386. {
  387. // Note: Net doesn't support keydown/up events and thus any passed keyDown/UpHandlers will never be called
  388. (mainLoop.Driver as NetMainLoop).KeyPressed = delegate (ConsoleKeyInfo consoleKey) {
  389. var map = MapKey (consoleKey);
  390. if (map == (Key)0xffffffff)
  391. return;
  392. if (keyModifiers == null)
  393. keyModifiers = new KeyModifiers ();
  394. switch (consoleKey.Modifiers) {
  395. case ConsoleModifiers.Alt:
  396. keyModifiers.Alt = true;
  397. break;
  398. case ConsoleModifiers.Shift:
  399. keyModifiers.Shift = true;
  400. break;
  401. case ConsoleModifiers.Control:
  402. keyModifiers.Ctrl = true;
  403. break;
  404. }
  405. keyHandler (new KeyEvent (map, keyModifiers));
  406. keyUpHandler (new KeyEvent (map, keyModifiers));
  407. };
  408. }
  409. /// <summary>
  410. ///
  411. /// </summary>
  412. /// <param name="foreground"></param>
  413. /// <param name="background"></param>
  414. public override void SetColors (ConsoleColor foreground, ConsoleColor background)
  415. {
  416. throw new NotImplementedException ();
  417. }
  418. /// <summary>
  419. ///
  420. /// </summary>
  421. /// <param name="foregroundColorId"></param>
  422. /// <param name="backgroundColorId"></param>
  423. public override void SetColors (short foregroundColorId, short backgroundColorId)
  424. {
  425. throw new NotImplementedException ();
  426. }
  427. /// <summary>
  428. ///
  429. /// </summary>
  430. public override void CookMouse ()
  431. {
  432. }
  433. /// <summary>
  434. ///
  435. /// </summary>
  436. public override void UncookMouse ()
  437. {
  438. }
  439. }
  440. }