ConsoleDriver.cs 34 KB

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