ConsoleDriver.cs 34 KB

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