IConsoleDriver.cs 15 KB

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