FakeDriver.cs 19 KB

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