IOutputBuffer.cs 6.0 KB

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