IOutputBuffer.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #nullable enable
  2. namespace Terminal.Gui.Drivers;
  3. /// <summary>
  4. /// Represents the desired screen state for console rendering. This interface provides methods for building up
  5. /// visual content (text, attributes, fills) in a buffer that can be efficiently written to the terminal
  6. /// in a single operation at the end of each iteration. Final output is handled by <see cref="IOutput"/>.
  7. /// </summary>
  8. /// <remarks>
  9. /// <para>
  10. /// The <see cref="IOutputBuffer"/> acts as an intermediary between Terminal.Gui's high-level drawing operations
  11. /// and the low-level console output. Rather than writing directly to the console for each operation, views
  12. /// draw to this buffer during layout and rendering. The buffer is then flushed to the terminal by
  13. /// <see cref="IOutput"/> after all drawing is complete, minimizing flicker and improving performance.
  14. /// </para>
  15. /// <para>
  16. /// The buffer maintains a 2D array of <see cref="Cell"/> objects in <see cref="Contents"/>, where each cell
  17. /// represents a single character position on screen with its associated character, attributes, and dirty state.
  18. /// Drawing operations like <see cref="AddRune(Rune)"/> and <see cref="AddStr(string)"/> modify cells at the
  19. /// current cursor position (tracked by <see cref="Col"/> and <see cref="Row"/>), respecting any active
  20. /// <see cref="Clip"/> region.
  21. /// </para>
  22. /// </remarks>
  23. public interface IOutputBuffer
  24. {
  25. /// <summary>Adds the specified rune to the display at the current cursor position.</summary>
  26. /// <param name="rune">Rune to add.</param>
  27. void AddRune (Rune rune);
  28. /// <summary>
  29. /// Adds the specified character to the display at the current cursor position. This is a convenience method for
  30. /// AddRune.
  31. /// </summary>
  32. /// <param name="c">Character to add.</param>
  33. void AddRune (char c);
  34. /// <summary>Adds the string to the display at the current cursor position.</summary>
  35. /// <param name="str">String to add.</param>
  36. void AddStr (string str);
  37. /// <summary>Clears the contents of the buffer.</summary>
  38. void ClearContents ();
  39. /// <summary>
  40. /// Gets or sets the clip rectangle that <see cref="AddRune(Rune)"/> and <see cref="AddStr(string)"/> are subject
  41. /// to.
  42. /// </summary>
  43. /// <value>The rectangle describing the of <see cref="Clip"/> region.</value>
  44. public Region? Clip { get; set; }
  45. /// <summary>
  46. /// Gets the column last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  47. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  48. /// </summary>
  49. public int Col { get; }
  50. /// <summary>The number of columns visible in the terminal.</summary>
  51. int Cols { get; set; }
  52. /// <summary>
  53. /// The contents of the application output. The driver outputs this buffer to the terminal when UpdateScreen is called.
  54. /// </summary>
  55. Cell [,]? Contents { get; set; }
  56. /// <summary>
  57. /// The <see cref="Attribute"/> that will be used for the next AddRune or AddStr call.
  58. /// </summary>
  59. Attribute CurrentAttribute { get; set; }
  60. /// <summary>
  61. /// Fills the given <paramref name="rect"/> with the given
  62. /// symbol using the currently selected attribute.
  63. /// </summary>
  64. /// <param name="rect"></param>
  65. /// <param name="rune"></param>
  66. void FillRect (Rectangle rect, Rune rune);
  67. /// <summary>
  68. /// Fills the given <paramref name="rect"/> with the given
  69. /// symbol using the currently selected attribute.
  70. /// </summary>
  71. /// <param name="rect"></param>
  72. /// <param name="rune"></param>
  73. void FillRect (Rectangle rect, char rune);
  74. /// <summary>
  75. /// Tests whether the specified coordinate is valid for drawing the specified Rune.
  76. /// </summary>
  77. /// <param name="rune">Used to determine if one or two columns are required.</param>
  78. /// <param name="col">The column.</param>
  79. /// <param name="row">The row.</param>
  80. /// <returns>
  81. /// True if the coordinate is valid for the Rune; false otherwise.
  82. /// </returns>
  83. bool IsValidLocation (Rune rune, int col, int row);
  84. /// <summary>
  85. /// The first cell index on left of screen - basically always 0.
  86. /// Changing this may have unexpected consequences.
  87. /// </summary>
  88. int Left { get; set; }
  89. /// <summary>
  90. /// Updates the column and row to the specified location in the buffer.
  91. /// </summary>
  92. /// <param name="col">The column to move to.</param>
  93. /// <param name="row">The row to move to.</param>
  94. void Move (int col, int row);
  95. /// <summary>
  96. /// Gets the row last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  97. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  98. /// </summary>
  99. public int Row { get; }
  100. /// <summary>The number of rows visible in the terminal.</summary>
  101. int Rows { get; set; }
  102. /// <summary>
  103. /// Changes the size of the buffer to the given size
  104. /// </summary>
  105. /// <param name="cols"></param>
  106. /// <param name="rows"></param>
  107. void SetSize (int cols, int rows);
  108. /// <summary>
  109. /// The first cell index on top of screen - basically always 0.
  110. /// Changing this may have unexpected consequences.
  111. /// </summary>
  112. int Top { get; set; }
  113. }