FakeDriver.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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 ((Rune)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 ((Rune)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. public override Attribute MakeColor (Color foreground, Color background)
  140. {
  141. return MakeColor ((ConsoleColor)foreground, (ConsoleColor)background);
  142. }
  143. static Attribute MakeColor (ConsoleColor f, ConsoleColor b)
  144. {
  145. // Encode the colors into the int value.
  146. return new Attribute (
  147. value: ((((int)f) & 0xffff) << 16) | (((int)b) & 0xffff),
  148. foreground: (Color)f,
  149. background: (Color)b
  150. );
  151. }
  152. public override void Init (Action terminalResized)
  153. {
  154. TerminalResized = terminalResized;
  155. cols = FakeConsole.WindowWidth = FakeConsole.BufferWidth = FakeConsole.WIDTH;
  156. rows = FakeConsole.WindowHeight = FakeConsole.BufferHeight = FakeConsole.HEIGHT;
  157. FakeConsole.Clear ();
  158. ResizeScreen ();
  159. UpdateOffScreen ();
  160. CreateColors ();
  161. //MockConsole.Clear ();
  162. }
  163. public override Attribute MakeAttribute (Color fore, Color back)
  164. {
  165. return MakeColor ((ConsoleColor)fore, (ConsoleColor)back);
  166. }
  167. int redrawColor = -1;
  168. void SetColor (int color)
  169. {
  170. redrawColor = color;
  171. IEnumerable<int> values = Enum.GetValues (typeof (ConsoleColor))
  172. .OfType<ConsoleColor> ()
  173. .Select (s => (int)s);
  174. if (values.Contains (color & 0xffff)) {
  175. FakeConsole.BackgroundColor = (ConsoleColor)(color & 0xffff);
  176. }
  177. if (values.Contains ((color >> 16) & 0xffff)) {
  178. FakeConsole.ForegroundColor = (ConsoleColor)((color >> 16) & 0xffff);
  179. }
  180. }
  181. public override void UpdateScreen ()
  182. {
  183. int top = Top;
  184. int left = Left;
  185. int rows = Math.Min (FakeConsole.WindowHeight + top, Rows);
  186. int cols = Cols;
  187. var savedRow = FakeConsole.CursorTop;
  188. var savedCol = FakeConsole.CursorLeft;
  189. var savedCursorVisible = FakeConsole.CursorVisible;
  190. for (int row = top; row < rows; row++) {
  191. if (!dirtyLine [row])
  192. continue;
  193. dirtyLine [row] = false;
  194. for (int col = left; col < cols; col++) {
  195. FakeConsole.CursorTop = row;
  196. FakeConsole.CursorLeft = col;
  197. for (; col < cols; col++) {
  198. if (contents [row, col, 2] == 0) {
  199. FakeConsole.CursorLeft++;
  200. continue;
  201. }
  202. var color = contents [row, col, 1];
  203. if (color != redrawColor)
  204. SetColor (color);
  205. Rune rune = contents [row, col, 0];
  206. if (Rune.DecodeSurrogatePair (rune, out char [] spair)) {
  207. FakeConsole.Write (spair);
  208. } else {
  209. FakeConsole.Write ((char)rune);
  210. }
  211. contents [row, col, 2] = 0;
  212. }
  213. }
  214. }
  215. FakeConsole.CursorTop = savedRow;
  216. FakeConsole.CursorLeft = savedCol;
  217. FakeConsole.CursorVisible = savedCursorVisible;
  218. }
  219. public override void Refresh ()
  220. {
  221. UpdateScreen ();
  222. UpdateCursor ();
  223. }
  224. Attribute currentAttribute;
  225. public override void SetAttribute (Attribute c)
  226. {
  227. currentAttribute = c;
  228. }
  229. public ConsoleKeyInfo FromVKPacketToKConsoleKeyInfo (ConsoleKeyInfo consoleKeyInfo)
  230. {
  231. if (consoleKeyInfo.Key != ConsoleKey.Packet) {
  232. return consoleKeyInfo;
  233. }
  234. var mod = consoleKeyInfo.Modifiers;
  235. var shift = (mod & ConsoleModifiers.Shift) != 0;
  236. var alt = (mod & ConsoleModifiers.Alt) != 0;
  237. var control = (mod & ConsoleModifiers.Control) != 0;
  238. var keyChar = ConsoleKeyMapping.GetKeyCharFromConsoleKey (consoleKeyInfo.KeyChar, consoleKeyInfo.Modifiers, out uint virtualKey, out _);
  239. return new ConsoleKeyInfo ((char)keyChar, (ConsoleKey)virtualKey, shift, alt, control);
  240. }
  241. Key MapKey (ConsoleKeyInfo keyInfo)
  242. {
  243. switch (keyInfo.Key) {
  244. case ConsoleKey.Escape:
  245. return MapKeyModifiers (keyInfo, Key.Esc);
  246. case ConsoleKey.Tab:
  247. return keyInfo.Modifiers == ConsoleModifiers.Shift ? Key.BackTab : Key.Tab;
  248. case ConsoleKey.Clear:
  249. return MapKeyModifiers (keyInfo, Key.Clear);
  250. case ConsoleKey.Home:
  251. return MapKeyModifiers (keyInfo, Key.Home);
  252. case ConsoleKey.End:
  253. return MapKeyModifiers (keyInfo, Key.End);
  254. case ConsoleKey.LeftArrow:
  255. return MapKeyModifiers (keyInfo, Key.CursorLeft);
  256. case ConsoleKey.RightArrow:
  257. return MapKeyModifiers (keyInfo, Key.CursorRight);
  258. case ConsoleKey.UpArrow:
  259. return MapKeyModifiers (keyInfo, Key.CursorUp);
  260. case ConsoleKey.DownArrow:
  261. return MapKeyModifiers (keyInfo, Key.CursorDown);
  262. case ConsoleKey.PageUp:
  263. return MapKeyModifiers (keyInfo, Key.PageUp);
  264. case ConsoleKey.PageDown:
  265. return MapKeyModifiers (keyInfo, Key.PageDown);
  266. case ConsoleKey.Enter:
  267. return MapKeyModifiers (keyInfo, Key.Enter);
  268. case ConsoleKey.Spacebar:
  269. return MapKeyModifiers (keyInfo, keyInfo.KeyChar == 0 ? Key.Space : (Key)keyInfo.KeyChar);
  270. case ConsoleKey.Backspace:
  271. return MapKeyModifiers (keyInfo, Key.Backspace);
  272. case ConsoleKey.Delete:
  273. return MapKeyModifiers (keyInfo, Key.DeleteChar);
  274. case ConsoleKey.Insert:
  275. return MapKeyModifiers (keyInfo, Key.InsertChar);
  276. case ConsoleKey.PrintScreen:
  277. return MapKeyModifiers (keyInfo, Key.PrintScreen);
  278. case ConsoleKey.Oem1:
  279. case ConsoleKey.Oem2:
  280. case ConsoleKey.Oem3:
  281. case ConsoleKey.Oem4:
  282. case ConsoleKey.Oem5:
  283. case ConsoleKey.Oem6:
  284. case ConsoleKey.Oem7:
  285. case ConsoleKey.Oem8:
  286. case ConsoleKey.Oem102:
  287. case ConsoleKey.OemPeriod:
  288. case ConsoleKey.OemComma:
  289. case ConsoleKey.OemPlus:
  290. case ConsoleKey.OemMinus:
  291. if (keyInfo.KeyChar == 0)
  292. return Key.Unknown;
  293. return (Key)((uint)keyInfo.KeyChar);
  294. }
  295. var key = keyInfo.Key;
  296. if (key >= ConsoleKey.A && key <= ConsoleKey.Z) {
  297. var delta = key - ConsoleKey.A;
  298. if (keyInfo.Modifiers == ConsoleModifiers.Control) {
  299. return (Key)(((uint)Key.CtrlMask) | ((uint)Key.A + delta));
  300. }
  301. if (keyInfo.Modifiers == ConsoleModifiers.Alt) {
  302. return (Key)(((uint)Key.AltMask) | ((uint)Key.A + delta));
  303. }
  304. if (keyInfo.Modifiers == (ConsoleModifiers.Shift | ConsoleModifiers.Alt)) {
  305. return MapKeyModifiers (keyInfo, (Key)((uint)Key.A + delta));
  306. }
  307. if ((keyInfo.Modifiers & (ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
  308. if (keyInfo.KeyChar == 0) {
  309. return (Key)(((uint)Key.AltMask | (uint)Key.CtrlMask) | ((uint)Key.A + delta));
  310. } else {
  311. return (Key)((uint)keyInfo.KeyChar);
  312. }
  313. }
  314. return (Key)((uint)keyInfo.KeyChar);
  315. }
  316. if (key >= ConsoleKey.D0 && key <= ConsoleKey.D9) {
  317. var delta = key - ConsoleKey.D0;
  318. if (keyInfo.Modifiers == ConsoleModifiers.Alt) {
  319. return (Key)(((uint)Key.AltMask) | ((uint)Key.D0 + delta));
  320. }
  321. if (keyInfo.Modifiers == ConsoleModifiers.Control) {
  322. return (Key)(((uint)Key.CtrlMask) | ((uint)Key.D0 + delta));
  323. }
  324. if (keyInfo.Modifiers == (ConsoleModifiers.Shift | ConsoleModifiers.Alt)) {
  325. return MapKeyModifiers (keyInfo, (Key)((uint)Key.D0 + delta));
  326. }
  327. if ((keyInfo.Modifiers & (ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
  328. if (keyInfo.KeyChar == 0 || keyInfo.KeyChar == 30) {
  329. return MapKeyModifiers (keyInfo, (Key)((uint)Key.D0 + delta));
  330. }
  331. }
  332. return (Key)((uint)keyInfo.KeyChar);
  333. }
  334. if (key >= ConsoleKey.F1 && key <= ConsoleKey.F12) {
  335. var delta = key - ConsoleKey.F1;
  336. if ((keyInfo.Modifiers & (ConsoleModifiers.Shift | ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
  337. return MapKeyModifiers (keyInfo, (Key)((uint)Key.F1 + delta));
  338. }
  339. return (Key)((uint)Key.F1 + delta);
  340. }
  341. if (keyInfo.KeyChar != 0) {
  342. return MapKeyModifiers (keyInfo, (Key)((uint)keyInfo.KeyChar));
  343. }
  344. return (Key)(0xffffffff);
  345. }
  346. KeyModifiers keyModifiers;
  347. private Key MapKeyModifiers (ConsoleKeyInfo keyInfo, Key key)
  348. {
  349. Key keyMod = new Key ();
  350. if ((keyInfo.Modifiers & ConsoleModifiers.Shift) != 0)
  351. keyMod = Key.ShiftMask;
  352. if ((keyInfo.Modifiers & ConsoleModifiers.Control) != 0)
  353. keyMod |= Key.CtrlMask;
  354. if ((keyInfo.Modifiers & ConsoleModifiers.Alt) != 0)
  355. keyMod |= Key.AltMask;
  356. return keyMod != Key.Null ? keyMod | key : key;
  357. }
  358. Action<KeyEvent> keyDownHandler;
  359. Action<KeyEvent> keyHandler;
  360. Action<KeyEvent> keyUpHandler;
  361. private CursorVisibility savedCursorVisibility;
  362. public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler)
  363. {
  364. this.keyDownHandler = keyDownHandler;
  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. if (consoleKey.Key == ConsoleKey.Packet) {
  373. consoleKey = FromVKPacketToKConsoleKeyInfo (consoleKey);
  374. }
  375. keyModifiers = new KeyModifiers ();
  376. if (consoleKey.Modifiers.HasFlag (ConsoleModifiers.Shift)) {
  377. keyModifiers.Shift = true;
  378. }
  379. if (consoleKey.Modifiers.HasFlag (ConsoleModifiers.Alt)) {
  380. keyModifiers.Alt = true;
  381. }
  382. if (consoleKey.Modifiers.HasFlag (ConsoleModifiers.Control)) {
  383. keyModifiers.Ctrl = true;
  384. }
  385. var map = MapKey (consoleKey);
  386. if (map == (Key)0xffffffff) {
  387. if ((consoleKey.Modifiers & (ConsoleModifiers.Shift | ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
  388. keyDownHandler (new KeyEvent (map, keyModifiers));
  389. keyUpHandler (new KeyEvent (map, keyModifiers));
  390. }
  391. return;
  392. }
  393. keyDownHandler (new KeyEvent (map, keyModifiers));
  394. keyHandler (new KeyEvent (map, keyModifiers));
  395. keyUpHandler (new KeyEvent (map, keyModifiers));
  396. }
  397. public override Attribute GetAttribute ()
  398. {
  399. return currentAttribute;
  400. }
  401. /// <inheritdoc/>
  402. public override bool GetCursorVisibility (out CursorVisibility visibility)
  403. {
  404. visibility = FakeConsole.CursorVisible
  405. ? CursorVisibility.Default
  406. : CursorVisibility.Invisible;
  407. return FakeConsole.CursorVisible;
  408. }
  409. /// <inheritdoc/>
  410. public override bool SetCursorVisibility (CursorVisibility visibility)
  411. {
  412. savedCursorVisibility = visibility;
  413. return FakeConsole.CursorVisible = visibility == CursorVisibility.Default;
  414. }
  415. /// <inheritdoc/>
  416. public override bool EnsureCursorVisibility ()
  417. {
  418. if (!(ccol >= 0 && crow >= 0 && ccol < Cols && crow < Rows)) {
  419. GetCursorVisibility (out CursorVisibility cursorVisibility);
  420. savedCursorVisibility = cursorVisibility;
  421. SetCursorVisibility (CursorVisibility.Invisible);
  422. return false;
  423. }
  424. SetCursorVisibility (savedCursorVisibility);
  425. return FakeConsole.CursorVisible;
  426. }
  427. public override void SendKeys (char keyChar, ConsoleKey key, bool shift, bool alt, bool control)
  428. {
  429. ProcessInput (new ConsoleKeyInfo (keyChar, key, shift, alt, control));
  430. }
  431. public void SetBufferSize (int width, int height)
  432. {
  433. FakeConsole.SetBufferSize (width, height);
  434. cols = width;
  435. rows = height;
  436. if (!HeightAsBuffer) {
  437. SetWindowSize (width, height);
  438. }
  439. ProcessResize ();
  440. }
  441. public void SetWindowSize (int width, int height)
  442. {
  443. FakeConsole.SetWindowSize (width, height);
  444. if (!HeightAsBuffer) {
  445. if (width != cols || height != rows) {
  446. SetBufferSize (width, height);
  447. cols = width;
  448. rows = height;
  449. }
  450. }
  451. ProcessResize ();
  452. }
  453. public void SetWindowPosition (int left, int top)
  454. {
  455. if (HeightAsBuffer) {
  456. this.left = Math.Max (Math.Min (left, Cols - FakeConsole.WindowWidth), 0);
  457. this.top = Math.Max (Math.Min (top, Rows - FakeConsole.WindowHeight), 0);
  458. } else if (this.left > 0 || this.top > 0) {
  459. this.left = 0;
  460. this.top = 0;
  461. }
  462. FakeConsole.SetWindowPosition (this.left, this.top);
  463. }
  464. void ProcessResize ()
  465. {
  466. ResizeScreen ();
  467. UpdateOffScreen ();
  468. TerminalResized?.Invoke ();
  469. }
  470. public override void ResizeScreen ()
  471. {
  472. if (!HeightAsBuffer) {
  473. if (FakeConsole.WindowHeight > 0) {
  474. // Can raise an exception while is still resizing.
  475. try {
  476. #pragma warning disable CA1416
  477. FakeConsole.CursorTop = 0;
  478. FakeConsole.CursorLeft = 0;
  479. FakeConsole.WindowTop = 0;
  480. FakeConsole.WindowLeft = 0;
  481. #pragma warning restore CA1416
  482. } catch (System.IO.IOException) {
  483. return;
  484. } catch (ArgumentOutOfRangeException) {
  485. return;
  486. }
  487. }
  488. } else {
  489. try {
  490. #pragma warning disable CA1416
  491. FakeConsole.WindowLeft = Math.Max (Math.Min (left, Cols - FakeConsole.WindowWidth), 0);
  492. FakeConsole.WindowTop = Math.Max (Math.Min (top, Rows - FakeConsole.WindowHeight), 0);
  493. #pragma warning restore CA1416
  494. } catch (Exception) {
  495. return;
  496. }
  497. }
  498. Clip = new Rect (0, 0, Cols, Rows);
  499. }
  500. public override void UpdateOffScreen ()
  501. {
  502. contents = new int [Rows, Cols, 3];
  503. dirtyLine = new bool [Rows];
  504. // Can raise an exception while is still resizing.
  505. try {
  506. for (int row = 0; row < rows; row++) {
  507. for (int c = 0; c < cols; c++) {
  508. contents [row, c, 0] = ' ';
  509. contents [row, c, 1] = (ushort)Colors.TopLevel.Normal;
  510. contents [row, c, 2] = 0;
  511. dirtyLine [row] = true;
  512. }
  513. }
  514. } catch (IndexOutOfRangeException) { }
  515. }
  516. public override bool GetColors (int value, out Color foreground, out Color background)
  517. {
  518. bool hasColor = false;
  519. foreground = default;
  520. background = default;
  521. IEnumerable<int> values = Enum.GetValues (typeof (ConsoleColor))
  522. .OfType<ConsoleColor> ()
  523. .Select (s => (int)s);
  524. if (values.Contains (value & 0xffff)) {
  525. hasColor = true;
  526. background = (Color)(ConsoleColor)(value & 0xffff);
  527. }
  528. if (values.Contains ((value >> 16) & 0xffff)) {
  529. hasColor = true;
  530. foreground = (Color)(ConsoleColor)((value >> 16) & 0xffff);
  531. }
  532. return hasColor;
  533. }
  534. #region Unused
  535. public override void UpdateCursor ()
  536. {
  537. if (!EnsureCursorVisibility ())
  538. return;
  539. // Prevents the exception of size changing during resizing.
  540. try {
  541. if (ccol >= 0 && ccol < FakeConsole.BufferWidth && crow >= 0 && crow < FakeConsole.BufferHeight) {
  542. FakeConsole.SetCursorPosition (ccol, crow);
  543. }
  544. } catch (System.IO.IOException) {
  545. } catch (ArgumentOutOfRangeException) {
  546. }
  547. }
  548. public override void StartReportingMouseMoves ()
  549. {
  550. }
  551. public override void StopReportingMouseMoves ()
  552. {
  553. }
  554. public override void Suspend ()
  555. {
  556. }
  557. public override void SetColors (ConsoleColor foreground, ConsoleColor background)
  558. {
  559. }
  560. public override void SetColors (short foregroundColorId, short backgroundColorId)
  561. {
  562. throw new NotImplementedException ();
  563. }
  564. public override void CookMouse ()
  565. {
  566. }
  567. public override void UncookMouse ()
  568. {
  569. }
  570. #endregion
  571. #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
  572. }
  573. }