FakeOutput.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  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 FakeOutput : OutputBase, IOutput
  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. ///
  14. /// </summary>
  15. public FakeOutput ()
  16. {
  17. LastBuffer = new OutputBufferImpl ();
  18. LastBuffer.SetSize (80, 25);
  19. }
  20. /// <summary>
  21. /// Gets or sets the last output buffer written.
  22. /// </summary>
  23. public IOutputBuffer? LastBuffer { get; set; }
  24. /// <summary>
  25. /// Gets the captured output as a string.
  26. /// </summary>
  27. public string Output => _output.ToString ();
  28. /// <inheritdoc />
  29. public Point GetCursorPosition ()
  30. {
  31. return new (_cursorLeft, _cursorTop);
  32. }
  33. /// <inheritdoc/>
  34. public void SetCursorPosition (int col, int row) { SetCursorPositionImpl (col, row); }
  35. /// <inheritdoc />
  36. public void SetSize (int width, int height)
  37. {
  38. _consoleSize = new (width, height);
  39. }
  40. /// <inheritdoc/>
  41. protected override bool SetCursorPositionImpl (int col, int row)
  42. {
  43. _cursorLeft = col;
  44. _cursorTop = row;
  45. return true;
  46. }
  47. /// <inheritdoc/>
  48. public Size GetSize () { return _consoleSize; }
  49. /// <inheritdoc/>
  50. public void Write (ReadOnlySpan<char> text)
  51. {
  52. _output.Append (text);
  53. }
  54. /// <inheritdoc cref="IDriver"/>
  55. public override void Write (IOutputBuffer buffer)
  56. {
  57. LastBuffer = buffer;
  58. base.Write (buffer);
  59. }
  60. /// <inheritdoc cref="IDriver"/>
  61. public override void SetCursorVisibility (CursorVisibility visibility)
  62. {
  63. // Capture but don't act on it in fake output
  64. }
  65. /// <inheritdoc/>
  66. public void Dispose ()
  67. {
  68. // Nothing to dispose
  69. }
  70. /// <inheritdoc/>
  71. protected override void AppendOrWriteAttribute (StringBuilder output, Attribute attr, TextStyle redrawTextStyle)
  72. {
  73. if (Terminal.Gui.Drivers.Driver.Force16Colors)
  74. {
  75. output.Append (EscSeqUtils.CSI_SetForegroundColor (attr.Foreground.GetAnsiColorCode ()));
  76. output.Append (EscSeqUtils.CSI_SetBackgroundColor (attr.Background.GetAnsiColorCode ()));
  77. }
  78. else
  79. {
  80. EscSeqUtils.CSI_AppendForegroundColorRGB (
  81. output,
  82. attr.Foreground.R,
  83. attr.Foreground.G,
  84. attr.Foreground.B
  85. );
  86. EscSeqUtils.CSI_AppendBackgroundColorRGB (
  87. output,
  88. attr.Background.R,
  89. attr.Background.G,
  90. attr.Background.B
  91. );
  92. }
  93. EscSeqUtils.CSI_AppendTextStyleChange (output, redrawTextStyle, attr.Style);
  94. }
  95. /// <inheritdoc/>
  96. protected override void Write (StringBuilder output)
  97. {
  98. _output.Append (output);
  99. }
  100. }