FakeOutput.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 _outputStringBuilder = new ();
  9. private int _cursorLeft;
  10. private int _cursorTop;
  11. private Size _consoleSize = new (80, 25);
  12. private IOutputBuffer? _lastBuffer;
  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. The <see cref="IOutputBuffer.Contents"/> contains
  23. /// a reference to the buffer last written with <see cref="Write(IOutputBuffer)"/>.
  24. /// </summary>
  25. public IOutputBuffer? GetLastBuffer () => _lastBuffer;
  26. ///// <inheritdoc cref="IOutput.GetLastOutput"/>
  27. //public override string GetLastOutput () => _outputStringBuilder.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. // _outputStringBuilder.Append (text);
  53. }
  54. /// <inheritdoc cref="IOutput.Write(IOutputBuffer)"/>
  55. public override void Write (IOutputBuffer buffer)
  56. {
  57. _lastBuffer = buffer;
  58. base.Write (buffer);
  59. }
  60. ///// <inheritdoc/>
  61. //protected override void Write (StringBuilder output)
  62. //{
  63. // _outputStringBuilder.Append (output);
  64. //}
  65. /// <inheritdoc cref="IDriver"/>
  66. public override void SetCursorVisibility (CursorVisibility visibility)
  67. {
  68. // Capture but don't act on it in fake output
  69. }
  70. /// <inheritdoc/>
  71. protected override void AppendOrWriteAttribute (StringBuilder output, Attribute attr, TextStyle redrawTextStyle)
  72. {
  73. if (Force16Colors)
  74. {
  75. if (!IsLegacyConsole)
  76. {
  77. output.Append (EscSeqUtils.CSI_SetForegroundColor (attr.Foreground.GetAnsiColorCode ()));
  78. output.Append (EscSeqUtils.CSI_SetBackgroundColor (attr.Background.GetAnsiColorCode ()));
  79. EscSeqUtils.CSI_AppendTextStyleChange (output, redrawTextStyle, attr.Style);
  80. }
  81. else
  82. {
  83. Write (output);
  84. Console.ForegroundColor = (ConsoleColor)attr.Foreground.GetClosestNamedColor16 ();
  85. Console.BackgroundColor = (ConsoleColor)attr.Background.GetClosestNamedColor16 ();
  86. }
  87. }
  88. else
  89. {
  90. EscSeqUtils.CSI_AppendForegroundColorRGB (
  91. output,
  92. attr.Foreground.R,
  93. attr.Foreground.G,
  94. attr.Foreground.B
  95. );
  96. EscSeqUtils.CSI_AppendBackgroundColorRGB (
  97. output,
  98. attr.Background.R,
  99. attr.Background.G,
  100. attr.Background.B
  101. );
  102. EscSeqUtils.CSI_AppendTextStyleChange (output, redrawTextStyle, attr.Style);
  103. }
  104. }
  105. /// <inheritdoc/>
  106. public void Dispose ()
  107. {
  108. // Nothing to dispose
  109. }
  110. }