FakeConsoleDriver.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #nullable enable
  2. using System.Collections.Concurrent;
  3. using System.Drawing;
  4. using TerminalGuiFluentTesting;
  5. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  6. namespace Terminal.Gui.Drivers;
  7. /// <summary>
  8. /// Implementation of <see cref="IConsoleDriver"/> that uses fake input/output.
  9. /// This is a lightweight alternative to <see cref="GuiTestContext"/> (if you don't
  10. /// need the entire application main loop running).
  11. /// </summary>
  12. internal class FakeConsoleDriver : ConsoleDriverFacade<ConsoleKeyInfo>, IFakeConsoleDriver
  13. {
  14. internal FakeConsoleDriver (
  15. ConcurrentQueue<ConsoleKeyInfo> inputBuffer,
  16. OutputBuffer outputBuffer,
  17. FakeOutput fakeOutput,
  18. Func<DateTime> datetimeFunc,
  19. FakeSizeMonitor sizeMonitor
  20. ) :
  21. base (
  22. new NetInputProcessor (inputBuffer),
  23. outputBuffer,
  24. fakeOutput,
  25. new (new AnsiResponseParser (), datetimeFunc),
  26. sizeMonitor)
  27. {
  28. FakeOutput fakeOutput1;
  29. InputBuffer = inputBuffer;
  30. SizeMonitor = sizeMonitor;
  31. OutputBuffer = outputBuffer;
  32. ConsoleOutput = fakeOutput1 = fakeOutput;
  33. SizeChanged += (_, e) =>
  34. {
  35. if (e.Size != null)
  36. {
  37. Size s = e.Size.Value;
  38. fakeOutput1.Size = s;
  39. OutputBuffer.SetWindowSize (s.Width, s.Height);
  40. }
  41. };
  42. }
  43. public void SetBufferSize (int width, int height)
  44. {
  45. SizeMonitor.RaiseSizeChanging (new (width, height));
  46. OutputBuffer.SetWindowSize (width, height);
  47. }
  48. /// <summary>
  49. /// Sets the screen size for testing purposes.
  50. /// </summary>
  51. /// <param name="width">The new width (columns).</param>
  52. /// <param name="height">The new height (rows).</param>
  53. public override void SetScreenSize (int width, int height)
  54. {
  55. SetBufferSize (width, height);
  56. }
  57. public IConsoleOutput ConsoleOutput { get; }
  58. public ConcurrentQueue<ConsoleKeyInfo> InputBuffer { get; }
  59. public new OutputBuffer OutputBuffer { get; }
  60. public FakeSizeMonitor SizeMonitor { get; }
  61. }