FakeConsoleOutput.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #nullable enable
  2. using System;
  3. using System.Text;
  4. namespace Terminal.Gui.Drivers;
  5. /// <summary>
  6. /// Fake console output for testing that captures what would be written to the console.
  7. /// </summary>
  8. public class FakeConsoleOutput : OutputBase, IConsoleOutput
  9. {
  10. private readonly StringBuilder _output = new ();
  11. private int _cursorLeft;
  12. private int _cursorTop;
  13. private Size _windowSize = new (80, 25);
  14. /// <summary>
  15. /// Gets the captured output as a string.
  16. /// </summary>
  17. public string Output => _output.ToString ();
  18. /// <summary>
  19. /// Clears the captured output.
  20. /// </summary>
  21. public void ClearOutput () => _output.Clear ();
  22. /// <inheritdoc/>
  23. public void SetCursorPosition (int col, int row)
  24. {
  25. SetCursorPositionImpl (col, row);
  26. }
  27. /// <inheritdoc/>
  28. protected override bool SetCursorPositionImpl (int col, int row)
  29. {
  30. _cursorLeft = col;
  31. _cursorTop = row;
  32. return true;
  33. }
  34. /// <summary>
  35. /// Sets the fake window size.
  36. /// </summary>
  37. public void SetWindowSize (int width, int height)
  38. {
  39. _windowSize = new Size (width, height);
  40. }
  41. /// <summary>
  42. /// Gets the current cursor position.
  43. /// </summary>
  44. public (int left, int top) GetCursorPosition () => (_cursorLeft, _cursorTop);
  45. /// <inheritdoc/>
  46. public Size GetWindowSize () => _windowSize;
  47. /// <inheritdoc/>
  48. public void Write (ReadOnlySpan<char> text)
  49. {
  50. _output.Append (text);
  51. }
  52. /// <inheritdoc/>
  53. public override void SetCursorVisibility (CursorVisibility visibility)
  54. {
  55. // Capture but don't act on it in fake output
  56. }
  57. /// <inheritdoc/>
  58. public void Dispose ()
  59. {
  60. // Nothing to dispose
  61. }
  62. /// <inheritdoc/>
  63. protected override void AppendOrWriteAttribute (StringBuilder output, Attribute attr, TextStyle redrawTextStyle)
  64. {
  65. // For testing, we can skip the actual color/style output
  66. // or capture it if needed for verification
  67. }
  68. /// <inheritdoc/>
  69. protected override void Write (StringBuilder output)
  70. {
  71. _output.Append (output);
  72. }
  73. }