FakeDriver.cs 12 KB

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