FakeDriver.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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.Runtime.InteropServices;
  11. using System.Threading;
  12. using NStack;
  13. // Alias Console to MockConsole so we don't accidentally use Console
  14. using Console = Terminal.Gui.FakeConsole;
  15. namespace Terminal.Gui {
  16. /// <summary>
  17. /// Implements a mock ConsoleDriver for unit testing
  18. /// </summary>
  19. public class FakeDriver : ConsoleDriver {
  20. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  21. int cols, rows, left, top;
  22. public override int Cols => cols;
  23. public override int Rows => rows;
  24. // Only handling left here because not all terminals has a horizontal scroll bar.
  25. public override int Left => 0;
  26. public override int Top => 0;
  27. public override bool HeightAsBuffer { get; set; }
  28. public override IClipboard Clipboard { get; }
  29. // The format is rows, columns and 3 values on the last column: Rune, Attribute and Dirty Flag
  30. int [,,] contents;
  31. bool [] dirtyLine;
  32. /// <summary>
  33. /// Assists with testing, the format is rows, columns and 3 values on the last column: Rune, Attribute and Dirty Flag
  34. /// </summary>
  35. public override int [,,] Contents => contents;
  36. //void UpdateOffscreen ()
  37. //{
  38. // int cols = Cols;
  39. // int rows = Rows;
  40. // contents = new int [rows, cols, 3];
  41. // for (int r = 0; r < rows; r++) {
  42. // for (int c = 0; c < cols; c++) {
  43. // contents [r, c, 0] = ' ';
  44. // contents [r, c, 1] = MakeColor (ConsoleColor.Gray, ConsoleColor.Black);
  45. // contents [r, c, 2] = 0;
  46. // }
  47. // }
  48. // dirtyLine = new bool [rows];
  49. // for (int row = 0; row < rows; row++)
  50. // dirtyLine [row] = true;
  51. //}
  52. static bool sync = false;
  53. public FakeDriver ()
  54. {
  55. if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
  56. Clipboard = new WindowsClipboard ();
  57. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
  58. Clipboard = new MacOSXClipboard ();
  59. } else {
  60. if (CursesDriver.Is_WSL_Platform ()) {
  61. Clipboard = new WSLClipboard ();
  62. } else {
  63. Clipboard = new CursesClipboard ();
  64. }
  65. }
  66. }
  67. bool needMove;
  68. // Current row, and current col, tracked by Move/AddCh only
  69. int ccol, crow;
  70. public override void Move (int col, int row)
  71. {
  72. ccol = col;
  73. crow = row;
  74. if (Clip.Contains (col, row)) {
  75. FakeConsole.CursorTop = row;
  76. FakeConsole.CursorLeft = col;
  77. needMove = false;
  78. } else {
  79. FakeConsole.CursorTop = Clip.Y;
  80. FakeConsole.CursorLeft = Clip.X;
  81. needMove = true;
  82. }
  83. }
  84. public override void AddRune (Rune rune)
  85. {
  86. rune = MakePrintable (rune);
  87. var runeWidth = Rune.ColumnWidth (rune);
  88. var validClip = IsValidContent (ccol, crow, Clip);
  89. if (validClip) {
  90. if (needMove) {
  91. //MockConsole.CursorLeft = ccol;
  92. //MockConsole.CursorTop = crow;
  93. needMove = false;
  94. }
  95. if (runeWidth < 2 && ccol > 0
  96. && Rune.ColumnWidth ((char)contents [crow, ccol - 1, 0]) > 1) {
  97. contents [crow, ccol - 1, 0] = (int)(uint)' ';
  98. } else if (runeWidth < 2 && ccol <= Clip.Right - 1
  99. && Rune.ColumnWidth ((char)contents [crow, ccol, 0]) > 1) {
  100. contents [crow, ccol + 1, 0] = (int)(uint)' ';
  101. contents [crow, ccol + 1, 2] = 1;
  102. }
  103. if (runeWidth > 1 && ccol == Clip.Right - 1) {
  104. contents [crow, ccol, 0] = (int)(uint)' ';
  105. } else {
  106. contents [crow, ccol, 0] = (int)(uint)rune;
  107. }
  108. contents [crow, ccol, 1] = currentAttribute;
  109. contents [crow, ccol, 2] = 1;
  110. dirtyLine [crow] = true;
  111. } else
  112. needMove = true;
  113. ccol++;
  114. if (runeWidth > 1) {
  115. if (validClip && ccol < Clip.Right) {
  116. contents [crow, ccol, 1] = currentAttribute;
  117. contents [crow, ccol, 2] = 0;
  118. }
  119. ccol++;
  120. }
  121. //if (ccol == Cols) {
  122. // ccol = 0;
  123. // if (crow + 1 < Rows)
  124. // crow++;
  125. //}
  126. if (sync)
  127. UpdateScreen ();
  128. }
  129. public override void AddStr (ustring str)
  130. {
  131. foreach (var rune in str)
  132. AddRune (rune);
  133. }
  134. public override void End ()
  135. {
  136. FakeConsole.ResetColor ();
  137. FakeConsole.Clear ();
  138. }
  139. static Attribute MakeColor (ConsoleColor f, ConsoleColor b)
  140. {
  141. // Encode the colors into the int value.
  142. return new Attribute (
  143. value: ((((int)f) & 0xffff) << 16) | (((int)b) & 0xffff),
  144. foreground: (Color)f,
  145. background: (Color)b
  146. );
  147. }
  148. public override void Init (Action terminalResized)
  149. {
  150. TerminalResized = terminalResized;
  151. cols = FakeConsole.WindowWidth = FakeConsole.BufferWidth = FakeConsole.WIDTH;
  152. rows = FakeConsole.WindowHeight = FakeConsole.BufferHeight = FakeConsole.HEIGHT;
  153. FakeConsole.Clear ();
  154. ResizeScreen ();
  155. UpdateOffScreen ();
  156. Colors.TopLevel = new ColorScheme ();
  157. Colors.Base = new ColorScheme ();
  158. Colors.Dialog = new ColorScheme ();
  159. Colors.Menu = new ColorScheme ();
  160. Colors.Error = new ColorScheme ();
  161. Clip = new Rect (0, 0, Cols, Rows);
  162. Colors.TopLevel.Normal = MakeColor (ConsoleColor.Green, ConsoleColor.Black);
  163. Colors.TopLevel.Focus = MakeColor (ConsoleColor.White, ConsoleColor.DarkCyan);
  164. Colors.TopLevel.HotNormal = MakeColor (ConsoleColor.DarkYellow, ConsoleColor.Black);
  165. Colors.TopLevel.HotFocus = MakeColor (ConsoleColor.DarkBlue, ConsoleColor.DarkCyan);
  166. Colors.TopLevel.Disabled = MakeColor (ConsoleColor.DarkGray, ConsoleColor.Black);
  167. Colors.Base.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Blue);
  168. Colors.Base.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Cyan);
  169. Colors.Base.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Blue);
  170. Colors.Base.HotFocus = MakeColor (ConsoleColor.Yellow, ConsoleColor.Cyan);
  171. Colors.Base.Disabled = MakeColor (ConsoleColor.DarkGray, ConsoleColor.DarkBlue);
  172. // Focused,
  173. // Selected, Hot: Yellow on Black
  174. // Selected, text: white on black
  175. // Unselected, hot: yellow on cyan
  176. // unselected, text: same as unfocused
  177. Colors.Menu.HotFocus = MakeColor (ConsoleColor.Yellow, ConsoleColor.Black);
  178. Colors.Menu.Focus = MakeColor (ConsoleColor.White, ConsoleColor.Black);
  179. Colors.Menu.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Cyan);
  180. Colors.Menu.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Cyan);
  181. Colors.Menu.Disabled = MakeColor (ConsoleColor.DarkGray, ConsoleColor.Cyan);
  182. Colors.Dialog.Normal = MakeColor (ConsoleColor.Black, ConsoleColor.Gray);
  183. Colors.Dialog.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Cyan);
  184. Colors.Dialog.HotNormal = MakeColor (ConsoleColor.Blue, ConsoleColor.Gray);
  185. Colors.Dialog.HotFocus = MakeColor (ConsoleColor.Blue, ConsoleColor.Cyan);
  186. Colors.Dialog.Disabled = MakeColor (ConsoleColor.DarkGray, ConsoleColor.Gray);
  187. Colors.Error.Normal = MakeColor (ConsoleColor.White, ConsoleColor.Red);
  188. Colors.Error.Focus = MakeColor (ConsoleColor.Black, ConsoleColor.Gray);
  189. Colors.Error.HotNormal = MakeColor (ConsoleColor.Yellow, ConsoleColor.Red);
  190. Colors.Error.HotFocus = Colors.Error.HotNormal;
  191. Colors.Error.Disabled = MakeColor (ConsoleColor.DarkGray, ConsoleColor.White);
  192. //MockConsole.Clear ();
  193. }
  194. public override Attribute MakeAttribute (Color fore, Color back)
  195. {
  196. return MakeColor ((ConsoleColor)fore, (ConsoleColor)back);
  197. }
  198. int redrawColor = -1;
  199. void SetColor (int color)
  200. {
  201. redrawColor = color;
  202. IEnumerable<int> values = Enum.GetValues (typeof (ConsoleColor))
  203. .OfType<ConsoleColor> ()
  204. .Select (s => (int)s);
  205. if (values.Contains (color & 0xffff)) {
  206. FakeConsole.BackgroundColor = (ConsoleColor)(color & 0xffff);
  207. }
  208. if (values.Contains ((color >> 16) & 0xffff)) {
  209. FakeConsole.ForegroundColor = (ConsoleColor)((color >> 16) & 0xffff);
  210. }
  211. }
  212. public override void UpdateScreen ()
  213. {
  214. int top = Top;
  215. int left = Left;
  216. int rows = Math.Min (FakeConsole.WindowHeight + top, Rows);
  217. int cols = Cols;
  218. var savedRow = FakeConsole.CursorTop;
  219. var savedCol = FakeConsole.CursorLeft;
  220. var savedCursorVisible = FakeConsole.CursorVisible;
  221. for (int row = top; row < rows; row++) {
  222. if (!dirtyLine [row])
  223. continue;
  224. dirtyLine [row] = false;
  225. for (int col = left; col < cols; col++) {
  226. FakeConsole.CursorTop = row;
  227. FakeConsole.CursorLeft = col;
  228. for (; col < cols; col++) {
  229. if (contents [row, col, 2] == 0) {
  230. FakeConsole.CursorLeft++;
  231. continue;
  232. }
  233. var color = contents [row, col, 1];
  234. if (color != redrawColor)
  235. SetColor (color);
  236. FakeConsole.Write ((char)contents [row, col, 0]);
  237. contents [row, col, 2] = 0;
  238. }
  239. }
  240. }
  241. FakeConsole.CursorTop = savedRow;
  242. FakeConsole.CursorLeft = savedCol;
  243. FakeConsole.CursorVisible = savedCursorVisible;
  244. }
  245. public override void Refresh ()
  246. {
  247. UpdateScreen ();
  248. UpdateCursor ();
  249. }
  250. Attribute currentAttribute;
  251. public override void SetAttribute (Attribute c)
  252. {
  253. currentAttribute = c;
  254. }
  255. Key MapKey (ConsoleKeyInfo keyInfo)
  256. {
  257. switch (keyInfo.Key) {
  258. case ConsoleKey.Escape:
  259. return MapKeyModifiers (keyInfo, Key.Esc);
  260. case ConsoleKey.Tab:
  261. return keyInfo.Modifiers == ConsoleModifiers.Shift ? Key.BackTab : Key.Tab;
  262. case ConsoleKey.Home:
  263. return MapKeyModifiers (keyInfo, Key.Home);
  264. case ConsoleKey.End:
  265. return MapKeyModifiers (keyInfo, Key.End);
  266. case ConsoleKey.LeftArrow:
  267. return MapKeyModifiers (keyInfo, Key.CursorLeft);
  268. case ConsoleKey.RightArrow:
  269. return MapKeyModifiers (keyInfo, Key.CursorRight);
  270. case ConsoleKey.UpArrow:
  271. return MapKeyModifiers (keyInfo, Key.CursorUp);
  272. case ConsoleKey.DownArrow:
  273. return MapKeyModifiers (keyInfo, Key.CursorDown);
  274. case ConsoleKey.PageUp:
  275. return MapKeyModifiers (keyInfo, Key.PageUp);
  276. case ConsoleKey.PageDown:
  277. return MapKeyModifiers (keyInfo, Key.PageDown);
  278. case ConsoleKey.Enter:
  279. return MapKeyModifiers (keyInfo, Key.Enter);
  280. case ConsoleKey.Spacebar:
  281. return MapKeyModifiers (keyInfo, keyInfo.KeyChar == 0 ? Key.Space : (Key)keyInfo.KeyChar);
  282. case ConsoleKey.Backspace:
  283. return MapKeyModifiers (keyInfo, Key.Backspace);
  284. case ConsoleKey.Delete:
  285. return MapKeyModifiers (keyInfo, Key.DeleteChar);
  286. case ConsoleKey.Insert:
  287. return MapKeyModifiers (keyInfo, Key.InsertChar);
  288. case ConsoleKey.Oem1:
  289. case ConsoleKey.Oem2:
  290. case ConsoleKey.Oem3:
  291. case ConsoleKey.Oem4:
  292. case ConsoleKey.Oem5:
  293. case ConsoleKey.Oem6:
  294. case ConsoleKey.Oem7:
  295. case ConsoleKey.Oem8:
  296. case ConsoleKey.Oem102:
  297. case ConsoleKey.OemPeriod:
  298. case ConsoleKey.OemComma:
  299. case ConsoleKey.OemPlus:
  300. case ConsoleKey.OemMinus:
  301. if (keyInfo.KeyChar == 0)
  302. return Key.Unknown;
  303. return (Key)((uint)keyInfo.KeyChar);
  304. }
  305. var key = keyInfo.Key;
  306. if (key >= ConsoleKey.A && key <= ConsoleKey.Z) {
  307. var delta = key - ConsoleKey.A;
  308. if (keyInfo.Modifiers == ConsoleModifiers.Control) {
  309. return (Key)(((uint)Key.CtrlMask) | ((uint)Key.A + delta));
  310. }
  311. if (keyInfo.Modifiers == ConsoleModifiers.Alt) {
  312. return (Key)(((uint)Key.AltMask) | ((uint)Key.A + delta));
  313. }
  314. if ((keyInfo.Modifiers & (ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
  315. if (keyInfo.KeyChar == 0) {
  316. return (Key)(((uint)Key.AltMask | (uint)Key.CtrlMask) | ((uint)Key.A + delta));
  317. } else {
  318. return (Key)((uint)keyInfo.KeyChar);
  319. }
  320. }
  321. return (Key)((uint)keyInfo.KeyChar);
  322. }
  323. if (key >= ConsoleKey.D0 && key <= ConsoleKey.D9) {
  324. var delta = key - ConsoleKey.D0;
  325. if (keyInfo.Modifiers == ConsoleModifiers.Alt) {
  326. return (Key)(((uint)Key.AltMask) | ((uint)Key.D0 + delta));
  327. }
  328. if (keyInfo.Modifiers == ConsoleModifiers.Control) {
  329. return (Key)(((uint)Key.CtrlMask) | ((uint)Key.D0 + delta));
  330. }
  331. if (keyInfo.KeyChar == 0 || keyInfo.KeyChar == 30) {
  332. return MapKeyModifiers (keyInfo, (Key)((uint)Key.D0 + delta));
  333. }
  334. return (Key)((uint)keyInfo.KeyChar);
  335. }
  336. if (key >= ConsoleKey.F1 && key <= ConsoleKey.F12) {
  337. var delta = key - ConsoleKey.F1;
  338. if ((keyInfo.Modifiers & (ConsoleModifiers.Shift | ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
  339. return MapKeyModifiers (keyInfo, (Key)((uint)Key.F1 + delta));
  340. }
  341. return (Key)((uint)Key.F1 + delta);
  342. }
  343. if (keyInfo.KeyChar != 0) {
  344. return MapKeyModifiers (keyInfo, (Key)((uint)keyInfo.KeyChar));
  345. }
  346. return (Key)(0xffffffff);
  347. }
  348. KeyModifiers keyModifiers;
  349. private Key MapKeyModifiers (ConsoleKeyInfo keyInfo, Key key)
  350. {
  351. Key keyMod = new Key ();
  352. if ((keyInfo.Modifiers & ConsoleModifiers.Shift) != 0)
  353. keyMod = Key.ShiftMask;
  354. if ((keyInfo.Modifiers & ConsoleModifiers.Control) != 0)
  355. keyMod |= Key.CtrlMask;
  356. if ((keyInfo.Modifiers & ConsoleModifiers.Alt) != 0)
  357. keyMod |= Key.AltMask;
  358. return keyMod != Key.Null ? keyMod | key : key;
  359. }
  360. Action<KeyEvent> keyHandler;
  361. Action<KeyEvent> keyUpHandler;
  362. private CursorVisibility savedCursorVisibility;
  363. public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler)
  364. {
  365. this.keyHandler = keyHandler;
  366. this.keyUpHandler = keyUpHandler;
  367. // Note: Net doesn't support keydown/up events and thus any passed keyDown/UpHandlers will never be called
  368. (mainLoop.Driver as FakeMainLoop).KeyPressed += (consoleKey) => ProcessInput (consoleKey);
  369. }
  370. void ProcessInput (ConsoleKeyInfo consoleKey)
  371. {
  372. keyModifiers = new KeyModifiers ();
  373. var map = MapKey (consoleKey);
  374. if (map == (Key)0xffffffff)
  375. return;
  376. if (consoleKey.Modifiers.HasFlag (ConsoleModifiers.Alt)) {
  377. keyModifiers.Alt = true;
  378. }
  379. if (consoleKey.Modifiers.HasFlag (ConsoleModifiers.Shift)) {
  380. keyModifiers.Shift = true;
  381. }
  382. if (consoleKey.Modifiers.HasFlag (ConsoleModifiers.Control)) {
  383. keyModifiers.Ctrl = true;
  384. }
  385. keyHandler (new KeyEvent (map, keyModifiers));
  386. keyUpHandler (new KeyEvent (map, keyModifiers));
  387. }
  388. public override Attribute GetAttribute ()
  389. {
  390. return currentAttribute;
  391. }
  392. /// <inheritdoc/>
  393. public override bool GetCursorVisibility (out CursorVisibility visibility)
  394. {
  395. visibility = FakeConsole.CursorVisible
  396. ? CursorVisibility.Default
  397. : CursorVisibility.Invisible;
  398. return FakeConsole.CursorVisible;
  399. }
  400. /// <inheritdoc/>
  401. public override bool SetCursorVisibility (CursorVisibility visibility)
  402. {
  403. savedCursorVisibility = visibility;
  404. return FakeConsole.CursorVisible = visibility == CursorVisibility.Default;
  405. }
  406. /// <inheritdoc/>
  407. public override bool EnsureCursorVisibility ()
  408. {
  409. if (!(ccol >= 0 && crow >= 0 && ccol < Cols && crow < Rows)) {
  410. GetCursorVisibility (out CursorVisibility cursorVisibility);
  411. savedCursorVisibility = cursorVisibility;
  412. SetCursorVisibility (CursorVisibility.Invisible);
  413. return false;
  414. }
  415. SetCursorVisibility (savedCursorVisibility);
  416. return FakeConsole.CursorVisible;
  417. }
  418. public override void SendKeys (char keyChar, ConsoleKey key, bool shift, bool alt, bool control)
  419. {
  420. ProcessInput (new ConsoleKeyInfo (keyChar, key, shift, alt, control));
  421. }
  422. public void SetBufferSize (int width, int height)
  423. {
  424. FakeConsole.SetBufferSize (width, height);
  425. cols = width;
  426. rows = height;
  427. if (!HeightAsBuffer) {
  428. SetWindowSize (width, height);
  429. }
  430. ProcessResize ();
  431. }
  432. public void SetWindowSize (int width, int height)
  433. {
  434. FakeConsole.SetWindowSize (width, height);
  435. if (!HeightAsBuffer) {
  436. if (width != cols || height != rows) {
  437. SetBufferSize (width, height);
  438. cols = width;
  439. rows = height;
  440. }
  441. }
  442. ProcessResize ();
  443. }
  444. public void SetWindowPosition (int left, int top)
  445. {
  446. if (HeightAsBuffer) {
  447. this.left = Math.Max (Math.Min (left, Cols - FakeConsole.WindowWidth), 0);
  448. this.top = Math.Max (Math.Min (top, Rows - FakeConsole.WindowHeight), 0);
  449. } else if (this.left > 0 || this.top > 0) {
  450. this.left = 0;
  451. this.top = 0;
  452. }
  453. FakeConsole.SetWindowPosition (this.left, this.top);
  454. }
  455. void ProcessResize ()
  456. {
  457. ResizeScreen ();
  458. UpdateOffScreen ();
  459. TerminalResized?.Invoke ();
  460. }
  461. void ResizeScreen ()
  462. {
  463. if (!HeightAsBuffer) {
  464. if (FakeConsole.WindowHeight > 0) {
  465. // Can raise an exception while is still resizing.
  466. try {
  467. #pragma warning disable CA1416
  468. FakeConsole.CursorTop = 0;
  469. FakeConsole.CursorLeft = 0;
  470. FakeConsole.WindowTop = 0;
  471. FakeConsole.WindowLeft = 0;
  472. #pragma warning restore CA1416
  473. } catch (System.IO.IOException) {
  474. return;
  475. } catch (ArgumentOutOfRangeException) {
  476. return;
  477. }
  478. }
  479. } else {
  480. try {
  481. #pragma warning disable CA1416
  482. FakeConsole.WindowLeft = Math.Max (Math.Min (left, Cols - FakeConsole.WindowWidth), 0);
  483. FakeConsole.WindowTop = Math.Max (Math.Min (top, Rows - FakeConsole.WindowHeight), 0);
  484. #pragma warning restore CA1416
  485. } catch (Exception) {
  486. return;
  487. }
  488. }
  489. Clip = new Rect (0, 0, Cols, Rows);
  490. }
  491. public override void UpdateOffScreen ()
  492. {
  493. contents = new int [Rows, Cols, 3];
  494. dirtyLine = new bool [Rows];
  495. // Can raise an exception while is still resizing.
  496. try {
  497. for (int row = 0; row < rows; row++) {
  498. for (int c = 0; c < cols; c++) {
  499. contents [row, c, 0] = ' ';
  500. contents [row, c, 1] = (ushort)Colors.TopLevel.Normal;
  501. contents [row, c, 2] = 0;
  502. dirtyLine [row] = true;
  503. }
  504. }
  505. } catch (IndexOutOfRangeException) { }
  506. }
  507. public override bool GetColors (int value, out Color foreground, out Color background)
  508. {
  509. bool hasColor = false;
  510. foreground = default;
  511. background = default;
  512. IEnumerable<int> values = Enum.GetValues (typeof (ConsoleColor))
  513. .OfType<ConsoleColor> ()
  514. .Select (s => (int)s);
  515. if (values.Contains (value & 0xffff)) {
  516. hasColor = true;
  517. background = (Color)(ConsoleColor)(value & 0xffff);
  518. }
  519. if (values.Contains ((value >> 16) & 0xffff)) {
  520. hasColor = true;
  521. foreground = (Color)(ConsoleColor)((value >> 16) & 0xffff);
  522. }
  523. return hasColor;
  524. }
  525. #region Unused
  526. public override void UpdateCursor ()
  527. {
  528. if (!EnsureCursorVisibility ())
  529. return;
  530. // Prevents the exception of size changing during resizing.
  531. try {
  532. if (ccol >= 0 && ccol < FakeConsole.BufferWidth && crow >= 0 && crow < FakeConsole.BufferHeight) {
  533. FakeConsole.SetCursorPosition (ccol, crow);
  534. }
  535. } catch (System.IO.IOException) {
  536. } catch (ArgumentOutOfRangeException) {
  537. }
  538. }
  539. public override void StartReportingMouseMoves ()
  540. {
  541. }
  542. public override void StopReportingMouseMoves ()
  543. {
  544. }
  545. public override void Suspend ()
  546. {
  547. }
  548. public override void SetColors (ConsoleColor foreground, ConsoleColor background)
  549. {
  550. }
  551. public override void SetColors (short foregroundColorId, short backgroundColorId)
  552. {
  553. throw new NotImplementedException ();
  554. }
  555. public override void CookMouse ()
  556. {
  557. }
  558. public override void UncookMouse ()
  559. {
  560. }
  561. #endregion
  562. #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
  563. }
  564. }