IConsoleDriver.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #nullable enable
  2. namespace Terminal.Gui.Drivers;
  3. /// <summary>Base interface for Terminal.Gui ConsoleDriver implementations.</summary>
  4. /// <remarks>
  5. /// There are currently four implementations: UnixDriver, WindowsDriver, DotNetDriver, and FakeDriver
  6. /// </remarks>
  7. public interface IConsoleDriver
  8. {
  9. /// <summary>Get the operating system clipboard.</summary>
  10. IClipboard? Clipboard { get; }
  11. /// <summary>Gets the location and size of the terminal screen.</summary>
  12. Rectangle Screen { get; }
  13. /// <summary>
  14. /// Sets the screen size for testing purposes. Only supported by FakeDriver.
  15. /// <see cref="Screen"/> is the source of truth for screen dimensions.
  16. /// </summary>
  17. /// <param name="width">The new width in columns.</param>
  18. /// <param name="height">The new height in rows.</param>
  19. /// <exception cref="NotSupportedException">Thrown when called on non-FakeDriver instances.</exception>
  20. void SetScreenSize (int width, int height);
  21. /// <summary>
  22. /// Gets or sets the clip rectangle that <see cref="AddRune(Rune)"/> and <see cref="AddStr(string)"/> are subject
  23. /// to.
  24. /// </summary>
  25. /// <value>The rectangle describing the of <see cref="Clip"/> region.</value>
  26. Region? Clip { get; set; }
  27. /// <summary>
  28. /// Gets the column last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  29. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  30. /// </summary>
  31. int Col { get; }
  32. /// <summary>The number of columns visible in the terminal.</summary>
  33. int Cols { get; set; }
  34. // BUGBUG: This should not be publicly settable.
  35. /// <summary>
  36. /// Gets or sets the contents of the application output. The driver outputs this buffer to the terminal.
  37. /// <remarks>The format of the array is rows, columns. The first index is the row, the second index is the column.</remarks>
  38. /// </summary>
  39. Cell [,]? Contents { get; set; }
  40. /// <summary>The leftmost column in the terminal.</summary>
  41. int Left { get; set; }
  42. /// <summary>
  43. /// Gets the row last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  44. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  45. /// </summary>
  46. int Row { get; }
  47. /// <summary>The number of rows visible in the terminal.</summary>
  48. int Rows { get; set; }
  49. /// <summary>The topmost row in the terminal.</summary>
  50. int Top { get; set; }
  51. /// <summary>Gets whether the <see cref="ConsoleDriver"/> supports TrueColor output.</summary>
  52. bool SupportsTrueColor { get; }
  53. /// <summary>
  54. /// Gets or sets whether the <see cref="ConsoleDriver"/> should use 16 colors instead of the default TrueColors.
  55. /// See <see cref="Application.Force16Colors"/> to change this setting via <see cref="ConfigurationManager"/>.
  56. /// </summary>
  57. /// <remarks>
  58. /// <para>
  59. /// Will be forced to <see langword="true"/> if <see cref="ConsoleDriver.SupportsTrueColor"/> is
  60. /// <see langword="false"/>, indicating that the <see cref="ConsoleDriver"/> cannot support TrueColor.
  61. /// </para>
  62. /// </remarks>
  63. bool Force16Colors { get; set; }
  64. /// <summary>
  65. /// The <see cref="System.Attribute"/> that will be used for the next <see cref="AddRune(Rune)"/> or <see cref="AddStr"/>
  66. /// call.
  67. /// </summary>
  68. Attribute CurrentAttribute { get; set; }
  69. /// <summary>Returns the name of the driver and relevant library version information.</summary>
  70. /// <returns></returns>
  71. string GetVersionInfo ();
  72. /// <summary>
  73. /// Provide proper writing to send escape sequence recognized by the <see cref="ConsoleDriver"/>.
  74. /// </summary>
  75. /// <param name="ansi"></param>
  76. void WriteRaw (string ansi);
  77. /// <summary>Tests if the specified rune is supported by the driver.</summary>
  78. /// <param name="rune"></param>
  79. /// <returns>
  80. /// <see langword="true"/> if the rune can be properly presented; <see langword="false"/> if the driver does not
  81. /// support displaying this rune.
  82. /// </returns>
  83. bool IsRuneSupported (Rune rune);
  84. /// <summary>Tests whether the specified coordinate are valid for drawing the specified Rune.</summary>
  85. /// <param name="rune">Used to determine if one or two columns are required.</param>
  86. /// <param name="col">The column.</param>
  87. /// <param name="row">The row.</param>
  88. /// <returns>
  89. /// <see langword="false"/> if the coordinate is outside the screen bounds or outside of
  90. /// <see cref="ConsoleDriver.Clip"/>.
  91. /// <see langword="true"/> otherwise.
  92. /// </returns>
  93. bool IsValidLocation (Rune rune, int col, int row);
  94. /// <summary>
  95. /// Updates <see cref="ConsoleDriver.Col"/> and <see cref="ConsoleDriver.Row"/> to the specified column and row in
  96. /// <see cref="ConsoleDriver.Contents"/>.
  97. /// Used by <see cref="ConsoleDriver.AddRune(System.Text.Rune)"/> and <see cref="ConsoleDriver.AddStr"/> to determine
  98. /// where to add content.
  99. /// </summary>
  100. /// <remarks>
  101. /// <para>This does not move the cursor on the screen, it only updates the internal state of the driver.</para>
  102. /// <para>
  103. /// If <paramref name="col"/> or <paramref name="row"/> are negative or beyond <see cref="ConsoleDriver.Cols"/>
  104. /// and
  105. /// <see cref="ConsoleDriver.Rows"/>, the method still sets those properties.
  106. /// </para>
  107. /// </remarks>
  108. /// <param name="col">Column to move to.</param>
  109. /// <param name="row">Row to move to.</param>
  110. void Move (int col, int row);
  111. /// <summary>Adds the specified rune to the display at the current cursor position.</summary>
  112. /// <remarks>
  113. /// <para>
  114. /// When the method returns, <see cref="ConsoleDriver.Col"/> will be incremented by the number of columns
  115. /// <paramref name="rune"/> required, even if the new column value is outside of the
  116. /// <see cref="ConsoleDriver.Clip"/> or screen
  117. /// dimensions defined by <see cref="ConsoleDriver.Cols"/>.
  118. /// </para>
  119. /// <para>
  120. /// If <paramref name="rune"/> requires more than one column, and <see cref="ConsoleDriver.Col"/> plus the number
  121. /// of columns
  122. /// needed exceeds the <see cref="ConsoleDriver.Clip"/> or screen dimensions, the default Unicode replacement
  123. /// character (U+FFFD)
  124. /// will be added instead.
  125. /// </para>
  126. /// </remarks>
  127. /// <param name="rune">Rune to add.</param>
  128. void AddRune (Rune rune);
  129. /// <summary>
  130. /// Adds the specified <see langword="char"/> to the display at the current cursor position. This method is a
  131. /// convenience method that calls <see cref="ConsoleDriver.AddRune(System.Text.Rune)"/> with the <see cref="Rune"/>
  132. /// constructor.
  133. /// </summary>
  134. /// <param name="c">Character to add.</param>
  135. void AddRune (char c);
  136. /// <summary>Adds the <paramref name="str"/> to the display at the cursor position.</summary>
  137. /// <remarks>
  138. /// <para>
  139. /// When the method returns, <see cref="ConsoleDriver.Col"/> will be incremented by the number of columns
  140. /// <paramref name="str"/> required, unless the new column value is outside of the <see cref="ConsoleDriver.Clip"/>
  141. /// or screen
  142. /// dimensions defined by <see cref="ConsoleDriver.Cols"/>.
  143. /// </para>
  144. /// <para>If <paramref name="str"/> requires more columns than are available, the output will be clipped.</para>
  145. /// </remarks>
  146. /// <param name="str">String.</param>
  147. void AddStr (string str);
  148. /// <summary>Clears the <see cref="ConsoleDriver.Contents"/> of the driver.</summary>
  149. void ClearContents ();
  150. /// <summary>
  151. /// Fills the specified rectangle with the specified rune, using <see cref="ConsoleDriver.CurrentAttribute"/>
  152. /// </summary>
  153. event EventHandler<EventArgs> ClearedContents;
  154. /// <summary>Fills the specified rectangle with the specified rune, using <see cref="ConsoleDriver.CurrentAttribute"/></summary>
  155. /// <remarks>
  156. /// The value of <see cref="ConsoleDriver.Clip"/> is honored. Any parts of the rectangle not in the clip will not be
  157. /// drawn.
  158. /// </remarks>
  159. /// <param name="rect">The Screen-relative rectangle.</param>
  160. /// <param name="rune">The Rune used to fill the rectangle</param>
  161. void FillRect (Rectangle rect, Rune rune = default);
  162. /// <summary>
  163. /// Fills the specified rectangle with the specified <see langword="char"/>. This method is a convenience method
  164. /// that calls <see cref="ConsoleDriver.FillRect(System.Drawing.Rectangle,System.Text.Rune)"/>.
  165. /// </summary>
  166. /// <param name="rect"></param>
  167. /// <param name="c"></param>
  168. void FillRect (Rectangle rect, char c);
  169. /// <summary>Gets the terminal cursor visibility.</summary>
  170. /// <param name="visibility">The current <see cref="CursorVisibility"/></param>
  171. /// <returns><see langword="true"/> upon success</returns>
  172. bool GetCursorVisibility (out CursorVisibility visibility);
  173. /// <summary>Updates the screen to reflect all the changes that have been done to the display buffer</summary>
  174. void Refresh ();
  175. /// <summary>Sets the terminal cursor visibility.</summary>
  176. /// <param name="visibility">The wished <see cref="CursorVisibility"/></param>
  177. /// <returns><see langword="true"/> upon success</returns>
  178. bool SetCursorVisibility (CursorVisibility visibility);
  179. /// <summary>
  180. /// The event fired when the screen changes (size, position, etc.).
  181. /// <see cref="Screen"/> is the source of truth for screen dimensions.
  182. /// </summary>
  183. event EventHandler<SizeChangedEventArgs>? SizeChanged;
  184. /// <summary>Suspends the application (e.g. on Linux via SIGTSTP) and upon resume, resets the console driver.</summary>
  185. /// <remarks>This is only implemented in UnixDriver.</remarks>
  186. void Suspend ();
  187. /// <summary>
  188. /// Sets the position of the terminal cursor to <see cref="ConsoleDriver.Col"/> and
  189. /// <see cref="ConsoleDriver.Row"/>.
  190. /// </summary>
  191. void UpdateCursor ();
  192. /// <summary>Initializes the driver</summary>
  193. void Init ();
  194. /// <summary>Ends the execution of the console driver.</summary>
  195. void End ();
  196. /// <summary>Selects the specified attribute as the attribute to use for future calls to AddRune and AddString.</summary>
  197. /// <remarks>Implementations should call <c>base.SetAttribute(c)</c>.</remarks>
  198. /// <param name="c">C.</param>
  199. Attribute SetAttribute (Attribute c);
  200. /// <summary>Gets the current <see cref="Attribute"/>.</summary>
  201. /// <returns>The current attribute.</returns>
  202. Attribute GetAttribute ();
  203. /// <summary>Event fired when a mouse event occurs.</summary>
  204. event EventHandler<MouseEventArgs>? MouseEvent;
  205. /// <summary>Event fired when a key is pressed down. This is a precursor to <see cref="ConsoleDriver.KeyUp"/>.</summary>
  206. event EventHandler<Key>? KeyDown;
  207. /// <summary>Event fired when a key is released.</summary>
  208. /// <remarks>
  209. /// Drivers that do not support key release events will fire this event after <see cref="ConsoleDriver.KeyDown"/>
  210. /// processing is
  211. /// complete.
  212. /// </remarks>
  213. event EventHandler<Key>? KeyUp;
  214. /// <summary>
  215. /// Queues the given <paramref name="request"/> for execution
  216. /// </summary>
  217. /// <param name="request"></param>
  218. public void QueueAnsiRequest (AnsiEscapeSequenceRequest request);
  219. /// <summary>
  220. /// Gets the <see cref="AnsiRequestScheduler"/> for the driver
  221. /// </summary>
  222. /// <returns></returns>
  223. public AnsiRequestScheduler GetRequestScheduler ();
  224. }