IConsoleDriver.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. // BUGBUG: This should not be publicly settable.
  29. /// <summary>
  30. /// Gets or sets the contents of the application output. The driver outputs this buffer to the terminal.
  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 the specified Rune.</summary>
  79. /// <param name="rune">Used to determine if one or two columns are required.</param>
  80. /// <param name="col">The column.</param>
  81. /// <param name="row">The row.</param>
  82. /// <returns>
  83. /// <see langword="false"/> if the coordinate is outside the screen bounds or outside of
  84. /// <see cref="ConsoleDriver.Clip"/>.
  85. /// <see langword="true"/> otherwise.
  86. /// </returns>
  87. bool IsValidLocation (Rune rune, int col, int row);
  88. /// <summary>
  89. /// Updates <see cref="ConsoleDriver.Col"/> and <see cref="ConsoleDriver.Row"/> to the specified column and row in
  90. /// <see cref="ConsoleDriver.Contents"/>.
  91. /// Used by <see cref="ConsoleDriver.AddRune(System.Text.Rune)"/> and <see cref="ConsoleDriver.AddStr"/> to determine
  92. /// where to add content.
  93. /// </summary>
  94. /// <remarks>
  95. /// <para>This does not move the cursor on the screen, it only updates the internal state of the driver.</para>
  96. /// <para>
  97. /// If <paramref name="col"/> or <paramref name="row"/> are negative or beyond <see cref="ConsoleDriver.Cols"/>
  98. /// and
  99. /// <see cref="ConsoleDriver.Rows"/>, the method still sets those properties.
  100. /// </para>
  101. /// </remarks>
  102. /// <param name="col">Column to move to.</param>
  103. /// <param name="row">Row to move to.</param>
  104. void Move (int col, int row);
  105. /// <summary>Adds the specified rune to the display at the current cursor position.</summary>
  106. /// <remarks>
  107. /// <para>
  108. /// When the method returns, <see cref="ConsoleDriver.Col"/> will be incremented by the number of columns
  109. /// <paramref name="rune"/> required, even if the new column value is outside of the
  110. /// <see cref="ConsoleDriver.Clip"/> or screen
  111. /// dimensions defined by <see cref="ConsoleDriver.Cols"/>.
  112. /// </para>
  113. /// <para>
  114. /// If <paramref name="rune"/> requires more than one column, and <see cref="ConsoleDriver.Col"/> plus the number
  115. /// of columns
  116. /// needed exceeds the <see cref="ConsoleDriver.Clip"/> or screen dimensions, the default Unicode replacement
  117. /// character (U+FFFD)
  118. /// will be added instead.
  119. /// </para>
  120. /// </remarks>
  121. /// <param name="rune">Rune to add.</param>
  122. void AddRune (Rune rune);
  123. /// <summary>
  124. /// Adds the specified <see langword="char"/> to the display at the current cursor position. This method is a
  125. /// convenience method that calls <see cref="ConsoleDriver.AddRune(System.Text.Rune)"/> with the <see cref="Rune"/>
  126. /// 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"/>
  135. /// or screen
  136. /// dimensions defined by <see cref="ConsoleDriver.Cols"/>.
  137. /// </para>
  138. /// <para>If <paramref name="str"/> requires more columns than are available, the output will be clipped.</para>
  139. /// </remarks>
  140. /// <param name="str">String.</param>
  141. void AddStr (string str);
  142. /// <summary>Clears the <see cref="ConsoleDriver.Contents"/> of the driver.</summary>
  143. void ClearContents ();
  144. /// <summary>
  145. /// Fills the specified rectangle with the specified rune, using <see cref="ConsoleDriver.CurrentAttribute"/>
  146. /// </summary>
  147. event EventHandler<EventArgs> ClearedContents;
  148. /// <summary>Fills the specified rectangle with the specified rune, using <see cref="ConsoleDriver.CurrentAttribute"/></summary>
  149. /// <remarks>
  150. /// The value of <see cref="ConsoleDriver.Clip"/> is honored. Any parts of the rectangle not in the clip will not be
  151. /// drawn.
  152. /// </remarks>
  153. /// <param name="rect">The Screen-relative rectangle.</param>
  154. /// <param name="rune">The Rune used to fill the rectangle</param>
  155. void FillRect (Rectangle rect, Rune rune = default);
  156. /// <summary>
  157. /// Fills the specified rectangle with the specified <see langword="char"/>. This method is a convenience method
  158. /// that calls <see cref="ConsoleDriver.FillRect(System.Drawing.Rectangle,System.Text.Rune)"/>.
  159. /// </summary>
  160. /// <param name="rect"></param>
  161. /// <param name="c"></param>
  162. void FillRect (Rectangle rect, char c);
  163. /// <summary>Gets the terminal cursor visibility.</summary>
  164. /// <param name="visibility">The current <see cref="CursorVisibility"/></param>
  165. /// <returns><see langword="true"/> upon success</returns>
  166. bool GetCursorVisibility (out CursorVisibility visibility);
  167. /// <summary>Updates the screen to reflect all the changes that have been done to the display buffer</summary>
  168. void Refresh ();
  169. /// <summary>Sets the terminal cursor visibility.</summary>
  170. /// <param name="visibility">The wished <see cref="CursorVisibility"/></param>
  171. /// <returns><see langword="true"/> upon success</returns>
  172. bool SetCursorVisibility (CursorVisibility visibility);
  173. /// <summary>The event fired when the terminal is resized.</summary>
  174. event EventHandler<SizeChangedEventArgs>? SizeChanged;
  175. /// <summary>Suspends the application (e.g. on Linux via SIGTSTP) and upon resume, resets the console driver.</summary>
  176. /// <remarks>This is only implemented in <see cref="CursesDriver"/>.</remarks>
  177. void Suspend ();
  178. /// <summary>
  179. /// Sets the position of the terminal cursor to <see cref="ConsoleDriver.Col"/> and
  180. /// <see cref="ConsoleDriver.Row"/>.
  181. /// </summary>
  182. void UpdateCursor ();
  183. /// <summary>Initializes the driver</summary>
  184. /// <returns>Returns an instance of <see cref="MainLoop"/> using the <see cref="IMainLoopDriver"/> for the driver.</returns>
  185. MainLoop Init ();
  186. /// <summary>Ends the execution of the console driver.</summary>
  187. void End ();
  188. /// <summary>Selects the specified attribute as the attribute to use for future calls to AddRune and AddString.</summary>
  189. /// <remarks>Implementations should call <c>base.SetAttribute(c)</c>.</remarks>
  190. /// <param name="c">C.</param>
  191. Attribute SetAttribute (Attribute c);
  192. /// <summary>Gets the current <see cref="Attribute"/>.</summary>
  193. /// <returns>The current attribute.</returns>
  194. Attribute GetAttribute ();
  195. /// <summary>Makes an <see cref="Attribute"/>.</summary>
  196. /// <param name="foreground">The foreground color.</param>
  197. /// <param name="background">The background color.</param>
  198. /// <returns>The attribute for the foreground and background colors.</returns>
  199. Attribute MakeColor (in Color foreground, in Color background);
  200. /// <summary>Event fired when a mouse event occurs.</summary>
  201. event EventHandler<MouseEventArgs>? MouseEvent;
  202. /// <summary>Event fired when a key is pressed down. This is a precursor to <see cref="ConsoleDriver.KeyUp"/>.</summary>
  203. event EventHandler<Key>? KeyDown;
  204. /// <summary>Event fired when a key is released.</summary>
  205. /// <remarks>
  206. /// Drivers that do not support key release events will fire this event after <see cref="ConsoleDriver.KeyDown"/>
  207. /// processing is
  208. /// complete.
  209. /// </remarks>
  210. event EventHandler<Key>? KeyUp;
  211. /// <summary>Simulates a key press.</summary>
  212. /// <param name="keyChar">The key character.</param>
  213. /// <param name="key">The key.</param>
  214. /// <param name="shift">If <see langword="true"/> simulates the Shift key being pressed.</param>
  215. /// <param name="alt">If <see langword="true"/> simulates the Alt key being pressed.</param>
  216. /// <param name="ctrl">If <see langword="true"/> simulates the Ctrl key being pressed.</param>
  217. void SendKeys (char keyChar, ConsoleKey key, bool shift, bool alt, bool ctrl);
  218. /// <summary>
  219. /// Queues the given <paramref name="request"/> for execution
  220. /// </summary>
  221. /// <param name="request"></param>
  222. public void QueueAnsiRequest (AnsiEscapeSequenceRequest request);
  223. /// <summary>
  224. /// Gets the <see cref="AnsiRequestScheduler"/> for the driver
  225. /// </summary>
  226. /// <returns></returns>
  227. public AnsiRequestScheduler GetRequestScheduler ();
  228. }