FakeDriver.cs 21 KB

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