FakeDriver.cs 19 KB

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