FakeDriver.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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, IMainLoopDriver {
  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. if (Clip.Contains (ccol, crow)) {
  84. if (needMove) {
  85. //MockConsole.CursorLeft = ccol;
  86. //MockConsole.CursorTop = crow;
  87. needMove = false;
  88. }
  89. contents [crow, ccol, 0] = (int)(uint)rune;
  90. contents [crow, ccol, 1] = currentAttribute;
  91. contents [crow, ccol, 2] = 1;
  92. dirtyLine [crow] = true;
  93. } else
  94. needMove = true;
  95. ccol++;
  96. //if (ccol == Cols) {
  97. // ccol = 0;
  98. // if (crow + 1 < Rows)
  99. // crow++;
  100. //}
  101. if (sync)
  102. UpdateScreen ();
  103. }
  104. /// <summary>
  105. ///
  106. /// </summary>
  107. /// <param name="str"></param>
  108. public override void AddStr (ustring str)
  109. {
  110. foreach (var rune in str)
  111. AddRune (rune);
  112. }
  113. /// <summary>
  114. ///
  115. /// </summary>
  116. public override void End ()
  117. {
  118. FakeConsole.ResetColor ();
  119. FakeConsole.Clear ();
  120. }
  121. static Attribute MakeColor (ConsoleColor f, ConsoleColor b)
  122. {
  123. // Encode the colors into the int value.
  124. return new Attribute () { value = ((((int)f) & 0xffff) << 16) | (((int)b) & 0xffff) };
  125. }
  126. /// <summary>
  127. ///
  128. /// </summary>
  129. /// <param name="terminalResized"></param>
  130. public override void Init (Action terminalResized)
  131. {
  132. Colors.TopLevel = new ColorScheme ();
  133. Colors.Base = new ColorScheme ();
  134. Colors.Dialog = new ColorScheme ();
  135. Colors.Menu = new ColorScheme ();
  136. Colors.Error = new ColorScheme ();
  137. Clip = new Rect (0, 0, Cols, Rows);
  138. Colors.TopLevel.Normal = MakeColor (ConsoleColor.Green, ConsoleColor.Black);
  139. Colors.TopLevel.Focus = MakeColor (ConsoleColor.White, ConsoleColor.DarkCyan);
  140. Colors.TopLevel.HotNormal = MakeColor (ConsoleColor.DarkYellow, ConsoleColor.Black);
  141. Colors.TopLevel.HotFocus = MakeColor (ConsoleColor.DarkBlue, ConsoleColor.DarkCyan);
  142. Colors.Base.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Blue);
  143. Colors.Base.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Cyan);
  144. Colors.Base.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Blue);
  145. Colors.Base.HotFocus = MakeColor (ConsoleColor.Yellow, ConsoleColor.Cyan);
  146. // Focused,
  147. // Selected, Hot: Yellow on Black
  148. // Selected, text: white on black
  149. // Unselected, hot: yellow on cyan
  150. // unselected, text: same as unfocused
  151. Colors.Menu.HotFocus = MakeColor (ConsoleColor.Yellow, ConsoleColor.Black);
  152. Colors.Menu.Focus = MakeColor (ConsoleColor.White, ConsoleColor.Black);
  153. Colors.Menu.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Cyan);
  154. Colors.Menu.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Cyan);
  155. Colors.Menu.Disabled = MakeColor (ConsoleColor.DarkGray, ConsoleColor.Cyan);
  156. Colors.Dialog.Normal = MakeColor (ConsoleColor.Black, ConsoleColor.Gray);
  157. Colors.Dialog.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Cyan);
  158. Colors.Dialog.HotNormal = MakeColor (ConsoleColor.Blue, ConsoleColor.Gray);
  159. Colors.Dialog.HotFocus = MakeColor (ConsoleColor.Blue, ConsoleColor.Cyan);
  160. Colors.Error.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Red);
  161. Colors.Error.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Gray);
  162. Colors.Error.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Red);
  163. Colors.Error.HotFocus = Colors.Error.HotNormal;
  164. HLine = '\u2500';
  165. VLine = '\u2502';
  166. Stipple = '\u2592';
  167. Diamond = '\u25c6';
  168. ULCorner = '\u250C';
  169. LLCorner = '\u2514';
  170. URCorner = '\u2510';
  171. LRCorner = '\u2518';
  172. LeftTee = '\u251c';
  173. RightTee = '\u2524';
  174. TopTee = '\u22a4';
  175. BottomTee = '\u22a5';
  176. Checked = '\u221a';
  177. UnChecked = ' ';
  178. Selected = '\u25cf';
  179. UnSelected = '\u25cc';
  180. RightArrow = '\u25ba';
  181. LeftArrow = '\u25c4';
  182. UpArrow = '\u25b2';
  183. DownArrow = '\u25bc';
  184. LeftDefaultIndicator = '\u25e6';
  185. RightDefaultIndicator = '\u25e6';
  186. LeftBracket = '[';
  187. RightBracket = ']';
  188. OnMeterSegment = '\u258c';
  189. OffMeterSegement = ' ';
  190. //MockConsole.Clear ();
  191. }
  192. /// <summary>
  193. ///
  194. /// </summary>
  195. /// <param name="fore"></param>
  196. /// <param name="back"></param>
  197. /// <returns></returns>
  198. public override Attribute MakeAttribute (Color fore, Color back)
  199. {
  200. return MakeColor ((ConsoleColor)fore, (ConsoleColor)back);
  201. }
  202. int redrawColor = -1;
  203. void SetColor (int color)
  204. {
  205. redrawColor = color;
  206. IEnumerable<int> values = Enum.GetValues (typeof (ConsoleColor))
  207. .OfType<ConsoleColor> ()
  208. .Select (s => (int)s);
  209. if (values.Contains (color & 0xffff)) {
  210. FakeConsole.BackgroundColor = (ConsoleColor)(color & 0xffff);
  211. }
  212. if (values.Contains ((color >> 16) & 0xffff)) {
  213. FakeConsole.ForegroundColor = (ConsoleColor)((color >> 16) & 0xffff);
  214. }
  215. }
  216. /// <summary>
  217. ///
  218. /// </summary>
  219. public override void UpdateScreen ()
  220. {
  221. int rows = Rows;
  222. int cols = Cols;
  223. FakeConsole.CursorTop = 0;
  224. FakeConsole.CursorLeft = 0;
  225. for (int row = 0; row < rows; row++) {
  226. dirtyLine [row] = false;
  227. for (int col = 0; col < cols; col++) {
  228. contents [row, col, 2] = 0;
  229. var color = contents [row, col, 1];
  230. if (color != redrawColor)
  231. SetColor (color);
  232. FakeConsole.Write ((char)contents [row, col, 0]);
  233. }
  234. }
  235. }
  236. /// <summary>
  237. ///
  238. /// </summary>
  239. public override void Refresh ()
  240. {
  241. int rows = Rows;
  242. int cols = Cols;
  243. var savedRow = FakeConsole.CursorTop;
  244. var savedCol = FakeConsole.CursorLeft;
  245. for (int row = 0; row < rows; row++) {
  246. if (!dirtyLine [row])
  247. continue;
  248. dirtyLine [row] = false;
  249. for (int col = 0; col < cols; col++) {
  250. if (contents [row, col, 2] != 1)
  251. continue;
  252. FakeConsole.CursorTop = row;
  253. FakeConsole.CursorLeft = col;
  254. for (; col < cols && contents [row, col, 2] == 1; col++) {
  255. var color = contents [row, col, 1];
  256. if (color != redrawColor)
  257. SetColor (color);
  258. FakeConsole.Write ((char)contents [row, col, 0]);
  259. contents [row, col, 2] = 0;
  260. }
  261. }
  262. }
  263. FakeConsole.CursorTop = savedRow;
  264. FakeConsole.CursorLeft = savedCol;
  265. }
  266. /// <summary>
  267. ///
  268. /// </summary>
  269. public override void UpdateCursor ()
  270. {
  271. //
  272. }
  273. /// <summary>
  274. ///
  275. /// </summary>
  276. public override void StartReportingMouseMoves ()
  277. {
  278. }
  279. /// <summary>
  280. ///
  281. /// </summary>
  282. public override void StopReportingMouseMoves ()
  283. {
  284. }
  285. /// <summary>
  286. ///
  287. /// </summary>
  288. public override void Suspend ()
  289. {
  290. }
  291. int currentAttribute;
  292. /// <summary>
  293. ///
  294. /// </summary>
  295. /// <param name="c"></param>
  296. public override void SetAttribute (Attribute c)
  297. {
  298. currentAttribute = c.value;
  299. }
  300. Key MapKey (ConsoleKeyInfo keyInfo)
  301. {
  302. switch (keyInfo.Key) {
  303. case ConsoleKey.Escape:
  304. return Key.Esc;
  305. case ConsoleKey.Tab:
  306. return keyInfo.Modifiers == ConsoleModifiers.Shift ? Key.BackTab : Key.Tab;
  307. case ConsoleKey.Home:
  308. return Key.Home;
  309. case ConsoleKey.End:
  310. return Key.End;
  311. case ConsoleKey.LeftArrow:
  312. return Key.CursorLeft;
  313. case ConsoleKey.RightArrow:
  314. return Key.CursorRight;
  315. case ConsoleKey.UpArrow:
  316. return Key.CursorUp;
  317. case ConsoleKey.DownArrow:
  318. return Key.CursorDown;
  319. case ConsoleKey.PageUp:
  320. return Key.PageUp;
  321. case ConsoleKey.PageDown:
  322. return Key.PageDown;
  323. case ConsoleKey.Enter:
  324. return Key.Enter;
  325. case ConsoleKey.Spacebar:
  326. return Key.Space;
  327. case ConsoleKey.Backspace:
  328. return Key.Backspace;
  329. case ConsoleKey.Delete:
  330. return Key.Delete;
  331. case ConsoleKey.Oem1:
  332. case ConsoleKey.Oem2:
  333. case ConsoleKey.Oem3:
  334. case ConsoleKey.Oem4:
  335. case ConsoleKey.Oem5:
  336. case ConsoleKey.Oem6:
  337. case ConsoleKey.Oem7:
  338. case ConsoleKey.Oem8:
  339. case ConsoleKey.Oem102:
  340. case ConsoleKey.OemPeriod:
  341. case ConsoleKey.OemComma:
  342. case ConsoleKey.OemPlus:
  343. case ConsoleKey.OemMinus:
  344. return (Key)((uint)keyInfo.KeyChar);
  345. }
  346. var key = keyInfo.Key;
  347. if (key >= ConsoleKey.A && key <= ConsoleKey.Z) {
  348. var delta = key - ConsoleKey.A;
  349. if (keyInfo.Modifiers == ConsoleModifiers.Control)
  350. return (Key)((uint)Key.ControlA + delta);
  351. if (keyInfo.Modifiers == ConsoleModifiers.Alt)
  352. return (Key)(((uint)Key.AltMask) | ((uint)'A' + delta));
  353. if (keyInfo.Modifiers == ConsoleModifiers.Shift)
  354. return (Key)((uint)'A' + delta);
  355. else
  356. return (Key)((uint)'a' + delta);
  357. }
  358. if (key >= ConsoleKey.D0 && key <= ConsoleKey.D9) {
  359. var delta = key - ConsoleKey.D0;
  360. if (keyInfo.Modifiers == ConsoleModifiers.Alt)
  361. return (Key)(((uint)Key.AltMask) | ((uint)'0' + delta));
  362. if (keyInfo.Modifiers == ConsoleModifiers.Shift)
  363. return (Key)((uint)keyInfo.KeyChar);
  364. return (Key)((uint)'0' + delta);
  365. }
  366. if (key >= ConsoleKey.F1 && key <= ConsoleKey.F10) {
  367. var delta = key - ConsoleKey.F1;
  368. return (Key)((int)Key.F1 + delta);
  369. }
  370. return (Key)(0xffffffff);
  371. }
  372. KeyModifiers keyModifiers = new KeyModifiers ();
  373. /// <summary>
  374. ///
  375. /// </summary>
  376. /// <param name="mainLoop"></param>
  377. /// <param name="keyHandler"></param>
  378. /// <param name="keyDownHandler"></param>
  379. /// <param name="keyUpHandler"></param>
  380. /// <param name="mouseHandler"></param>
  381. public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler)
  382. {
  383. // Note: Net doesn't support keydown/up events and thus any passed keyDown/UpHandlers will never be called
  384. (mainLoop.Driver as FakeDriver).WindowsKeyPressed = delegate (ConsoleKeyInfo consoleKey) {
  385. var map = MapKey (consoleKey);
  386. if (map == (Key)0xffffffff)
  387. return;
  388. keyHandler (new KeyEvent (map, keyModifiers));
  389. keyUpHandler (new KeyEvent (map, keyModifiers));
  390. };
  391. }
  392. /// <summary>
  393. ///
  394. /// </summary>
  395. /// <param name="foreground"></param>
  396. /// <param name="background"></param>
  397. public override void SetColors (ConsoleColor foreground, ConsoleColor background)
  398. {
  399. throw new NotImplementedException ();
  400. }
  401. /// <summary>
  402. ///
  403. /// </summary>
  404. /// <param name="foregroundColorId"></param>
  405. /// <param name="backgroundColorId"></param>
  406. public override void SetColors (short foregroundColorId, short backgroundColorId)
  407. {
  408. throw new NotImplementedException ();
  409. }
  410. /// <summary>
  411. ///
  412. /// </summary>
  413. public override void CookMouse ()
  414. {
  415. }
  416. /// <summary>
  417. ///
  418. /// </summary>
  419. public override void UncookMouse ()
  420. {
  421. }
  422. AutoResetEvent keyReady = new AutoResetEvent (false);
  423. AutoResetEvent waitForProbe = new AutoResetEvent (false);
  424. ConsoleKeyInfo? windowsKeyResult = null;
  425. /// <summary>
  426. ///
  427. /// </summary>
  428. public Action<ConsoleKeyInfo> WindowsKeyPressed;
  429. MainLoop mainLoop;
  430. void WindowsKeyReader ()
  431. {
  432. while (true) {
  433. waitForProbe.WaitOne ();
  434. windowsKeyResult = FakeConsole.ReadKey (true);
  435. keyReady.Set ();
  436. }
  437. }
  438. void IMainLoopDriver.Setup (MainLoop mainLoop)
  439. {
  440. this.mainLoop = mainLoop;
  441. Thread readThread = new Thread (WindowsKeyReader);
  442. readThread.Start ();
  443. }
  444. void IMainLoopDriver.Wakeup ()
  445. {
  446. }
  447. bool IMainLoopDriver.EventsPending (bool wait)
  448. {
  449. long now = DateTime.UtcNow.Ticks;
  450. int waitTimeout;
  451. if (mainLoop.timeouts.Count > 0) {
  452. waitTimeout = (int)((mainLoop.timeouts.Keys [0] - now) / TimeSpan.TicksPerMillisecond);
  453. if (waitTimeout < 0)
  454. return true;
  455. } else
  456. waitTimeout = -1;
  457. if (!wait)
  458. waitTimeout = 0;
  459. windowsKeyResult = null;
  460. waitForProbe.Set ();
  461. keyReady.WaitOne (waitTimeout);
  462. return windowsKeyResult.HasValue;
  463. }
  464. void IMainLoopDriver.MainIteration ()
  465. {
  466. if (windowsKeyResult.HasValue) {
  467. if (WindowsKeyPressed != null)
  468. WindowsKeyPressed (windowsKeyResult.Value);
  469. windowsKeyResult = null;
  470. }
  471. }
  472. }
  473. }