OutputBase.cs 6.9 KB

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