FakeDriver.cs 20 KB

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