FakeDriver.cs 20 KB

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