OutputBase.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. namespace Terminal.Gui.Drivers;
  2. public abstract class OutputBase
  3. {
  4. private CursorVisibility? _cachedCursorVisibility;
  5. // Last text style used, for updating style with EscSeqUtils.CSI_AppendTextStyleChange().
  6. private TextStyle _redrawTextStyle = TextStyle.None;
  7. /// <inheritdoc/>
  8. public virtual void Write (IOutputBuffer buffer)
  9. {
  10. if (ConsoleDriver.RunningUnitTests)
  11. {
  12. return;
  13. }
  14. if (Console.WindowHeight < 1
  15. || buffer.Contents.Length != buffer.Rows * buffer.Cols
  16. || buffer.Rows != Console.WindowHeight)
  17. {
  18. // return;
  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 maxCharsPerRune = 2;
  30. Span<char> runeBuffer = stackalloc char [maxCharsPerRune];
  31. for (int row = top; row < rows; row++)
  32. {
  33. if (Console.WindowHeight < 1)
  34. {
  35. return;
  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 attr = buffer.Contents [row, col].Attribute.Value;
  69. // Performance: Only send the escape sequence if the attribute has changed.
  70. if (attr != redrawAttr)
  71. {
  72. redrawAttr = attr;
  73. AppendOrWriteAttribute (output, attr, _redrawTextStyle);
  74. _redrawTextStyle = attr.Style;
  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. // WriteToConsole (output, ref lastCol, row, ref outputWidth);
  94. }
  95. else if (rune.IsSurrogatePair () && rune.GetColumns () < 2)
  96. {
  97. WriteToConsole (output, ref lastCol, row, ref outputWidth);
  98. SetCursorPositionImpl (col - 1, row);
  99. }
  100. buffer.Contents [row, col].IsDirty = false;
  101. }
  102. }
  103. if (output.Length > 0)
  104. {
  105. SetCursorPositionImpl (lastCol, row);
  106. Write (output);
  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. protected abstract void AppendOrWriteAttribute (StringBuilder output, Attribute attr, TextStyle redrawTextStyle);
  121. private void WriteToConsole (StringBuilder output, ref int lastCol, int row, ref int outputWidth)
  122. {
  123. SetCursorPositionImpl (lastCol, row);
  124. Write (output);
  125. output.Clear ();
  126. lastCol += outputWidth;
  127. outputWidth = 0;
  128. }
  129. protected abstract void Write (StringBuilder output);
  130. protected abstract bool SetCursorPositionImpl (int screenPositionX, int screenPositionY);
  131. public abstract void SetCursorVisibility (CursorVisibility visibility);
  132. }