ConsoleDriver.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. //
  2. // ConsoleDriver.cs: Base class for Terminal.Gui ConsoleDriver implementations.
  3. //
  4. using System.Diagnostics;
  5. namespace Terminal.Gui;
  6. /// <summary>Base class for Terminal.Gui ConsoleDriver implementations.</summary>
  7. /// <remarks>
  8. /// There are currently four implementations: - <see cref="CursesDriver"/> (for Unix and Mac) -
  9. /// <see cref="WindowsDriver"/> - <see cref="NetDriver"/> that uses the .NET Console API - <see cref="FakeConsole"/>
  10. /// for unit testing.
  11. /// </remarks>
  12. public abstract class ConsoleDriver
  13. {
  14. // As performance is a concern, we keep track of the dirty lines and only refresh those.
  15. // This is in addition to the dirty flag on each cell.
  16. internal bool [] _dirtyLines;
  17. // QUESTION: When non-full screen apps are supported, will this represent the app size, or will that be in Application?
  18. /// <summary>Gets the location and size of the terminal screen.</summary>
  19. internal Rectangle Screen => new (0, 0, Cols, Rows);
  20. private Rectangle _clip;
  21. /// <summary>
  22. /// Gets or sets the clip rectangle that <see cref="AddRune(Rune)"/> and <see cref="AddStr(string)"/> are subject
  23. /// to.
  24. /// </summary>
  25. /// <value>The rectangle describing the of <see cref="Clip"/> region.</value>
  26. public Rectangle Clip
  27. {
  28. get => _clip;
  29. set
  30. {
  31. if (_clip == value)
  32. {
  33. return;
  34. }
  35. // Don't ever let Clip be bigger than Screen
  36. _clip = Rectangle.Intersect (Screen, value);
  37. }
  38. }
  39. /// <summary>Get the operating system clipboard.</summary>
  40. public IClipboard Clipboard { get; internal set; }
  41. /// <summary>
  42. /// Gets the column last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  43. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  44. /// </summary>
  45. public int Col { get; internal set; }
  46. /// <summary>The number of columns visible in the terminal.</summary>
  47. public virtual int Cols
  48. {
  49. get => _cols;
  50. internal set
  51. {
  52. _cols = value;
  53. ClearContents ();
  54. }
  55. }
  56. /// <summary>
  57. /// The contents of the application output. The driver outputs this buffer to the terminal when
  58. /// <see cref="UpdateScreen"/> is called.
  59. /// <remarks>The format of the array is rows, columns. The first index is the row, the second index is the column.</remarks>
  60. /// </summary>
  61. public Cell [,] Contents { get; internal set; }
  62. /// <summary>The leftmost column in the terminal.</summary>
  63. public virtual int Left { get; internal set; } = 0;
  64. /// <summary>
  65. /// Gets the row 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 Row { get; internal set; }
  69. /// <summary>The number of rows visible in the terminal.</summary>
  70. public virtual int Rows
  71. {
  72. get => _rows;
  73. internal set
  74. {
  75. _rows = value;
  76. ClearContents ();
  77. }
  78. }
  79. /// <summary>The topmost row in the terminal.</summary>
  80. public virtual int Top { get; internal set; } = 0;
  81. /// <summary>
  82. /// Set this to true in any unit tests that attempt to test drivers other than FakeDriver.
  83. /// <code>
  84. /// public ColorTests ()
  85. /// {
  86. /// ConsoleDriver.RunningUnitTests = true;
  87. /// }
  88. /// </code>
  89. /// </summary>
  90. internal static bool RunningUnitTests { get; set; }
  91. /// <summary>Adds the specified rune to the display at the current cursor position.</summary>
  92. /// <remarks>
  93. /// <para>
  94. /// When the method returns, <see cref="Col"/> will be incremented by the number of columns
  95. /// <paramref name="rune"/> required, even if the new column value is outside of the <see cref="Clip"/> or screen
  96. /// dimensions defined by <see cref="Cols"/>.
  97. /// </para>
  98. /// <para>
  99. /// If <paramref name="rune"/> requires more than one column, and <see cref="Col"/> plus the number of columns
  100. /// needed exceeds the <see cref="Clip"/> or screen dimensions, the default Unicode replacement character (U+FFFD)
  101. /// will be added instead.
  102. /// </para>
  103. /// </remarks>
  104. /// <param name="rune">Rune to add.</param>
  105. public void AddRune (Rune rune)
  106. {
  107. int runeWidth = -1;
  108. bool validLocation = IsValidLocation (Col, Row);
  109. if (validLocation)
  110. {
  111. rune = rune.MakePrintable ();
  112. runeWidth = rune.GetColumns ();
  113. if (runeWidth == 0 && rune.IsCombiningMark ())
  114. {
  115. // AtlasEngine does not support NON-NORMALIZED combining marks in a way
  116. // compatible with the driver architecture. Any CMs (except in the first col)
  117. // are correctly combined with the base char, but are ALSO treated as 1 column
  118. // width codepoints E.g. `echo "[e`u{0301}`u{0301}]"` will output `[é ]`.
  119. //
  120. // Until this is addressed (see Issue #), we do our best by
  121. // a) Attempting to normalize any CM with the base char to it's left
  122. // b) Ignoring any CMs that don't normalize
  123. if (Col > 0)
  124. {
  125. if (Contents [Row, Col - 1].CombiningMarks.Count > 0)
  126. {
  127. // Just add this mark to the list
  128. Contents [Row, Col - 1].CombiningMarks.Add (rune);
  129. // Ignore. Don't move to next column (let the driver figure out what to do).
  130. }
  131. else
  132. {
  133. // Attempt to normalize the cell to our left combined with this mark
  134. string combined = Contents [Row, Col - 1].Rune + rune.ToString ();
  135. // Normalize to Form C (Canonical Composition)
  136. string normalized = combined.Normalize (NormalizationForm.FormC);
  137. if (normalized.Length == 1)
  138. {
  139. // It normalized! We can just set the Cell to the left with the
  140. // normalized codepoint
  141. Contents [Row, Col - 1].Rune = (Rune)normalized [0];
  142. // Ignore. Don't move to next column because we're already there
  143. }
  144. else
  145. {
  146. // It didn't normalize. Add it to the Cell to left's CM list
  147. Contents [Row, Col - 1].CombiningMarks.Add (rune);
  148. // Ignore. Don't move to next column (let the driver figure out what to do).
  149. }
  150. }
  151. Contents [Row, Col - 1].Attribute = CurrentAttribute;
  152. Contents [Row, Col - 1].IsDirty = true;
  153. }
  154. else
  155. {
  156. // Most drivers will render a combining mark at col 0 as the mark
  157. Contents [Row, Col].Rune = rune;
  158. Contents [Row, Col].Attribute = CurrentAttribute;
  159. Contents [Row, Col].IsDirty = true;
  160. Col++;
  161. }
  162. }
  163. else
  164. {
  165. Contents [Row, Col].Attribute = CurrentAttribute;
  166. Contents [Row, Col].IsDirty = true;
  167. if (Col > 0)
  168. {
  169. // Check if cell to left has a wide glyph
  170. if (Contents [Row, Col - 1].Rune.GetColumns () > 1)
  171. {
  172. // Invalidate cell to left
  173. Contents [Row, Col - 1].Rune = Rune.ReplacementChar;
  174. Contents [Row, Col - 1].IsDirty = true;
  175. }
  176. }
  177. if (runeWidth < 1)
  178. {
  179. Contents [Row, Col].Rune = Rune.ReplacementChar;
  180. }
  181. else if (runeWidth == 1)
  182. {
  183. Contents [Row, Col].Rune = rune;
  184. if (Col < Clip.Right - 1)
  185. {
  186. Contents [Row, Col + 1].IsDirty = true;
  187. }
  188. }
  189. else if (runeWidth == 2)
  190. {
  191. if (Col == Clip.Right - 1)
  192. {
  193. // We're at the right edge of the clip, so we can't display a wide character.
  194. // TODO: Figure out if it is better to show a replacement character or ' '
  195. Contents [Row, Col].Rune = Rune.ReplacementChar;
  196. }
  197. else
  198. {
  199. Contents [Row, Col].Rune = rune;
  200. if (Col < Clip.Right - 1)
  201. {
  202. // Invalidate cell to right so that it doesn't get drawn
  203. // TODO: Figure out if it is better to show a replacement character or ' '
  204. Contents [Row, Col + 1].Rune = Rune.ReplacementChar;
  205. Contents [Row, Col + 1].IsDirty = true;
  206. }
  207. }
  208. }
  209. else
  210. {
  211. // This is a non-spacing character, so we don't need to do anything
  212. Contents [Row, Col].Rune = (Rune)' ';
  213. Contents [Row, Col].IsDirty = false;
  214. }
  215. _dirtyLines [Row] = true;
  216. }
  217. }
  218. if (runeWidth is < 0 or > 0)
  219. {
  220. Col++;
  221. }
  222. if (runeWidth > 1)
  223. {
  224. Debug.Assert (runeWidth <= 2);
  225. if (validLocation && Col < Clip.Right)
  226. {
  227. // This is a double-width character, and we are not at the end of the line.
  228. // Col now points to the second column of the character. Ensure it doesn't
  229. // Get rendered.
  230. Contents [Row, Col].IsDirty = false;
  231. Contents [Row, Col].Attribute = CurrentAttribute;
  232. // TODO: Determine if we should wipe this out (for now now)
  233. //Contents [Row, Col].Rune = (Rune)' ';
  234. }
  235. Col++;
  236. }
  237. }
  238. /// <summary>
  239. /// Adds the specified <see langword="char"/> to the display at the current cursor position. This method is a
  240. /// convenience method that calls <see cref="AddRune(Rune)"/> with the <see cref="Rune"/> constructor.
  241. /// </summary>
  242. /// <param name="c">Character to add.</param>
  243. public void AddRune (char c) { AddRune (new Rune (c)); }
  244. /// <summary>Adds the <paramref name="str"/> to the display at the cursor position.</summary>
  245. /// <remarks>
  246. /// <para>
  247. /// When the method returns, <see cref="Col"/> will be incremented by the number of columns
  248. /// <paramref name="str"/> required, unless the new column value is outside of the <see cref="Clip"/> or screen
  249. /// dimensions defined by <see cref="Cols"/>.
  250. /// </para>
  251. /// <para>If <paramref name="str"/> requires more columns than are available, the output will be clipped.</para>
  252. /// </remarks>
  253. /// <param name="str">String.</param>
  254. public void AddStr (string str)
  255. {
  256. List<Rune> runes = str.EnumerateRunes ().ToList ();
  257. for (var i = 0; i < runes.Count; i++)
  258. {
  259. AddRune (runes [i]);
  260. }
  261. }
  262. /// <summary>Clears the <see cref="Contents"/> of the driver.</summary>
  263. public void ClearContents ()
  264. {
  265. Contents = new Cell [Rows, Cols];
  266. //CONCURRENCY: Unsynchronized access to Clip isn't safe.
  267. // TODO: ClearContents should not clear the clip; it should only clear the contents. Move clearing it elsewhere.
  268. Clip = Screen;
  269. _dirtyLines = new bool [Rows];
  270. lock (Contents)
  271. {
  272. for (var row = 0; row < Rows; row++)
  273. {
  274. for (var c = 0; c < Cols; c++)
  275. {
  276. Contents [row, c] = new Cell
  277. {
  278. Rune = (Rune)' ',
  279. Attribute = new Attribute (Color.White, Color.Black),
  280. IsDirty = true
  281. };
  282. }
  283. _dirtyLines [row] = true;
  284. }
  285. }
  286. }
  287. /// <summary>
  288. /// Sets <see cref="Contents"/> as dirty for situations where views
  289. /// don't need layout and redrawing, but just refresh the screen.
  290. /// </summary>
  291. public void SetContentsAsDirty ()
  292. {
  293. lock (Contents)
  294. {
  295. for (var row = 0; row < Rows; row++)
  296. {
  297. for (var c = 0; c < Cols; c++)
  298. {
  299. Contents [row, c].IsDirty = true;
  300. }
  301. _dirtyLines [row] = true;
  302. }
  303. }
  304. }
  305. /// <summary>Determines if the terminal cursor should be visible or not and sets it accordingly.</summary>
  306. /// <returns><see langword="true"/> upon success</returns>
  307. public abstract bool EnsureCursorVisibility ();
  308. /// <summary>Fills the specified rectangle with the specified rune, using <see cref="CurrentAttribute"/></summary>
  309. /// <remarks>
  310. /// The value of <see cref="Clip"/> is honored. Any parts of the rectangle not in the clip will not be drawn.
  311. /// </remarks>
  312. /// <param name="rect">The Screen-relative rectangle.</param>
  313. /// <param name="rune">The Rune used to fill the rectangle</param>
  314. public void FillRect (Rectangle rect, Rune rune = default)
  315. {
  316. rect = Rectangle.Intersect (rect, Clip);
  317. lock (Contents)
  318. {
  319. for (int r = rect.Y; r < rect.Y + rect.Height; r++)
  320. {
  321. for (int c = rect.X; c < rect.X + rect.Width; c++)
  322. {
  323. Contents [r, c] = new Cell
  324. {
  325. Rune = (rune != default ? rune : (Rune)' '),
  326. Attribute = CurrentAttribute, IsDirty = true
  327. };
  328. _dirtyLines [r] = true;
  329. }
  330. }
  331. }
  332. }
  333. /// <summary>
  334. /// Fills the specified rectangle with the specified <see langword="char"/>. This method is a convenience method
  335. /// that calls <see cref="FillRect(Rectangle, Rune)"/>.
  336. /// </summary>
  337. /// <param name="rect"></param>
  338. /// <param name="c"></param>
  339. public void FillRect (Rectangle rect, char c) { FillRect (rect, new Rune (c)); }
  340. /// <summary>Gets the terminal cursor visibility.</summary>
  341. /// <param name="visibility">The current <see cref="CursorVisibility"/></param>
  342. /// <returns><see langword="true"/> upon success</returns>
  343. public abstract bool GetCursorVisibility (out CursorVisibility visibility);
  344. /// <summary>Returns the name of the driver and relevant library version information.</summary>
  345. /// <returns></returns>
  346. public virtual string GetVersionInfo () { return GetType ().Name; }
  347. /// <summary>Tests if the specified rune is supported by the driver.</summary>
  348. /// <param name="rune"></param>
  349. /// <returns>
  350. /// <see langword="true"/> if the rune can be properly presented; <see langword="false"/> if the driver does not
  351. /// support displaying this rune.
  352. /// </returns>
  353. public virtual bool IsRuneSupported (Rune rune) { return Rune.IsValid (rune.Value); }
  354. /// <summary>Tests whether the specified coordinate are valid for drawing.</summary>
  355. /// <param name="col">The column.</param>
  356. /// <param name="row">The row.</param>
  357. /// <returns>
  358. /// <see langword="false"/> if the coordinate is outside the screen bounds or outside of <see cref="Clip"/>.
  359. /// <see langword="true"/> otherwise.
  360. /// </returns>
  361. public bool IsValidLocation (int col, int row)
  362. {
  363. return col >= 0 && row >= 0 && col < Cols && row < Rows && Clip.Contains (col, row);
  364. }
  365. /// <summary>
  366. /// Updates <see cref="Col"/> and <see cref="Row"/> to the specified column and row in <see cref="Contents"/>.
  367. /// Used by <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  368. /// </summary>
  369. /// <remarks>
  370. /// <para>This does not move the cursor on the screen, it only updates the internal state of the driver.</para>
  371. /// <para>
  372. /// If <paramref name="col"/> or <paramref name="row"/> are negative or beyond <see cref="Cols"/> and
  373. /// <see cref="Rows"/>, the method still sets those properties.
  374. /// </para>
  375. /// </remarks>
  376. /// <param name="col">Column to move to.</param>
  377. /// <param name="row">Row to move to.</param>
  378. public virtual void Move (int col, int row)
  379. {
  380. //Debug.Assert (col >= 0 && row >= 0 && col < Contents.GetLength(1) && row < Contents.GetLength(0));
  381. Col = col;
  382. Row = row;
  383. }
  384. /// <summary>Called when the terminal size changes. Fires the <see cref="SizeChanged"/> event.</summary>
  385. /// <param name="args"></param>
  386. public void OnSizeChanged (SizeChangedEventArgs args) { SizeChanged?.Invoke (this, args); }
  387. /// <summary>Updates the screen to reflect all the changes that have been done to the display buffer</summary>
  388. public abstract void Refresh ();
  389. /// <summary>Sets the terminal cursor visibility.</summary>
  390. /// <param name="visibility">The wished <see cref="CursorVisibility"/></param>
  391. /// <returns><see langword="true"/> upon success</returns>
  392. public abstract bool SetCursorVisibility (CursorVisibility visibility);
  393. /// <summary>The event fired when the terminal is resized.</summary>
  394. public event EventHandler<SizeChangedEventArgs> SizeChanged;
  395. /// <summary>Suspends the application (e.g. on Linux via SIGTSTP) and upon resume, resets the console driver.</summary>
  396. /// <remarks>This is only implemented in <see cref="CursesDriver"/>.</remarks>
  397. public abstract void Suspend ();
  398. /// <summary>Sets the position of the terminal cursor to <see cref="Col"/> and <see cref="Row"/>.</summary>
  399. public abstract void UpdateCursor ();
  400. /// <summary>Redraws the physical screen with the contents that have been queued up via any of the printing commands.</summary>
  401. public abstract void UpdateScreen ();
  402. #region Setup & Teardown
  403. /// <summary>Initializes the driver</summary>
  404. /// <returns>Returns an instance of <see cref="MainLoop"/> using the <see cref="IMainLoopDriver"/> for the driver.</returns>
  405. internal abstract MainLoop Init ();
  406. /// <summary>Ends the execution of the console driver.</summary>
  407. internal abstract void End ();
  408. #endregion
  409. #region Color Handling
  410. /// <summary>Gets whether the <see cref="ConsoleDriver"/> supports TrueColor output.</summary>
  411. public virtual bool SupportsTrueColor => true;
  412. // TODO: This makes ConsoleDriver dependent on Application, which is not ideal. This should be moved to Application.
  413. /// <summary>
  414. /// Gets or sets whether the <see cref="ConsoleDriver"/> should use 16 colors instead of the default TrueColors.
  415. /// See <see cref="Application.Force16Colors"/> to change this setting via <see cref="ConfigurationManager"/>.
  416. /// </summary>
  417. /// <remarks>
  418. /// <para>
  419. /// Will be forced to <see langword="true"/> if <see cref="ConsoleDriver.SupportsTrueColor"/> is
  420. /// <see langword="false"/>, indicating that the <see cref="ConsoleDriver"/> cannot support TrueColor.
  421. /// </para>
  422. /// </remarks>
  423. internal virtual bool Force16Colors
  424. {
  425. get => Application.Force16Colors || !SupportsTrueColor;
  426. set => Application.Force16Colors = value || !SupportsTrueColor;
  427. }
  428. private Attribute _currentAttribute;
  429. private int _cols;
  430. private int _rows;
  431. /// <summary>
  432. /// The <see cref="Attribute"/> that will be used for the next <see cref="AddRune(Rune)"/> or <see cref="AddStr"/>
  433. /// call.
  434. /// </summary>
  435. public Attribute CurrentAttribute
  436. {
  437. get => _currentAttribute;
  438. set
  439. {
  440. // TODO: This makes ConsoleDriver dependent on Application, which is not ideal. Once Attribute.PlatformColor is removed, this can be fixed.
  441. if (Application.Driver is { })
  442. {
  443. _currentAttribute = new Attribute (value.Foreground, value.Background);
  444. return;
  445. }
  446. _currentAttribute = value;
  447. }
  448. }
  449. /// <summary>Selects the specified attribute as the attribute to use for future calls to AddRune and AddString.</summary>
  450. /// <remarks>Implementations should call <c>base.SetAttribute(c)</c>.</remarks>
  451. /// <param name="c">C.</param>
  452. public Attribute SetAttribute (Attribute c)
  453. {
  454. Attribute prevAttribute = CurrentAttribute;
  455. CurrentAttribute = c;
  456. return prevAttribute;
  457. }
  458. /// <summary>Gets the current <see cref="Attribute"/>.</summary>
  459. /// <returns>The current attribute.</returns>
  460. public Attribute GetAttribute () { return CurrentAttribute; }
  461. // TODO: This is only overridden by CursesDriver. Once CursesDriver supports 24-bit color, this virtual method can be
  462. // removed (and Attribute can lose the platformColor property).
  463. /// <summary>Makes an <see cref="Attribute"/>.</summary>
  464. /// <param name="foreground">The foreground color.</param>
  465. /// <param name="background">The background color.</param>
  466. /// <returns>The attribute for the foreground and background colors.</returns>
  467. public virtual Attribute MakeColor (in Color foreground, in Color background)
  468. {
  469. // Encode the colors into the int value.
  470. return new Attribute (
  471. -1, // only used by cursesdriver!
  472. foreground,
  473. background
  474. );
  475. }
  476. #endregion
  477. #region Mouse and Keyboard
  478. /// <summary>Event fired when a key is pressed down. This is a precursor to <see cref="KeyUp"/>.</summary>
  479. public event EventHandler<Key> KeyDown;
  480. /// <summary>
  481. /// Called when a key is pressed down. Fires the <see cref="KeyDown"/> event. This is a precursor to
  482. /// <see cref="OnKeyUp"/>.
  483. /// </summary>
  484. /// <param name="a"></param>
  485. public void OnKeyDown (Key a) { KeyDown?.Invoke (this, a); }
  486. /// <summary>Event fired when a key is released.</summary>
  487. /// <remarks>
  488. /// Drivers that do not support key release events will fire this event after <see cref="KeyDown"/> processing is
  489. /// complete.
  490. /// </remarks>
  491. public event EventHandler<Key> KeyUp;
  492. /// <summary>Called when a key is released. Fires the <see cref="KeyUp"/> event.</summary>
  493. /// <remarks>
  494. /// Drivers that do not support key release events will calls this method after <see cref="OnKeyDown"/> processing
  495. /// is complete.
  496. /// </remarks>
  497. /// <param name="a"></param>
  498. public void OnKeyUp (Key a) { KeyUp?.Invoke (this, a); }
  499. /// <summary>Event fired when a mouse event occurs.</summary>
  500. public event EventHandler<MouseEvent> MouseEvent;
  501. /// <summary>Called when a mouse event occurs. Fires the <see cref="MouseEvent"/> event.</summary>
  502. /// <param name="a"></param>
  503. public void OnMouseEvent (MouseEvent a) { MouseEvent?.Invoke (this, a); }
  504. /// <summary>Simulates a key press.</summary>
  505. /// <param name="keyChar">The key character.</param>
  506. /// <param name="key">The key.</param>
  507. /// <param name="shift">If <see langword="true"/> simulates the Shift key being pressed.</param>
  508. /// <param name="alt">If <see langword="true"/> simulates the Alt key being pressed.</param>
  509. /// <param name="ctrl">If <see langword="true"/> simulates the Ctrl key being pressed.</param>
  510. public abstract void SendKeys (char keyChar, ConsoleKey key, bool shift, bool alt, bool ctrl);
  511. #endregion
  512. }
  513. /// <summary>Terminal Cursor Visibility settings.</summary>
  514. /// <remarks>
  515. /// Hex value are set as 0xAABBCCDD where : AA stand for the TERMINFO DECSUSR parameter value to be used under
  516. /// Linux and MacOS BB stand for the NCurses curs_set parameter value to be used under Linux and MacOS CC stand for the
  517. /// CONSOLE_CURSOR_INFO.bVisible parameter value to be used under Windows DD stand for the CONSOLE_CURSOR_INFO.dwSize
  518. /// parameter value to be used under Windows
  519. /// </remarks>
  520. public enum CursorVisibility
  521. {
  522. /// <summary>Cursor caret has default</summary>
  523. /// <remarks>
  524. /// Works under Xterm-like terminal otherwise this is equivalent to <see ref="Underscore"/>. This default directly
  525. /// depends of the XTerm user configuration settings so it could be Block, I-Beam, Underline with possible blinking.
  526. /// </remarks>
  527. Default = 0x00010119,
  528. /// <summary>Cursor caret is hidden</summary>
  529. Invisible = 0x03000019,
  530. /// <summary>Cursor caret is normally shown as a blinking underline bar _</summary>
  531. Underline = 0x03010119,
  532. /// <summary>Cursor caret is normally shown as a underline bar _</summary>
  533. /// <remarks>Under Windows, this is equivalent to <see ref="UnderscoreBlinking"/></remarks>
  534. UnderlineFix = 0x04010119,
  535. /// <summary>Cursor caret is displayed a blinking vertical bar |</summary>
  536. /// <remarks>Works under Xterm-like terminal otherwise this is equivalent to <see ref="Underscore"/></remarks>
  537. Vertical = 0x05010119,
  538. /// <summary>Cursor caret is displayed a blinking vertical bar |</summary>
  539. /// <remarks>Works under Xterm-like terminal otherwise this is equivalent to <see ref="Underscore"/></remarks>
  540. VerticalFix = 0x06010119,
  541. /// <summary>Cursor caret is displayed as a blinking block ▉</summary>
  542. Box = 0x01020164,
  543. /// <summary>Cursor caret is displayed a block ▉</summary>
  544. /// <remarks>Works under Xterm-like terminal otherwise this is equivalent to <see ref="Block"/></remarks>
  545. BoxFix = 0x02020164
  546. }
  547. /// <summary>
  548. /// The <see cref="KeyCode"/> enumeration encodes key information from <see cref="ConsoleDriver"/>s and provides a
  549. /// consistent way for application code to specify keys and receive key events.
  550. /// <para>
  551. /// The <see cref="Key"/> class provides a higher-level abstraction, with helper methods and properties for
  552. /// common operations. For example, <see cref="Key.IsAlt"/> and <see cref="Key.IsCtrl"/> provide a convenient way
  553. /// to check whether the Alt or Ctrl modifier keys were pressed when a key was pressed.
  554. /// </para>
  555. /// </summary>
  556. /// <remarks>
  557. /// <para>
  558. /// Lowercase alpha keys are encoded as values between 65 and 90 corresponding to the un-shifted A to Z keys on a
  559. /// keyboard. Enum values are provided for these (e.g. <see cref="KeyCode.A"/>, <see cref="KeyCode.B"/>, etc.).
  560. /// Even though the values are the same as the ASCII values for uppercase characters, these enum values represent
  561. /// *lowercase*, un-shifted characters.
  562. /// </para>
  563. /// <para>
  564. /// Numeric keys are the values between 48 and 57 corresponding to 0 to 9 (e.g. <see cref="KeyCode.D0"/>,
  565. /// <see cref="KeyCode.D1"/>, etc.).
  566. /// </para>
  567. /// <para>
  568. /// The shift modifiers (<see cref="KeyCode.ShiftMask"/>, <see cref="KeyCode.CtrlMask"/>, and
  569. /// <see cref="KeyCode.AltMask"/>) can be combined (with logical or) with the other key codes to represent shifted
  570. /// keys. For example, the <see cref="KeyCode.A"/> enum value represents the un-shifted 'a' key, while
  571. /// <see cref="KeyCode.ShiftMask"/> | <see cref="KeyCode.A"/> represents the 'A' key (shifted 'a' key). Likewise,
  572. /// <see cref="KeyCode.AltMask"/> | <see cref="KeyCode.A"/> represents the 'Alt+A' key combination.
  573. /// </para>
  574. /// <para>
  575. /// All other keys that produce a printable character are encoded as the Unicode value of the character. For
  576. /// example, the <see cref="KeyCode"/> for the '!' character is 33, which is the Unicode value for '!'. Likewise,
  577. /// `â` is 226, `Â` is 194, etc.
  578. /// </para>
  579. /// <para>
  580. /// If the <see cref="SpecialMask"/> is set, then the value is that of the special mask, otherwise, the value is
  581. /// the one of the lower bits (as extracted by <see cref="CharMask"/>).
  582. /// </para>
  583. /// </remarks>
  584. [Flags]
  585. public enum KeyCode : uint
  586. {
  587. /// <summary>
  588. /// Mask that indicates that the key is a unicode codepoint. Values outside this range indicate the key has shift
  589. /// modifiers or is a special key like function keys, arrows keys and so on.
  590. /// </summary>
  591. CharMask = 0x_f_ffff,
  592. /// <summary>
  593. /// If the <see cref="SpecialMask"/> is set, then the value is that of the special mask, otherwise, the value is
  594. /// in the the lower bits (as extracted by <see cref="CharMask"/>).
  595. /// </summary>
  596. SpecialMask = 0x_fff0_0000,
  597. /// <summary>
  598. /// When this value is set, the Key encodes the sequence Shift-KeyValue. The actual value must be extracted by
  599. /// removing the ShiftMask.
  600. /// </summary>
  601. ShiftMask = 0x_1000_0000,
  602. /// <summary>
  603. /// When this value is set, the Key encodes the sequence Alt-KeyValue. The actual value must be extracted by
  604. /// removing the AltMask.
  605. /// </summary>
  606. AltMask = 0x_8000_0000,
  607. /// <summary>
  608. /// When this value is set, the Key encodes the sequence Ctrl-KeyValue. The actual value must be extracted by
  609. /// removing the CtrlMask.
  610. /// </summary>
  611. CtrlMask = 0x_4000_0000,
  612. /// <summary>The key code representing an invalid or empty key.</summary>
  613. Null = 0,
  614. /// <summary>Backspace key.</summary>
  615. Backspace = 8,
  616. /// <summary>The key code for the tab key (forwards tab key).</summary>
  617. Tab = 9,
  618. /// <summary>The key code for the return key.</summary>
  619. Enter = ConsoleKey.Enter,
  620. /// <summary>The key code for the clear key.</summary>
  621. Clear = 12,
  622. /// <summary>The key code for the escape key.</summary>
  623. Esc = 27,
  624. /// <summary>The key code for the space bar key.</summary>
  625. Space = 32,
  626. /// <summary>Digit 0.</summary>
  627. D0 = 48,
  628. /// <summary>Digit 1.</summary>
  629. D1,
  630. /// <summary>Digit 2.</summary>
  631. D2,
  632. /// <summary>Digit 3.</summary>
  633. D3,
  634. /// <summary>Digit 4.</summary>
  635. D4,
  636. /// <summary>Digit 5.</summary>
  637. D5,
  638. /// <summary>Digit 6.</summary>
  639. D6,
  640. /// <summary>Digit 7.</summary>
  641. D7,
  642. /// <summary>Digit 8.</summary>
  643. D8,
  644. /// <summary>Digit 9.</summary>
  645. D9,
  646. /// <summary>The key code for the A key</summary>
  647. A = 65,
  648. /// <summary>The key code for the B key</summary>
  649. B,
  650. /// <summary>The key code for the C key</summary>
  651. C,
  652. /// <summary>The key code for the D key</summary>
  653. D,
  654. /// <summary>The key code for the E key</summary>
  655. E,
  656. /// <summary>The key code for the F key</summary>
  657. F,
  658. /// <summary>The key code for the G key</summary>
  659. G,
  660. /// <summary>The key code for the H key</summary>
  661. H,
  662. /// <summary>The key code for the I key</summary>
  663. I,
  664. /// <summary>The key code for the J key</summary>
  665. J,
  666. /// <summary>The key code for the K key</summary>
  667. K,
  668. /// <summary>The key code for the L key</summary>
  669. L,
  670. /// <summary>The key code for the M key</summary>
  671. M,
  672. /// <summary>The key code for the N key</summary>
  673. N,
  674. /// <summary>The key code for the O key</summary>
  675. O,
  676. /// <summary>The key code for the P key</summary>
  677. P,
  678. /// <summary>The key code for the Q key</summary>
  679. Q,
  680. /// <summary>The key code for the R key</summary>
  681. R,
  682. /// <summary>The key code for the S key</summary>
  683. S,
  684. /// <summary>The key code for the T key</summary>
  685. T,
  686. /// <summary>The key code for the U key</summary>
  687. U,
  688. /// <summary>The key code for the V key</summary>
  689. V,
  690. /// <summary>The key code for the W key</summary>
  691. W,
  692. /// <summary>The key code for the X key</summary>
  693. X,
  694. /// <summary>The key code for the Y key</summary>
  695. Y,
  696. /// <summary>The key code for the Z key</summary>
  697. Z,
  698. ///// <summary>
  699. ///// The key code for the Delete key.
  700. ///// </summary>
  701. //Delete = 127,
  702. // --- Special keys ---
  703. // The values below are common non-alphanum keys. Their values are
  704. // based on the .NET ConsoleKey values, which, in-turn are based on the
  705. // VK_ values from the Windows API.
  706. // We add MaxCodePoint to avoid conflicts with the Unicode values.
  707. /// <summary>The maximum Unicode codepoint value. Used to encode the non-alphanumeric control keys.</summary>
  708. MaxCodePoint = 0x10FFFF,
  709. /// <summary>Cursor up key</summary>
  710. CursorUp = MaxCodePoint + ConsoleKey.UpArrow,
  711. /// <summary>Cursor down key.</summary>
  712. CursorDown = MaxCodePoint + ConsoleKey.DownArrow,
  713. /// <summary>Cursor left key.</summary>
  714. CursorLeft = MaxCodePoint + ConsoleKey.LeftArrow,
  715. /// <summary>Cursor right key.</summary>
  716. CursorRight = MaxCodePoint + ConsoleKey.RightArrow,
  717. /// <summary>Page Up key.</summary>
  718. PageUp = MaxCodePoint + ConsoleKey.PageUp,
  719. /// <summary>Page Down key.</summary>
  720. PageDown = MaxCodePoint + ConsoleKey.PageDown,
  721. /// <summary>Home key.</summary>
  722. Home = MaxCodePoint + ConsoleKey.Home,
  723. /// <summary>End key.</summary>
  724. End = MaxCodePoint + ConsoleKey.End,
  725. /// <summary>Insert (INS) key.</summary>
  726. Insert = MaxCodePoint + ConsoleKey.Insert,
  727. /// <summary>Delete (DEL) key.</summary>
  728. Delete = MaxCodePoint + ConsoleKey.Delete,
  729. /// <summary>Print screen character key.</summary>
  730. PrintScreen = MaxCodePoint + ConsoleKey.PrintScreen,
  731. /// <summary>F1 key.</summary>
  732. F1 = MaxCodePoint + ConsoleKey.F1,
  733. /// <summary>F2 key.</summary>
  734. F2 = MaxCodePoint + ConsoleKey.F2,
  735. /// <summary>F3 key.</summary>
  736. F3 = MaxCodePoint + ConsoleKey.F3,
  737. /// <summary>F4 key.</summary>
  738. F4 = MaxCodePoint + ConsoleKey.F4,
  739. /// <summary>F5 key.</summary>
  740. F5 = MaxCodePoint + ConsoleKey.F5,
  741. /// <summary>F6 key.</summary>
  742. F6 = MaxCodePoint + ConsoleKey.F6,
  743. /// <summary>F7 key.</summary>
  744. F7 = MaxCodePoint + ConsoleKey.F7,
  745. /// <summary>F8 key.</summary>
  746. F8 = MaxCodePoint + ConsoleKey.F8,
  747. /// <summary>F9 key.</summary>
  748. F9 = MaxCodePoint + ConsoleKey.F9,
  749. /// <summary>F10 key.</summary>
  750. F10 = MaxCodePoint + ConsoleKey.F10,
  751. /// <summary>F11 key.</summary>
  752. F11 = MaxCodePoint + ConsoleKey.F11,
  753. /// <summary>F12 key.</summary>
  754. F12 = MaxCodePoint + ConsoleKey.F12,
  755. /// <summary>F13 key.</summary>
  756. F13 = MaxCodePoint + ConsoleKey.F13,
  757. /// <summary>F14 key.</summary>
  758. F14 = MaxCodePoint + ConsoleKey.F14,
  759. /// <summary>F15 key.</summary>
  760. F15 = MaxCodePoint + ConsoleKey.F15,
  761. /// <summary>F16 key.</summary>
  762. F16 = MaxCodePoint + ConsoleKey.F16,
  763. /// <summary>F17 key.</summary>
  764. F17 = MaxCodePoint + ConsoleKey.F17,
  765. /// <summary>F18 key.</summary>
  766. F18 = MaxCodePoint + ConsoleKey.F18,
  767. /// <summary>F19 key.</summary>
  768. F19 = MaxCodePoint + ConsoleKey.F19,
  769. /// <summary>F20 key.</summary>
  770. F20 = MaxCodePoint + ConsoleKey.F20,
  771. /// <summary>F21 key.</summary>
  772. F21 = MaxCodePoint + ConsoleKey.F21,
  773. /// <summary>F22 key.</summary>
  774. F22 = MaxCodePoint + ConsoleKey.F22,
  775. /// <summary>F23 key.</summary>
  776. F23 = MaxCodePoint + ConsoleKey.F23,
  777. /// <summary>F24 key.</summary>
  778. F24 = MaxCodePoint + ConsoleKey.F24
  779. }