OutputBufferImpl.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. using System.Diagnostics;
  2. namespace Terminal.Gui.Drivers;
  3. /// <summary>
  4. /// Stores the desired output state for the whole application. This is updated during
  5. /// draw operations before being flushed to the console as part of the main loop.
  6. /// operation
  7. /// </summary>
  8. public class OutputBufferImpl : IOutputBuffer
  9. {
  10. /// <summary>
  11. /// The contents of the application output. The driver outputs this buffer to the terminal when
  12. /// UpdateScreen is called.
  13. /// <remarks>The format of the array is rows, columns. The first index is the row, the second index is the column.</remarks>
  14. /// </summary>
  15. public Cell [,]? Contents { get; set; } = new Cell[0, 0];
  16. private int _cols;
  17. private int _rows;
  18. /// <summary>
  19. /// The <see cref="Attribute"/> that will be used for the next <see cref="AddRune(Rune)"/> or <see cref="AddStr"/>
  20. /// call.
  21. /// </summary>
  22. public Attribute CurrentAttribute { get; set; }
  23. /// <summary>The leftmost column in the terminal.</summary>
  24. public virtual int Left { get; set; } = 0;
  25. /// <summary>
  26. /// Gets the row last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  27. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  28. /// </summary>
  29. public int Row { get; private set; }
  30. /// <summary>
  31. /// Gets the column last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  32. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  33. /// </summary>
  34. public int Col { get; private set; }
  35. /// <summary>The number of rows visible in the terminal.</summary>
  36. public int Rows
  37. {
  38. get => _rows;
  39. set
  40. {
  41. _rows = value;
  42. ClearContents ();
  43. }
  44. }
  45. /// <summary>The number of columns visible in the terminal.</summary>
  46. public int Cols
  47. {
  48. get => _cols;
  49. set
  50. {
  51. _cols = value;
  52. ClearContents ();
  53. }
  54. }
  55. /// <summary>The topmost row in the terminal.</summary>
  56. public virtual int Top { get; set; } = 0;
  57. /// <summary>
  58. /// Indicates which lines have been modified and need to be redrawn.
  59. /// </summary>
  60. public bool [] DirtyLines { get; set; } = [];
  61. // QUESTION: When non-full screen apps are supported, will this represent the app size, or will that be in Application?
  62. /// <summary>Gets the location and size of the terminal screen.</summary>
  63. internal Rectangle Screen => new (0, 0, Cols, Rows);
  64. private Region? _clip;
  65. /// <summary>
  66. /// Gets or sets the clip rectangle that <see cref="AddRune(Rune)"/> and <see cref="AddStr(string)"/> are subject
  67. /// to.
  68. /// </summary>
  69. /// <value>The rectangle describing the of <see cref="Clip"/> region.</value>
  70. public Region? Clip
  71. {
  72. get => _clip;
  73. set
  74. {
  75. if (_clip == value)
  76. {
  77. return;
  78. }
  79. _clip = value;
  80. // Don't ever let Clip be bigger than Screen
  81. if (_clip is { })
  82. {
  83. _clip.Intersect (Screen);
  84. }
  85. }
  86. }
  87. /// <summary>Adds the specified rune to the display at the current cursor position.</summary>
  88. /// <remarks>
  89. /// <para>
  90. /// When the method returns, <see cref="Col"/> will be incremented by the number of columns
  91. /// <paramref name="rune"/> required, even if the new column value is outside of the <see cref="Clip"/> or screen
  92. /// dimensions defined by <see cref="Cols"/>.
  93. /// </para>
  94. /// <para>
  95. /// If <paramref name="rune"/> requires more than one column, and <see cref="Col"/> plus the number of columns
  96. /// needed exceeds the <see cref="Clip"/> or screen dimensions, the default Unicode replacement character (U+FFFD)
  97. /// will be added instead.
  98. /// </para>
  99. /// </remarks>
  100. /// <param name="rune">Text to add.</param>
  101. public void AddRune (Rune rune) { AddStr (rune.ToString ()); }
  102. /// <summary>
  103. /// Adds the specified <see langword="char"/> to the display at the current cursor position. This method is a
  104. /// convenience method that calls <see cref="AddRune(Rune)"/> with the <see cref="Rune"/> constructor.
  105. /// </summary>
  106. /// <param name="c">Character to add.</param>
  107. public void AddRune (char c) { AddRune (new Rune (c)); }
  108. /// <summary>Adds the <paramref name="str"/> to the display at the cursor position.</summary>
  109. /// <remarks>
  110. /// <para>
  111. /// When the method returns, <see cref="Col"/> will be incremented by the number of columns
  112. /// <paramref name="str"/> required, unless the new column value is outside the <see cref="Clip"/> or screen
  113. /// dimensions defined by <see cref="Cols"/>.
  114. /// </para>
  115. /// <para>If <paramref name="str"/> requires more columns than are available, the output will be clipped.</para>
  116. /// </remarks>
  117. /// <param name="str">String.</param>
  118. public void AddStr (string str)
  119. {
  120. foreach (string grapheme in GraphemeHelper.GetGraphemes (str))
  121. {
  122. string text = grapheme;
  123. int textWidth = -1;
  124. bool validLocation = IsValidLocation (text, Col, Row);
  125. if (Contents is null)
  126. {
  127. return;
  128. }
  129. Clip ??= new (Screen);
  130. Rectangle clipRect = Clip!.GetBounds ();
  131. if (validLocation)
  132. {
  133. text = text.MakePrintable ();
  134. textWidth = text.GetColumns ();
  135. lock (Contents)
  136. {
  137. Contents [Row, Col].Attribute = CurrentAttribute;
  138. Contents [Row, Col].IsDirty = true;
  139. if (Col > 0)
  140. {
  141. // Check if cell to left has a wide glyph
  142. if (Contents [Row, Col - 1].Grapheme.GetColumns () > 1)
  143. {
  144. // Invalidate cell to left
  145. Contents [Row, Col - 1].Grapheme = Rune.ReplacementChar.ToString ();
  146. Contents [Row, Col - 1].IsDirty = true;
  147. }
  148. }
  149. if (textWidth is 0 or 1)
  150. {
  151. Contents [Row, Col].Grapheme = text;
  152. if (Col < clipRect.Right - 1)
  153. {
  154. Contents [Row, Col + 1].IsDirty = true;
  155. }
  156. }
  157. else if (textWidth == 2)
  158. {
  159. if (!Clip.Contains (Col + 1, Row))
  160. {
  161. // We're at the right edge of the clip, so we can't display a wide character.
  162. // TODO: Figure out if it is better to show a replacement character or ' '
  163. Contents [Row, Col].Grapheme = Rune.ReplacementChar.ToString ();
  164. }
  165. else if (!Clip.Contains (Col, Row))
  166. {
  167. // Our 1st column is outside the clip, so we can't display a wide character.
  168. Contents [Row, Col + 1].Grapheme = Rune.ReplacementChar.ToString ();
  169. }
  170. else
  171. {
  172. Contents [Row, Col].Grapheme = text;
  173. if (Col < clipRect.Right - 1)
  174. {
  175. // Invalidate cell to right so that it doesn't get drawn
  176. // TODO: Figure out if it is better to show a replacement character or ' '
  177. Contents [Row, Col + 1].Grapheme = Rune.ReplacementChar.ToString ();
  178. Contents [Row, Col + 1].IsDirty = true;
  179. }
  180. }
  181. }
  182. else
  183. {
  184. // This is a non-spacing character, so we don't need to do anything
  185. Contents [Row, Col].Grapheme = " ";
  186. Contents [Row, Col].IsDirty = false;
  187. }
  188. DirtyLines [Row] = true;
  189. }
  190. }
  191. Col++;
  192. if (textWidth > 1)
  193. {
  194. Debug.Assert (textWidth <= 2);
  195. if (validLocation && Col < clipRect.Right)
  196. {
  197. lock (Contents!)
  198. {
  199. // This is a double-width character, and we are not at the end of the line.
  200. // Col now points to the second column of the character. Ensure it doesn't
  201. // Get rendered.
  202. Contents [Row, Col].IsDirty = false;
  203. Contents [Row, Col].Attribute = CurrentAttribute;
  204. // TODO: Determine if we should wipe this out (for now now)
  205. //Contents [Row, Col].Text = (Text)' ';
  206. }
  207. }
  208. Col++;
  209. }
  210. }
  211. }
  212. /// <summary>Clears the <see cref="Contents"/> of the driver.</summary>
  213. public void ClearContents ()
  214. {
  215. Contents = new Cell [Rows, Cols];
  216. //CONCURRENCY: Unsynchronized access to Clip isn't safe.
  217. // TODO: ClearContents should not clear the clip; it should only clear the contents. Move clearing it elsewhere.
  218. Clip = new (Screen);
  219. DirtyLines = new bool [Rows];
  220. lock (Contents)
  221. {
  222. for (var row = 0; row < Rows; row++)
  223. {
  224. for (var c = 0; c < Cols; c++)
  225. {
  226. Contents [row, c] = new ()
  227. {
  228. Grapheme = " ",
  229. Attribute = new Attribute (Color.White, Color.Black),
  230. IsDirty = true
  231. };
  232. }
  233. DirtyLines [row] = true;
  234. }
  235. }
  236. // TODO: Who uses this and why? I am removing for now - this class is a state class not an events class
  237. //ClearedContents?.Invoke (this, EventArgs.Empty);
  238. }
  239. /// <summary>Tests whether the specified coordinate are valid for drawing the specified Text.</summary>
  240. /// <param name="text">Used to determine if one or two columns are required.</param>
  241. /// <param name="col">The column.</param>
  242. /// <param name="row">The row.</param>
  243. /// <returns>
  244. /// <see langword="false"/> if the coordinate is outside the screen bounds or outside of <see cref="Clip"/>.
  245. /// <see langword="true"/> otherwise.
  246. /// </returns>
  247. public bool IsValidLocation (string text, int col, int row)
  248. {
  249. int textWidth = text.GetColumns ();
  250. return col >= 0 && row >= 0 && col + textWidth <= Cols && row < Rows && Clip!.Contains (col, row);
  251. }
  252. /// <inheritdoc/>
  253. public void SetSize (int cols, int rows)
  254. {
  255. Cols = cols;
  256. Rows = rows;
  257. ClearContents ();
  258. }
  259. /// <inheritdoc/>
  260. public void FillRect (Rectangle rect, Rune rune)
  261. {
  262. // BUGBUG: This should be a method on Region
  263. rect = Rectangle.Intersect (rect, Clip?.GetBounds () ?? Screen);
  264. lock (Contents!)
  265. {
  266. for (int r = rect.Y; r < rect.Y + rect.Height; r++)
  267. {
  268. for (int c = rect.X; c < rect.X + rect.Width; c++)
  269. {
  270. if (!IsValidLocation (rune.ToString (), c, r))
  271. {
  272. continue;
  273. }
  274. Contents [r, c] = new ()
  275. {
  276. Grapheme = rune != default (Rune) ? rune.ToString () : " ",
  277. Attribute = CurrentAttribute, IsDirty = true
  278. };
  279. }
  280. }
  281. }
  282. }
  283. /// <inheritdoc/>
  284. public void FillRect (Rectangle rect, char rune)
  285. {
  286. for (int y = rect.Top; y < rect.Top + rect.Height; y++)
  287. {
  288. for (int x = rect.Left; x < rect.Left + rect.Width; x++)
  289. {
  290. Move (x, y);
  291. AddRune (rune);
  292. }
  293. }
  294. }
  295. // TODO: Make internal once Menu is upgraded
  296. /// <summary>
  297. /// Updates <see cref="Col"/> and <see cref="Row"/> to the specified column and row in <see cref="Contents"/>.
  298. /// Used by <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  299. /// </summary>
  300. /// <remarks>
  301. /// <para>This does not move the cursor on the screen, it only updates the internal state of the driver.</para>
  302. /// <para>
  303. /// If <paramref name="col"/> or <paramref name="row"/> are negative or beyond <see cref="Cols"/> and
  304. /// <see cref="Rows"/>, the method still sets those properties.
  305. /// </para>
  306. /// </remarks>
  307. /// <param name="col">Column to move to.</param>
  308. /// <param name="row">Row to move to.</param>
  309. public virtual void Move (int col, int row)
  310. {
  311. //Debug.Assert (col >= 0 && row >= 0 && col < Contents.GetLength(1) && row < Contents.GetLength(0));
  312. Col = col;
  313. Row = row;
  314. }
  315. }