ConsoleDriverFacade.cs 16 KB

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