2
0

OutputBase.cs 6.9 KB

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