FakeDriver.cs 12 KB

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