NetOutput.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using Microsoft.Extensions.Logging;
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Implementation of <see cref="IConsoleOutput"/> that uses native dotnet
  5. /// methods e.g. <see cref="System.Console"/>
  6. /// </summary>
  7. public class NetOutput : IConsoleOutput
  8. {
  9. private readonly bool _isWinPlatform;
  10. private CursorVisibility? _cachedCursorVisibility;
  11. /// <summary>
  12. /// Creates a new instance of the <see cref="NetOutput"/> class.
  13. /// </summary>
  14. public NetOutput ()
  15. {
  16. Logging.Logger.LogInformation ($"Creating {nameof (NetOutput)}");
  17. PlatformID p = Environment.OSVersion.Platform;
  18. if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows)
  19. {
  20. _isWinPlatform = true;
  21. }
  22. //Enable alternative screen buffer.
  23. Console.Out.Write (EscSeqUtils.CSI_SaveCursorAndActivateAltBufferNoBackscroll);
  24. //Set cursor key to application.
  25. Console.Out.Write (EscSeqUtils.CSI_HideCursor);
  26. }
  27. /// <inheritdoc/>
  28. public void Write (string text) { Console.Write (text); }
  29. /// <inheritdoc/>
  30. public void Write (IOutputBuffer buffer)
  31. {
  32. if (Console.WindowHeight < 1
  33. || buffer.Contents.Length != buffer.Rows * buffer.Cols
  34. || buffer.Rows != Console.WindowHeight)
  35. {
  36. // return;
  37. }
  38. var top = 0;
  39. var left = 0;
  40. int rows = buffer.Rows;
  41. int cols = buffer.Cols;
  42. var output = new StringBuilder ();
  43. Attribute? redrawAttr = null;
  44. int lastCol = -1;
  45. CursorVisibility? savedVisibility = _cachedCursorVisibility;
  46. SetCursorVisibility (CursorVisibility.Invisible);
  47. for (int row = top; row < rows; row++)
  48. {
  49. if (Console.WindowHeight < 1)
  50. {
  51. return;
  52. }
  53. if (!buffer.DirtyLines [row])
  54. {
  55. continue;
  56. }
  57. if (!SetCursorPositionImpl (0, row))
  58. {
  59. return;
  60. }
  61. buffer.DirtyLines [row] = false;
  62. output.Clear ();
  63. for (int col = left; col < cols; col++)
  64. {
  65. lastCol = -1;
  66. var outputWidth = 0;
  67. for (; col < cols; col++)
  68. {
  69. if (!buffer.Contents [row, col].IsDirty)
  70. {
  71. if (output.Length > 0)
  72. {
  73. WriteToConsole (output, ref lastCol, row, ref outputWidth);
  74. }
  75. else if (lastCol == -1)
  76. {
  77. lastCol = col;
  78. }
  79. if (lastCol + 1 < cols)
  80. {
  81. lastCol++;
  82. }
  83. continue;
  84. }
  85. if (lastCol == -1)
  86. {
  87. lastCol = col;
  88. }
  89. Attribute attr = buffer.Contents [row, col].Attribute.Value;
  90. // Performance: Only send the escape sequence if the attribute has changed.
  91. if (attr != redrawAttr)
  92. {
  93. redrawAttr = attr;
  94. output.Append (
  95. EscSeqUtils.CSI_SetForegroundColorRGB (
  96. attr.Foreground.R,
  97. attr.Foreground.G,
  98. attr.Foreground.B
  99. )
  100. );
  101. output.Append (
  102. EscSeqUtils.CSI_SetBackgroundColorRGB (
  103. attr.Background.R,
  104. attr.Background.G,
  105. attr.Background.B
  106. )
  107. );
  108. }
  109. outputWidth++;
  110. Rune rune = buffer.Contents [row, col].Rune;
  111. output.Append (rune);
  112. if (buffer.Contents [row, col].CombiningMarks.Count > 0)
  113. {
  114. // AtlasEngine does not support NON-NORMALIZED combining marks in a way
  115. // compatible with the driver architecture. Any CMs (except in the first col)
  116. // are correctly combined with the base char, but are ALSO treated as 1 column
  117. // width codepoints E.g. `echo "[e`u{0301}`u{0301}]"` will output `[é ]`.
  118. //
  119. // For now, we just ignore the list of CMs.
  120. //foreach (var combMark in Contents [row, col].CombiningMarks) {
  121. // output.Append (combMark);
  122. //}
  123. // WriteToConsole (output, ref lastCol, row, ref outputWidth);
  124. }
  125. else if (rune.IsSurrogatePair () && rune.GetColumns () < 2)
  126. {
  127. WriteToConsole (output, ref lastCol, row, ref outputWidth);
  128. SetCursorPositionImpl (col - 1, row);
  129. }
  130. buffer.Contents [row, col].IsDirty = false;
  131. }
  132. }
  133. if (output.Length > 0)
  134. {
  135. SetCursorPositionImpl (lastCol, row);
  136. Console.Write (output);
  137. }
  138. }
  139. foreach (SixelToRender s in Application.Sixel)
  140. {
  141. if (!string.IsNullOrWhiteSpace (s.SixelData))
  142. {
  143. SetCursorPositionImpl (s.ScreenPosition.X, s.ScreenPosition.Y);
  144. Console.Write (s.SixelData);
  145. }
  146. }
  147. SetCursorVisibility (savedVisibility ?? CursorVisibility.Default);
  148. _cachedCursorVisibility = savedVisibility;
  149. }
  150. /// <inheritdoc/>
  151. public Size GetWindowSize () { return new (Console.WindowWidth, Console.WindowHeight); }
  152. private void WriteToConsole (StringBuilder output, ref int lastCol, int row, ref int outputWidth)
  153. {
  154. SetCursorPositionImpl (lastCol, row);
  155. Console.Write (output);
  156. output.Clear ();
  157. lastCol += outputWidth;
  158. outputWidth = 0;
  159. }
  160. /// <inheritdoc/>
  161. public void SetCursorPosition (int col, int row) { SetCursorPositionImpl (col, row); }
  162. private Point _lastCursorPosition;
  163. private bool SetCursorPositionImpl (int col, int row)
  164. {
  165. if (_lastCursorPosition.X == col && _lastCursorPosition.Y == row)
  166. {
  167. return true;
  168. }
  169. _lastCursorPosition = new (col, row);
  170. if (_isWinPlatform)
  171. {
  172. // Could happens that the windows is still resizing and the col is bigger than Console.WindowWidth.
  173. try
  174. {
  175. Console.SetCursorPosition (col, row);
  176. return true;
  177. }
  178. catch (Exception)
  179. {
  180. return false;
  181. }
  182. }
  183. // + 1 is needed because non-Windows is based on 1 instead of 0 and
  184. // Console.CursorTop/CursorLeft isn't reliable.
  185. Console.Out.Write (EscSeqUtils.CSI_SetCursorPosition (row + 1, col + 1));
  186. return true;
  187. }
  188. /// <inheritdoc/>
  189. public void Dispose ()
  190. {
  191. Console.ResetColor ();
  192. //Disable alternative screen buffer.
  193. Console.Out.Write (EscSeqUtils.CSI_RestoreCursorAndRestoreAltBufferWithBackscroll);
  194. //Set cursor key to cursor.
  195. Console.Out.Write (EscSeqUtils.CSI_ShowCursor);
  196. Console.Out.Close ();
  197. }
  198. /// <inheritdoc/>
  199. public void SetCursorVisibility (CursorVisibility visibility)
  200. {
  201. Console.Out.Write (visibility == CursorVisibility.Default ? EscSeqUtils.CSI_ShowCursor : EscSeqUtils.CSI_HideCursor);
  202. }
  203. }