IConsoleDriver.cs 14 KB

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