FakeDriver.cs 19 KB

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