IConsoleDriver.cs 12 KB

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