OutputBase.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #nullable enable
  2. namespace Terminal.Gui.Drivers;
  3. /// <summary>
  4. /// Abstract base class to assist with implementing <see cref="IConsoleOutput"/>.
  5. /// </summary>
  6. public abstract class OutputBase
  7. {
  8. private CursorVisibility? _cachedCursorVisibility;
  9. // Last text style used, for updating style with EscSeqUtils.CSI_AppendTextStyleChange().
  10. private TextStyle _redrawTextStyle = TextStyle.None;
  11. /// <summary>
  12. /// Changes the visibility of the cursor in the terminal to the specified <paramref name="visibility"/> e.g.
  13. /// the flashing indicator, invisible, box indicator etc.
  14. /// </summary>
  15. /// <param name="visibility"></param>
  16. public abstract void SetCursorVisibility (CursorVisibility visibility);
  17. /// <inheritdoc cref="IConsoleOutput.Write(IOutputBuffer)"/>
  18. public virtual void Write (IOutputBuffer buffer)
  19. {
  20. if (ConsoleDriver.RunningUnitTests)
  21. {
  22. return;
  23. }
  24. var top = 0;
  25. var left = 0;
  26. int rows = buffer.Rows;
  27. int cols = buffer.Cols;
  28. var output = new StringBuilder ();
  29. Attribute? redrawAttr = null;
  30. int lastCol = -1;
  31. CursorVisibility? savedVisibility = _cachedCursorVisibility;
  32. SetCursorVisibility (CursorVisibility.Invisible);
  33. const int MAX_CHARS_PER_RUNE = 2;
  34. Span<char> runeBuffer = stackalloc char [MAX_CHARS_PER_RUNE];
  35. for (int row = top; row < rows; row++)
  36. {
  37. if (!SetCursorPositionImpl (0, row))
  38. {
  39. return;
  40. }
  41. output.Clear ();
  42. for (int col = left; col < cols; col++)
  43. {
  44. lastCol = -1;
  45. var outputWidth = 0;
  46. for (; col < cols; col++)
  47. {
  48. if (!buffer.Contents! [row, col].IsDirty)
  49. {
  50. if (output.Length > 0)
  51. {
  52. WriteToConsole (output, ref lastCol, row, ref outputWidth);
  53. }
  54. else if (lastCol == -1)
  55. {
  56. lastCol = col;
  57. }
  58. if (lastCol + 1 < cols)
  59. {
  60. lastCol++;
  61. }
  62. continue;
  63. }
  64. if (lastCol == -1)
  65. {
  66. lastCol = col;
  67. }
  68. Attribute? attribute = buffer.Contents [row, col].Attribute;
  69. if (attribute is { })
  70. {
  71. Attribute attr = attribute.Value;
  72. // Performance: Only send the escape sequence if the attribute has changed.
  73. if (attr != redrawAttr)
  74. {
  75. redrawAttr = attr;
  76. AppendOrWriteAttribute (output, attr, _redrawTextStyle);
  77. _redrawTextStyle = attr.Style;
  78. }
  79. }
  80. outputWidth++;
  81. // Avoid Rune.ToString() by appending the rune chars.
  82. Rune rune = buffer.Contents [row, col].Rune;
  83. int runeCharsWritten = rune.EncodeToUtf16 (runeBuffer);
  84. ReadOnlySpan<char> runeChars = runeBuffer [..runeCharsWritten];
  85. output.Append (runeChars);
  86. if (buffer.Contents [row, col].CombiningMarks.Count > 0)
  87. {
  88. // AtlasEngine does not support NON-NORMALIZED combining marks in a way
  89. // compatible with the driver architecture. Any CMs (except in the first col)
  90. // are correctly combined with the base char, but are ALSO treated as 1 column
  91. // width codepoints E.g. `echo "[e`u{0301}`u{0301}]"` will output `[é ]`.
  92. //
  93. // For now, we just ignore the list of CMs.
  94. //foreach (var combMark in Contents [row, col].CombiningMarks) {
  95. // output.Append (combMark);
  96. }
  97. else if (rune.IsSurrogatePair () && rune.GetColumns () < 2)
  98. {
  99. WriteToConsole (output, ref lastCol, row, ref outputWidth);
  100. SetCursorPositionImpl (col - 1, row);
  101. }
  102. buffer.Contents [row, col].IsDirty = false;
  103. }
  104. }
  105. if (output.Length > 0)
  106. {
  107. SetCursorPositionImpl (lastCol, row);
  108. // Wrap URLs with OSC 8 hyperlink sequences using the new Osc8UrlLinker
  109. StringBuilder processed = Osc8UrlLinker.WrapOsc8 (output);
  110. Write (processed);
  111. }
  112. }
  113. foreach (SixelToRender s in Application.Sixel)
  114. {
  115. if (!string.IsNullOrWhiteSpace (s.SixelData))
  116. {
  117. SetCursorPositionImpl (s.ScreenPosition.X, s.ScreenPosition.Y);
  118. Console.Out.Write (s.SixelData);
  119. }
  120. }
  121. SetCursorVisibility (savedVisibility ?? CursorVisibility.Default);
  122. _cachedCursorVisibility = savedVisibility;
  123. }
  124. /// <summary>
  125. /// Changes the color and text style of the console to the given <paramref name="attr"/> and
  126. /// <paramref name="redrawTextStyle"/>.
  127. /// If command can be buffered in line with other output (e.g. CSI sequence) then it should be appended to
  128. /// <paramref name="output"/>
  129. /// otherwise the relevant output state should be flushed directly (e.g. by calling relevant win 32 API method)
  130. /// </summary>
  131. /// <param name="output"></param>
  132. /// <param name="attr"></param>
  133. /// <param name="redrawTextStyle"></param>
  134. protected abstract void AppendOrWriteAttribute (StringBuilder output, Attribute attr, TextStyle redrawTextStyle);
  135. /// <summary>
  136. /// When overriden in derived class, positions the terminal output cursor to the specified point on the screen.
  137. /// </summary>
  138. /// <param name="screenPositionX">Column to move cursor to</param>
  139. /// <param name="screenPositionY">Row to move cursor to</param>
  140. /// <returns></returns>
  141. protected abstract bool SetCursorPositionImpl (int screenPositionX, int screenPositionY);
  142. /// <summary>
  143. /// Output the contents of the <paramref name="output"/> to the console.
  144. /// </summary>
  145. /// <param name="output"></param>
  146. protected abstract void Write (StringBuilder output);
  147. private void WriteToConsole (StringBuilder output, ref int lastCol, int row, ref int outputWidth)
  148. {
  149. SetCursorPositionImpl (lastCol, row);
  150. // Wrap URLs with OSC 8 hyperlink sequences using the new Osc8UrlLinker
  151. StringBuilder processed = Osc8UrlLinker.WrapOsc8 (output);
  152. Write (processed);
  153. output.Clear ();
  154. lastCol += outputWidth;
  155. outputWidth = 0;
  156. }
  157. }