FakeDriver.cs 21 KB

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