MockDriver.cs 12 KB

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