OutputBase.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. for (int row = top; row < rows; row++)
  29. {
  30. if (!SetCursorPositionImpl (0, row))
  31. {
  32. return;
  33. }
  34. output.Clear ();
  35. for (int col = left; col < cols; col++)
  36. {
  37. lastCol = -1;
  38. var outputWidth = 0;
  39. for (; col < cols; col++)
  40. {
  41. if (!buffer.Contents! [row, col].IsDirty)
  42. {
  43. if (output.Length > 0)
  44. {
  45. WriteToConsole (output, ref lastCol, row, ref outputWidth);
  46. }
  47. else if (lastCol == -1)
  48. {
  49. lastCol = col;
  50. }
  51. if (lastCol + 1 < cols)
  52. {
  53. lastCol++;
  54. }
  55. continue;
  56. }
  57. if (lastCol == -1)
  58. {
  59. lastCol = col;
  60. }
  61. Cell cell = buffer.Contents [row, col];
  62. AppendCellAnsi (cell, output, ref redrawAttr, ref _redrawTextStyle, cols, ref col);
  63. outputWidth++;
  64. buffer.Contents [row, col].IsDirty = false;
  65. }
  66. }
  67. if (output.Length > 0)
  68. {
  69. SetCursorPositionImpl (lastCol, row);
  70. // Wrap URLs with OSC 8 hyperlink sequences using the new Osc8UrlLinker
  71. StringBuilder processed = Osc8UrlLinker.WrapOsc8 (output);
  72. Write (processed);
  73. }
  74. }
  75. // BUGBUG: The Sixel impl depends on the legacy static Application object
  76. // BUGBUG: Disabled for now
  77. //foreach (SixelToRender s in Application.Sixel)
  78. //{
  79. // if (!string.IsNullOrWhiteSpace (s.SixelData))
  80. // {
  81. // SetCursorPositionImpl (s.ScreenPosition.X, s.ScreenPosition.Y);
  82. // Console.Out.Write (s.SixelData);
  83. // }
  84. //}
  85. SetCursorVisibility (savedVisibility ?? CursorVisibility.Default);
  86. _cachedCursorVisibility = savedVisibility;
  87. }
  88. /// <summary>
  89. /// Changes the color and text style of the console to the given <paramref name="attr"/> and
  90. /// <paramref name="redrawTextStyle"/>.
  91. /// If command can be buffered in line with other output (e.g. CSI sequence) then it should be appended to
  92. /// <paramref name="output"/>
  93. /// otherwise the relevant output state should be flushed directly (e.g. by calling relevant win 32 API method)
  94. /// </summary>
  95. /// <param name="output"></param>
  96. /// <param name="attr"></param>
  97. /// <param name="redrawTextStyle"></param>
  98. protected abstract void AppendOrWriteAttribute (StringBuilder output, Attribute attr, TextStyle redrawTextStyle);
  99. /// <summary>
  100. /// When overriden in derived class, positions the terminal output cursor to the specified point on the screen.
  101. /// </summary>
  102. /// <param name="screenPositionX">Column to move cursor to</param>
  103. /// <param name="screenPositionY">Row to move cursor to</param>
  104. /// <returns></returns>
  105. protected abstract bool SetCursorPositionImpl (int screenPositionX, int screenPositionY);
  106. /// <summary>
  107. /// Output the contents of the <paramref name="output"/> to the console.
  108. /// </summary>
  109. /// <param name="output"></param>
  110. protected abstract void Write (StringBuilder output);
  111. /// <summary>
  112. /// Builds ANSI escape sequences for the specified rectangular region of the buffer.
  113. /// </summary>
  114. /// <param name="buffer">The output buffer to build ANSI for.</param>
  115. /// <param name="startRow">The starting row (inclusive).</param>
  116. /// <param name="endRow">The ending row (exclusive).</param>
  117. /// <param name="startCol">The starting column (inclusive).</param>
  118. /// <param name="endCol">The ending column (exclusive).</param>
  119. /// <param name="output">The StringBuilder to append ANSI sequences to.</param>
  120. /// <param name="lastAttr">The last attribute used, for optimization.</param>
  121. /// <param name="includeCellPredicate">Predicate to determine which cells to include. If null, includes all cells.</param>
  122. /// <param name="addNewlines">Whether to add newlines between rows.</param>
  123. protected void BuildAnsiForRegion (
  124. IOutputBuffer buffer,
  125. int startRow,
  126. int endRow,
  127. int startCol,
  128. int endCol,
  129. StringBuilder output,
  130. ref Attribute? lastAttr,
  131. Func<int, int, bool>? includeCellPredicate = null,
  132. bool addNewlines = true
  133. )
  134. {
  135. TextStyle redrawTextStyle = TextStyle.None;
  136. for (int row = startRow; row < endRow; row++)
  137. {
  138. for (int col = startCol; col < endCol; col++)
  139. {
  140. if (includeCellPredicate != null && !includeCellPredicate (row, col))
  141. {
  142. continue;
  143. }
  144. Cell cell = buffer.Contents![row, col];
  145. AppendCellAnsi (cell, output, ref lastAttr, ref redrawTextStyle, endCol, ref col);
  146. }
  147. // Add newline at end of row if requested
  148. if (addNewlines)
  149. {
  150. output.AppendLine ();
  151. }
  152. }
  153. }
  154. /// <summary>
  155. /// Appends ANSI sequences for a single cell to the output.
  156. /// </summary>
  157. /// <param name="cell">The cell to append ANSI for.</param>
  158. /// <param name="output">The StringBuilder to append to.</param>
  159. /// <param name="lastAttr">The last attribute used, updated if the cell's attribute is different.</param>
  160. /// <param name="redrawTextStyle">The current text style for optimization.</param>
  161. /// <param name="maxCol">The maximum column, used for wide character handling.</param>
  162. /// <param name="currentCol">The current column, updated for wide characters.</param>
  163. protected void AppendCellAnsi (Cell cell, StringBuilder output, ref Attribute? lastAttr, ref TextStyle redrawTextStyle, int maxCol, ref int currentCol)
  164. {
  165. Attribute? attribute = cell.Attribute;
  166. // Add ANSI escape sequence for attribute change
  167. if (attribute.HasValue && attribute.Value != lastAttr)
  168. {
  169. lastAttr = attribute.Value;
  170. AppendOrWriteAttribute (output, attribute.Value, redrawTextStyle);
  171. redrawTextStyle = attribute.Value.Style;
  172. }
  173. // Add the grapheme
  174. string grapheme = cell.Grapheme;
  175. output.Append (grapheme);
  176. // Handle wide grapheme
  177. if (grapheme.GetColumns () > 1 && currentCol + 1 < maxCol)
  178. {
  179. currentCol++; // Skip next cell for wide character
  180. }
  181. }
  182. /// <summary>
  183. /// Generates an ANSI escape sequence string representation of the given <paramref name="buffer"/> contents.
  184. /// This is the same output that would be written to the terminal to recreate the current screen contents.
  185. /// </summary>
  186. /// <param name="buffer">The output buffer to convert to ANSI.</param>
  187. /// <returns>A string containing ANSI escape sequences representing the buffer contents.</returns>
  188. public string ToAnsi (IOutputBuffer buffer)
  189. {
  190. var output = new StringBuilder ();
  191. Attribute? lastAttr = null;
  192. BuildAnsiForRegion (buffer, 0, buffer.Rows, 0, buffer.Cols, output, ref lastAttr);
  193. return output.ToString ();
  194. }
  195. private void WriteToConsole (StringBuilder output, ref int lastCol, int row, ref int outputWidth)
  196. {
  197. SetCursorPositionImpl (lastCol, row);
  198. // Wrap URLs with OSC 8 hyperlink sequences using the new Osc8UrlLinker
  199. StringBuilder processed = Osc8UrlLinker.WrapOsc8 (output);
  200. Write (processed);
  201. output.Clear ();
  202. lastCol += outputWidth;
  203. outputWidth = 0;
  204. }
  205. }