ConsoleDriverFacade.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. using System.Runtime.InteropServices;
  2. namespace Terminal.Gui;
  3. internal class ConsoleDriverFacade<T> : IConsoleDriver, IConsoleDriverFacade
  4. {
  5. private readonly IConsoleOutput _output;
  6. private readonly IOutputBuffer _outputBuffer;
  7. private readonly AnsiRequestScheduler _ansiRequestScheduler;
  8. private CursorVisibility _lastCursor = CursorVisibility.Default;
  9. /// <summary>The event fired when the terminal is resized.</summary>
  10. public event EventHandler<SizeChangedEventArgs> SizeChanged;
  11. public IInputProcessor InputProcessor { get; }
  12. public ConsoleDriverFacade (
  13. IInputProcessor inputProcessor,
  14. IOutputBuffer outputBuffer,
  15. IConsoleOutput output,
  16. AnsiRequestScheduler ansiRequestScheduler,
  17. IWindowSizeMonitor windowSizeMonitor
  18. )
  19. {
  20. InputProcessor = inputProcessor;
  21. _output = output;
  22. _outputBuffer = outputBuffer;
  23. _ansiRequestScheduler = ansiRequestScheduler;
  24. InputProcessor.KeyDown += (s, e) => KeyDown?.Invoke (s, e);
  25. InputProcessor.KeyUp += (s, e) => KeyUp?.Invoke (s, e);
  26. InputProcessor.MouseEvent += (s, e) => MouseEvent?.Invoke (s, e);
  27. windowSizeMonitor.SizeChanging += (_, e) => SizeChanged?.Invoke (this, e);
  28. CreateClipboard ();
  29. }
  30. private void CreateClipboard ()
  31. {
  32. PlatformID p = Environment.OSVersion.Platform;
  33. if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows)
  34. {
  35. Clipboard = new WindowsClipboard ();
  36. }
  37. else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX))
  38. {
  39. Clipboard = new MacOSXClipboard ();
  40. }
  41. else if (CursesDriver.Is_WSL_Platform ())
  42. {
  43. Clipboard = new WSLClipboard ();
  44. }
  45. else
  46. {
  47. Clipboard = new FakeDriver.FakeClipboard ();
  48. }
  49. }
  50. /// <summary>Gets the location and size of the terminal screen.</summary>
  51. public Rectangle Screen => new (new (0, 0), _output.GetWindowSize ());
  52. /// <summary>
  53. /// Gets or sets the clip rectangle that <see cref="AddRune(Rune)"/> and <see cref="AddStr(string)"/> are subject
  54. /// to.
  55. /// </summary>
  56. /// <value>The rectangle describing the of <see cref="Clip"/> region.</value>
  57. public Region Clip
  58. {
  59. get => _outputBuffer.Clip;
  60. set => _outputBuffer.Clip = value;
  61. }
  62. /// <summary>Get the operating system clipboard.</summary>
  63. public IClipboard Clipboard { get; private set; } = new FakeDriver.FakeClipboard ();
  64. /// <summary>
  65. /// Gets the column last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  66. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  67. /// </summary>
  68. public int Col => _outputBuffer.Col;
  69. /// <summary>The number of columns visible in the terminal.</summary>
  70. public int Cols
  71. {
  72. get => _outputBuffer.Cols;
  73. set => _outputBuffer.Cols = value;
  74. }
  75. /// <summary>
  76. /// The contents of the application output. The driver outputs this buffer to the terminal.
  77. /// <remarks>The format of the array is rows, columns. The first index is the row, the second index is the column.</remarks>
  78. /// </summary>
  79. public Cell [,] Contents
  80. {
  81. get => _outputBuffer.Contents;
  82. set => _outputBuffer.Contents = value;
  83. }
  84. /// <summary>The leftmost column in the terminal.</summary>
  85. public int Left
  86. {
  87. get => _outputBuffer.Left;
  88. set => _outputBuffer.Left = value;
  89. }
  90. /// <summary>
  91. /// Gets the row last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  92. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  93. /// </summary>
  94. public int Row => _outputBuffer.Row;
  95. /// <summary>The number of rows visible in the terminal.</summary>
  96. public int Rows
  97. {
  98. get => _outputBuffer.Rows;
  99. set => _outputBuffer.Rows = value;
  100. }
  101. /// <summary>The topmost row in the terminal.</summary>
  102. public int Top
  103. {
  104. get => _outputBuffer.Top;
  105. set => _outputBuffer.Top = value;
  106. }
  107. // TODO: Probably not everyone right?
  108. /// <summary>Gets whether the <see cref="ConsoleDriver"/> supports TrueColor output.</summary>
  109. public bool SupportsTrueColor => true;
  110. // TODO: Currently ignored
  111. /// <summary>
  112. /// Gets or sets whether the <see cref="ConsoleDriver"/> should use 16 colors instead of the default TrueColors.
  113. /// See <see cref="Application.Force16Colors"/> to change this setting via <see cref="ConfigurationManager"/>.
  114. /// </summary>
  115. /// <remarks>
  116. /// <para>
  117. /// Will be forced to <see langword="true"/> if <see cref="ConsoleDriver.SupportsTrueColor"/> is
  118. /// <see langword="false"/>, indicating that the <see cref="ConsoleDriver"/> cannot support TrueColor.
  119. /// </para>
  120. /// </remarks>
  121. public bool Force16Colors
  122. {
  123. get => Application.Force16Colors || !SupportsTrueColor;
  124. set => Application.Force16Colors = value || !SupportsTrueColor;
  125. }
  126. /// <summary>
  127. /// The <see cref="Attribute"/> that will be used for the next <see cref="AddRune(Rune)"/> or <see cref="AddStr"/>
  128. /// call.
  129. /// </summary>
  130. public Attribute CurrentAttribute
  131. {
  132. get => _outputBuffer.CurrentAttribute;
  133. set => _outputBuffer.CurrentAttribute = value;
  134. }
  135. /// <summary>Adds the specified rune to the display at the current cursor position.</summary>
  136. /// <remarks>
  137. /// <para>
  138. /// When the method returns, <see cref="ConsoleDriver.Col"/> will be incremented by the number of columns
  139. /// <paramref name="rune"/> required, even if the new column value is outside of the
  140. /// <see cref="ConsoleDriver.Clip"/> or screen
  141. /// dimensions defined by <see cref="ConsoleDriver.Cols"/>.
  142. /// </para>
  143. /// <para>
  144. /// If <paramref name="rune"/> requires more than one column, and <see cref="ConsoleDriver.Col"/> plus the number
  145. /// of columns
  146. /// needed exceeds the <see cref="ConsoleDriver.Clip"/> or screen dimensions, the default Unicode replacement
  147. /// character (U+FFFD)
  148. /// will be added instead.
  149. /// </para>
  150. /// </remarks>
  151. /// <param name="rune">Rune to add.</param>
  152. public void AddRune (Rune rune) { _outputBuffer.AddRune (rune); }
  153. /// <summary>
  154. /// Adds the specified <see langword="char"/> to the display at the current cursor position. This method is a
  155. /// convenience method that calls <see cref="ConsoleDriver.AddRune(System.Text.Rune)"/> with the <see cref="Rune"/>
  156. /// constructor.
  157. /// </summary>
  158. /// <param name="c">Character to add.</param>
  159. public void AddRune (char c) { _outputBuffer.AddRune (c); }
  160. /// <summary>Adds the <paramref name="str"/> to the display at the cursor position.</summary>
  161. /// <remarks>
  162. /// <para>
  163. /// When the method returns, <see cref="ConsoleDriver.Col"/> will be incremented by the number of columns
  164. /// <paramref name="str"/> required, unless the new column value is outside of the <see cref="ConsoleDriver.Clip"/>
  165. /// or screen
  166. /// dimensions defined by <see cref="ConsoleDriver.Cols"/>.
  167. /// </para>
  168. /// <para>If <paramref name="str"/> requires more columns than are available, the output will be clipped.</para>
  169. /// </remarks>
  170. /// <param name="str">String.</param>
  171. public void AddStr (string str) { _outputBuffer.AddStr (str); }
  172. /// <summary>Clears the <see cref="ConsoleDriver.Contents"/> of the driver.</summary>
  173. public void ClearContents ()
  174. {
  175. _outputBuffer.ClearContents ();
  176. ClearedContents?.Invoke (this, new MouseEventArgs ());
  177. }
  178. /// <summary>
  179. /// Raised each time <see cref="ConsoleDriver.ClearContents"/> is called. For benchmarking.
  180. /// </summary>
  181. public event EventHandler<EventArgs> ClearedContents;
  182. /// <summary>
  183. /// Fills the specified rectangle with the specified rune, using <see cref="ConsoleDriver.CurrentAttribute"/>
  184. /// </summary>
  185. /// <remarks>
  186. /// The value of <see cref="ConsoleDriver.Clip"/> is honored. Any parts of the rectangle not in the clip will not be
  187. /// drawn.
  188. /// </remarks>
  189. /// <param name="rect">The Screen-relative rectangle.</param>
  190. /// <param name="rune">The Rune used to fill the rectangle</param>
  191. public void FillRect (Rectangle rect, Rune rune = default) { _outputBuffer.FillRect (rect, rune); }
  192. /// <summary>
  193. /// Fills the specified rectangle with the specified <see langword="char"/>. This method is a convenience method
  194. /// that calls <see cref="ConsoleDriver.FillRect(System.Drawing.Rectangle,System.Text.Rune)"/>.
  195. /// </summary>
  196. /// <param name="rect"></param>
  197. /// <param name="c"></param>
  198. public void FillRect (Rectangle rect, char c) { _outputBuffer.FillRect (rect, c); }
  199. /// <inheritdoc/>
  200. public virtual string GetVersionInfo ()
  201. {
  202. var type = "";
  203. if (InputProcessor is WindowsInputProcessor)
  204. {
  205. type = "win";
  206. }
  207. else if (InputProcessor is NetInputProcessor)
  208. {
  209. type = "net";
  210. }
  211. return "v2" + type;
  212. }
  213. /// <summary>Tests if the specified rune is supported by the driver.</summary>
  214. /// <param name="rune"></param>
  215. /// <returns>
  216. /// <see langword="true"/> if the rune can be properly presented; <see langword="false"/> if the driver does not
  217. /// support displaying this rune.
  218. /// </returns>
  219. public bool IsRuneSupported (Rune rune) { return Rune.IsValid (rune.Value); }
  220. /// <summary>Tests whether the specified coordinate are valid for drawing the specified Rune.</summary>
  221. /// <param name="rune">Used to determine if one or two columns are required.</param>
  222. /// <param name="col">The column.</param>
  223. /// <param name="row">The row.</param>
  224. /// <returns>
  225. /// <see langword="false"/> if the coordinate is outside the screen bounds or outside of
  226. /// <see cref="ConsoleDriver.Clip"/>.
  227. /// <see langword="true"/> otherwise.
  228. /// </returns>
  229. public bool IsValidLocation (Rune rune, int col, int row) { return _outputBuffer.IsValidLocation (rune, col, row); }
  230. /// <summary>
  231. /// Updates <see cref="ConsoleDriver.Col"/> and <see cref="ConsoleDriver.Row"/> to the specified column and row in
  232. /// <see cref="ConsoleDriver.Contents"/>.
  233. /// Used by <see cref="ConsoleDriver.AddRune(System.Text.Rune)"/> and <see cref="ConsoleDriver.AddStr"/> to determine
  234. /// where to add content.
  235. /// </summary>
  236. /// <remarks>
  237. /// <para>This does not move the cursor on the screen, it only updates the internal state of the driver.</para>
  238. /// <para>
  239. /// If <paramref name="col"/> or <paramref name="row"/> are negative or beyond <see cref="ConsoleDriver.Cols"/>
  240. /// and
  241. /// <see cref="ConsoleDriver.Rows"/>, the method still sets those properties.
  242. /// </para>
  243. /// </remarks>
  244. /// <param name="col">Column to move to.</param>
  245. /// <param name="row">Row to move to.</param>
  246. public void Move (int col, int row) { _outputBuffer.Move (col, row); }
  247. // TODO: Probably part of output
  248. /// <summary>Sets the terminal cursor visibility.</summary>
  249. /// <param name="visibility">The wished <see cref="CursorVisibility"/></param>
  250. /// <returns><see langword="true"/> upon success</returns>
  251. public bool SetCursorVisibility (CursorVisibility visibility)
  252. {
  253. _lastCursor = visibility;
  254. _output.SetCursorVisibility (visibility);
  255. return true;
  256. }
  257. /// <inheritdoc/>
  258. public bool GetCursorVisibility (out CursorVisibility current)
  259. {
  260. current = _lastCursor;
  261. return true;
  262. }
  263. /// <inheritdoc/>
  264. public void Suspend () { }
  265. /// <summary>
  266. /// Sets the position of the terminal cursor to <see cref="ConsoleDriver.Col"/> and
  267. /// <see cref="ConsoleDriver.Row"/>.
  268. /// </summary>
  269. public void UpdateCursor () { _output.SetCursorPosition (Col, Row); }
  270. /// <summary>Initializes the driver</summary>
  271. /// <returns>Returns an instance of <see cref="MainLoop"/> using the <see cref="IMainLoopDriver"/> for the driver.</returns>
  272. public MainLoop Init () { throw new NotSupportedException (); }
  273. /// <summary>Ends the execution of the console driver.</summary>
  274. public void End ()
  275. {
  276. // TODO: Nope
  277. }
  278. /// <summary>Selects the specified attribute as the attribute to use for future calls to AddRune and AddString.</summary>
  279. /// <remarks>Implementations should call <c>base.SetAttribute(c)</c>.</remarks>
  280. /// <param name="c">C.</param>
  281. public Attribute SetAttribute (Attribute c) { return _outputBuffer.CurrentAttribute = c; }
  282. /// <summary>Gets the current <see cref="Attribute"/>.</summary>
  283. /// <returns>The current attribute.</returns>
  284. public Attribute GetAttribute () { return _outputBuffer.CurrentAttribute; }
  285. /// <summary>Makes an <see cref="Attribute"/>.</summary>
  286. /// <param name="foreground">The foreground color.</param>
  287. /// <param name="background">The background color.</param>
  288. /// <returns>The attribute for the foreground and background colors.</returns>
  289. public Attribute MakeColor (in Color foreground, in Color background)
  290. {
  291. // TODO: what even is this? why Attribute constructor wants to call Driver method which must return an instance of Attribute? ?!?!?!
  292. return new (
  293. -1, // only used by cursesdriver!
  294. foreground,
  295. background
  296. );
  297. }
  298. /// <summary>Event fired when a key is pressed down. This is a precursor to <see cref="ConsoleDriver.KeyUp"/>.</summary>
  299. public event EventHandler<Key> KeyDown;
  300. /// <summary>Event fired when a key is released.</summary>
  301. /// <remarks>
  302. /// Drivers that do not support key release events will fire this event after <see cref="ConsoleDriver.KeyDown"/>
  303. /// processing is
  304. /// complete.
  305. /// </remarks>
  306. public event EventHandler<Key> KeyUp;
  307. /// <summary>Event fired when a mouse event occurs.</summary>
  308. public event EventHandler<MouseEventArgs> MouseEvent;
  309. /// <summary>Simulates a key press.</summary>
  310. /// <param name="keyChar">The key character.</param>
  311. /// <param name="key">The key.</param>
  312. /// <param name="shift">If <see langword="true"/> simulates the Shift key being pressed.</param>
  313. /// <param name="alt">If <see langword="true"/> simulates the Alt key being pressed.</param>
  314. /// <param name="ctrl">If <see langword="true"/> simulates the Ctrl key being pressed.</param>
  315. public void SendKeys (char keyChar, ConsoleKey key, bool shift, bool alt, bool ctrl)
  316. {
  317. // TODO: implement
  318. }
  319. /// <summary>
  320. /// Provide proper writing to send escape sequence recognized by the <see cref="ConsoleDriver"/>.
  321. /// </summary>
  322. /// <param name="ansi"></param>
  323. public void WriteRaw (string ansi) { _output.Write (ansi); }
  324. /// <summary>
  325. /// Queues the given <paramref name="request"/> for execution
  326. /// </summary>
  327. /// <param name="request"></param>
  328. public void QueueAnsiRequest (AnsiEscapeSequenceRequest request) { _ansiRequestScheduler.SendOrSchedule (request); }
  329. public AnsiRequestScheduler GetRequestScheduler () { return _ansiRequestScheduler; }
  330. /// <inheritdoc/>
  331. public void Refresh ()
  332. {
  333. // No need we will always draw when dirty
  334. }
  335. }