FakeConsoleOutput.cs 2.1 KB

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