FakeDriver.cs 21 KB

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