FakeOutput.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #nullable enable
  2. using System;
  3. namespace Terminal.Gui.Drivers;
  4. /// <summary>
  5. /// Fake console output for testing that captures what would be written to the console.
  6. /// </summary>
  7. public class FakeOutput : OutputBase, IOutput
  8. {
  9. private readonly StringBuilder _output = new ();
  10. private int _cursorLeft;
  11. private int _cursorTop;
  12. private Size _consoleSize = new (80, 25);
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. public FakeOutput ()
  17. {
  18. LastBuffer = new OutputBufferImpl ();
  19. LastBuffer.SetSize (80, 25);
  20. }
  21. /// <summary>
  22. /// Gets or sets the last output buffer written.
  23. /// </summary>
  24. public IOutputBuffer? LastBuffer { get; set; }
  25. /// <summary>
  26. /// Gets the captured output as a string.
  27. /// </summary>
  28. public string Output => _output.ToString ();
  29. /// <inheritdoc />
  30. public Point GetCursorPosition ()
  31. {
  32. return new (_cursorLeft, _cursorTop);
  33. }
  34. /// <inheritdoc/>
  35. public void SetCursorPosition (int col, int row) { SetCursorPositionImpl (col, row); }
  36. /// <inheritdoc />
  37. public void SetSize (int width, int height)
  38. {
  39. _consoleSize = new (width, height);
  40. }
  41. /// <inheritdoc/>
  42. protected override bool SetCursorPositionImpl (int col, int row)
  43. {
  44. _cursorLeft = col;
  45. _cursorTop = row;
  46. return true;
  47. }
  48. /// <inheritdoc/>
  49. public Size GetSize () { return _consoleSize; }
  50. /// <inheritdoc/>
  51. public void Write (ReadOnlySpan<char> text)
  52. {
  53. _output.Append (text);
  54. }
  55. /// <inheritdoc cref="IDriver"/>
  56. public override void Write (IOutputBuffer buffer)
  57. {
  58. LastBuffer = buffer;
  59. base.Write (buffer);
  60. }
  61. /// <inheritdoc cref="IDriver"/>
  62. public override void SetCursorVisibility (CursorVisibility visibility)
  63. {
  64. // Capture but don't act on it in fake output
  65. }
  66. /// <inheritdoc/>
  67. public void Dispose ()
  68. {
  69. // Nothing to dispose
  70. }
  71. /// <inheritdoc/>
  72. protected override void AppendOrWriteAttribute (StringBuilder output, Attribute attr, TextStyle redrawTextStyle)
  73. {
  74. // For testing, we can skip the actual color/style output
  75. // or capture it if needed for verification
  76. }
  77. /// <inheritdoc/>
  78. protected override void Write (StringBuilder output)
  79. {
  80. _output.Append (output);
  81. }
  82. }