FakeDriver.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 (
  104. value: ((((int)f) & 0xffff) << 16) | (((int)b) & 0xffff),
  105. foreground: (Color)f,
  106. background: (Color)b
  107. );
  108. }
  109. public override void Init (Action terminalResized)
  110. {
  111. Colors.TopLevel = new ColorScheme ();
  112. Colors.Base = new ColorScheme ();
  113. Colors.Dialog = new ColorScheme ();
  114. Colors.Menu = new ColorScheme ();
  115. Colors.Error = new ColorScheme ();
  116. Clip = new Rect (0, 0, Cols, Rows);
  117. Colors.TopLevel.Normal = MakeColor (ConsoleColor.Green, ConsoleColor.Black);
  118. Colors.TopLevel.Focus = MakeColor (ConsoleColor.White, ConsoleColor.DarkCyan);
  119. Colors.TopLevel.HotNormal = MakeColor (ConsoleColor.DarkYellow, ConsoleColor.Black);
  120. Colors.TopLevel.HotFocus = MakeColor (ConsoleColor.DarkBlue, ConsoleColor.DarkCyan);
  121. Colors.Base.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Blue);
  122. Colors.Base.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Cyan);
  123. Colors.Base.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Blue);
  124. Colors.Base.HotFocus = MakeColor (ConsoleColor.Yellow, ConsoleColor.Cyan);
  125. // Focused,
  126. // Selected, Hot: Yellow on Black
  127. // Selected, text: white on black
  128. // Unselected, hot: yellow on cyan
  129. // unselected, text: same as unfocused
  130. Colors.Menu.HotFocus = MakeColor (ConsoleColor.Yellow, ConsoleColor.Black);
  131. Colors.Menu.Focus = MakeColor (ConsoleColor.White, ConsoleColor.Black);
  132. Colors.Menu.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Cyan);
  133. Colors.Menu.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Cyan);
  134. Colors.Menu.Disabled = MakeColor (ConsoleColor.DarkGray, ConsoleColor.Cyan);
  135. Colors.Dialog.Normal = MakeColor (ConsoleColor.Black, ConsoleColor.Gray);
  136. Colors.Dialog.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Cyan);
  137. Colors.Dialog.HotNormal = MakeColor (ConsoleColor.Blue, ConsoleColor.Gray);
  138. Colors.Dialog.HotFocus = MakeColor (ConsoleColor.Blue, ConsoleColor.Cyan);
  139. Colors.Error.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Red);
  140. Colors.Error.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Gray);
  141. Colors.Error.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Red);
  142. Colors.Error.HotFocus = Colors.Error.HotNormal;
  143. //MockConsole.Clear ();
  144. }
  145. public override Attribute MakeAttribute (Color fore, Color back)
  146. {
  147. return MakeColor ((ConsoleColor)fore, (ConsoleColor)back);
  148. }
  149. int redrawColor = -1;
  150. void SetColor (int color)
  151. {
  152. redrawColor = color;
  153. IEnumerable<int> values = Enum.GetValues (typeof (ConsoleColor))
  154. .OfType<ConsoleColor> ()
  155. .Select (s => (int)s);
  156. if (values.Contains (color & 0xffff)) {
  157. FakeConsole.BackgroundColor = (ConsoleColor)(color & 0xffff);
  158. }
  159. if (values.Contains ((color >> 16) & 0xffff)) {
  160. FakeConsole.ForegroundColor = (ConsoleColor)((color >> 16) & 0xffff);
  161. }
  162. }
  163. public override void UpdateScreen ()
  164. {
  165. int rows = Rows;
  166. int cols = Cols;
  167. FakeConsole.CursorTop = 0;
  168. FakeConsole.CursorLeft = 0;
  169. for (int row = 0; row < rows; row++) {
  170. dirtyLine [row] = false;
  171. for (int col = 0; col < cols; col++) {
  172. contents [row, col, 2] = 0;
  173. var color = contents [row, col, 1];
  174. if (color != redrawColor)
  175. SetColor (color);
  176. FakeConsole.Write ((char)contents [row, col, 0]);
  177. }
  178. }
  179. }
  180. public override void Refresh ()
  181. {
  182. int rows = Rows;
  183. int cols = Cols;
  184. var savedRow = FakeConsole.CursorTop;
  185. var savedCol = FakeConsole.CursorLeft;
  186. for (int row = 0; row < rows; row++) {
  187. if (!dirtyLine [row])
  188. continue;
  189. dirtyLine [row] = false;
  190. for (int col = 0; col < cols; col++) {
  191. if (contents [row, col, 2] != 1)
  192. continue;
  193. FakeConsole.CursorTop = row;
  194. FakeConsole.CursorLeft = col;
  195. for (; col < cols && contents [row, col, 2] == 1; col++) {
  196. var color = contents [row, col, 1];
  197. if (color != redrawColor)
  198. SetColor (color);
  199. FakeConsole.Write ((char)contents [row, col, 0]);
  200. contents [row, col, 2] = 0;
  201. }
  202. }
  203. }
  204. FakeConsole.CursorTop = savedRow;
  205. FakeConsole.CursorLeft = savedCol;
  206. }
  207. public override void UpdateCursor ()
  208. {
  209. //
  210. }
  211. public override void StartReportingMouseMoves ()
  212. {
  213. }
  214. public override void StopReportingMouseMoves ()
  215. {
  216. }
  217. public override void Suspend ()
  218. {
  219. }
  220. int currentAttribute;
  221. public override void SetAttribute (Attribute c)
  222. {
  223. currentAttribute = c.Value;
  224. }
  225. Key MapKey (ConsoleKeyInfo keyInfo)
  226. {
  227. switch (keyInfo.Key) {
  228. case ConsoleKey.Escape:
  229. return MapKeyModifiers (keyInfo, Key.Esc);
  230. case ConsoleKey.Tab:
  231. return keyInfo.Modifiers == ConsoleModifiers.Shift ? Key.BackTab : Key.Tab;
  232. case ConsoleKey.Home:
  233. return MapKeyModifiers (keyInfo, Key.Home);
  234. case ConsoleKey.End:
  235. return MapKeyModifiers (keyInfo, Key.End);
  236. case ConsoleKey.LeftArrow:
  237. return MapKeyModifiers (keyInfo, Key.CursorLeft);
  238. case ConsoleKey.RightArrow:
  239. return MapKeyModifiers (keyInfo, Key.CursorRight);
  240. case ConsoleKey.UpArrow:
  241. return MapKeyModifiers (keyInfo, Key.CursorUp);
  242. case ConsoleKey.DownArrow:
  243. return MapKeyModifiers (keyInfo, Key.CursorDown);
  244. case ConsoleKey.PageUp:
  245. return MapKeyModifiers (keyInfo, Key.PageUp);
  246. case ConsoleKey.PageDown:
  247. return MapKeyModifiers (keyInfo, Key.PageDown);
  248. case ConsoleKey.Enter:
  249. return MapKeyModifiers (keyInfo, Key.Enter);
  250. case ConsoleKey.Spacebar:
  251. return MapKeyModifiers (keyInfo, Key.Space);
  252. case ConsoleKey.Backspace:
  253. return MapKeyModifiers (keyInfo, Key.Backspace);
  254. case ConsoleKey.Delete:
  255. return MapKeyModifiers (keyInfo, Key.DeleteChar);
  256. case ConsoleKey.Insert:
  257. return MapKeyModifiers (keyInfo, Key.InsertChar);
  258. case ConsoleKey.Oem1:
  259. case ConsoleKey.Oem2:
  260. case ConsoleKey.Oem3:
  261. case ConsoleKey.Oem4:
  262. case ConsoleKey.Oem5:
  263. case ConsoleKey.Oem6:
  264. case ConsoleKey.Oem7:
  265. case ConsoleKey.Oem8:
  266. case ConsoleKey.Oem102:
  267. case ConsoleKey.OemPeriod:
  268. case ConsoleKey.OemComma:
  269. case ConsoleKey.OemPlus:
  270. case ConsoleKey.OemMinus:
  271. if (keyInfo.KeyChar == 0)
  272. return Key.Unknown;
  273. return (Key)((uint)keyInfo.KeyChar);
  274. }
  275. var key = keyInfo.Key;
  276. if (key >= ConsoleKey.A && key <= ConsoleKey.Z) {
  277. var delta = key - ConsoleKey.A;
  278. if (keyInfo.Modifiers == ConsoleModifiers.Control) {
  279. return (Key)(((uint)Key.CtrlMask) | ((uint)Key.A + delta));
  280. }
  281. if (keyInfo.Modifiers == ConsoleModifiers.Alt) {
  282. return (Key)(((uint)Key.AltMask) | ((uint)Key.A + delta));
  283. }
  284. if ((keyInfo.Modifiers & (ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
  285. if (keyInfo.KeyChar == 0) {
  286. return (Key)(((uint)Key.AltMask | (uint)Key.CtrlMask) | ((uint)Key.A + delta));
  287. } else {
  288. return (Key)((uint)keyInfo.KeyChar);
  289. }
  290. }
  291. return (Key)((uint)keyInfo.KeyChar);
  292. }
  293. if (key >= ConsoleKey.D0 && key <= ConsoleKey.D9) {
  294. var delta = key - ConsoleKey.D0;
  295. if (keyInfo.Modifiers == ConsoleModifiers.Alt) {
  296. return (Key)(((uint)Key.AltMask) | ((uint)Key.D0 + delta));
  297. }
  298. if (keyInfo.Modifiers == ConsoleModifiers.Control) {
  299. return (Key)(((uint)Key.CtrlMask) | ((uint)Key.D0 + delta));
  300. }
  301. if (keyInfo.KeyChar == 0 || keyInfo.KeyChar == 30) {
  302. return MapKeyModifiers (keyInfo, (Key)((uint)Key.D0 + delta));
  303. }
  304. return (Key)((uint)keyInfo.KeyChar);
  305. }
  306. if (key >= ConsoleKey.F1 && key <= ConsoleKey.F12) {
  307. var delta = key - ConsoleKey.F1;
  308. if ((keyInfo.Modifiers & (ConsoleModifiers.Shift | ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
  309. return MapKeyModifiers (keyInfo, (Key)((uint)Key.F1 + delta));
  310. }
  311. return (Key)((uint)Key.F1 + delta);
  312. }
  313. return (Key)(0xffffffff);
  314. }
  315. KeyModifiers keyModifiers;
  316. private Key MapKeyModifiers (ConsoleKeyInfo keyInfo, Key key)
  317. {
  318. Key keyMod = new Key ();
  319. if ((keyInfo.Modifiers & ConsoleModifiers.Shift) != 0)
  320. keyMod = Key.ShiftMask;
  321. if ((keyInfo.Modifiers & ConsoleModifiers.Control) != 0)
  322. keyMod |= Key.CtrlMask;
  323. if ((keyInfo.Modifiers & ConsoleModifiers.Alt) != 0)
  324. keyMod |= Key.AltMask;
  325. return keyMod != Key.Null ? keyMod | key : key;
  326. }
  327. public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler)
  328. {
  329. // Note: Net doesn't support keydown/up events and thus any passed keyDown/UpHandlers will never be called
  330. (mainLoop.Driver as FakeMainLoop).KeyPressed = delegate (ConsoleKeyInfo consoleKey) {
  331. var map = MapKey (consoleKey);
  332. if (map == (Key)0xffffffff)
  333. return;
  334. if (keyModifiers == null)
  335. keyModifiers = new KeyModifiers ();
  336. switch (consoleKey.Modifiers) {
  337. case ConsoleModifiers.Alt:
  338. keyModifiers.Alt = true;
  339. break;
  340. case ConsoleModifiers.Shift:
  341. keyModifiers.Shift = true;
  342. break;
  343. case ConsoleModifiers.Control:
  344. keyModifiers.Ctrl = true;
  345. break;
  346. }
  347. keyHandler (new KeyEvent (map, keyModifiers));
  348. keyUpHandler (new KeyEvent (map, keyModifiers));
  349. keyModifiers = new KeyModifiers ();
  350. };
  351. }
  352. public override Attribute GetAttribute ()
  353. {
  354. return currentAttribute;
  355. }
  356. #region Unused
  357. public override void SetColors (ConsoleColor foreground, ConsoleColor background)
  358. {
  359. }
  360. public override void SetColors (short foregroundColorId, short backgroundColorId)
  361. {
  362. throw new NotImplementedException ();
  363. }
  364. public override void CookMouse ()
  365. {
  366. }
  367. public override void UncookMouse ()
  368. {
  369. }
  370. public override bool GetCursorVisibility (out CursorVisibility visibility)
  371. {
  372. visibility = CursorVisibility.Normal;
  373. return true;
  374. }
  375. public override bool SetCursorVisibility (CursorVisibility visibility)
  376. {
  377. return true;
  378. }
  379. public override bool EnsureCursorVisibility ()
  380. {
  381. return false;
  382. }
  383. #endregion
  384. #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
  385. }
  386. }