FakeDriver.cs 20 KB

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