ConsoleDriver.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. //
  2. // ConsoleDriver.cs: Definition for the Console Driver API
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // Define this to enable diagnostics drawing for Window Frames
  8. using NStack;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Runtime.CompilerServices;
  13. namespace Terminal.Gui {
  14. /// <summary>
  15. /// Basic colors that can be used to set the foreground and background colors in console applications.
  16. /// </summary>
  17. public enum Color {
  18. /// <summary>
  19. /// The black color.
  20. /// </summary>
  21. Black,
  22. /// <summary>
  23. /// The blue color.
  24. /// </summary>
  25. Blue,
  26. /// <summary>
  27. /// The green color.
  28. /// </summary>
  29. Green,
  30. /// <summary>
  31. /// The cyan color.
  32. /// </summary>
  33. Cyan,
  34. /// <summary>
  35. /// The red color.
  36. /// </summary>
  37. Red,
  38. /// <summary>
  39. /// The magenta color.
  40. /// </summary>
  41. Magenta,
  42. /// <summary>
  43. /// The brown color.
  44. /// </summary>
  45. Brown,
  46. /// <summary>
  47. /// The gray color.
  48. /// </summary>
  49. Gray,
  50. /// <summary>
  51. /// The dark gray color.
  52. /// </summary>
  53. DarkGray,
  54. /// <summary>
  55. /// The bright bBlue color.
  56. /// </summary>
  57. BrightBlue,
  58. /// <summary>
  59. /// The bright green color.
  60. /// </summary>
  61. BrightGreen,
  62. /// <summary>
  63. /// The bright cyan color.
  64. /// </summary>
  65. BrighCyan,
  66. /// <summary>
  67. /// The bright red color.
  68. /// </summary>
  69. BrightRed,
  70. /// <summary>
  71. /// The bright magenta color.
  72. /// </summary>
  73. BrightMagenta,
  74. /// <summary>
  75. /// The bright yellow color.
  76. /// </summary>
  77. BrightYellow,
  78. /// <summary>
  79. /// The White color.
  80. /// </summary>
  81. White
  82. }
  83. /// <summary>
  84. /// Attributes are used as elements that contain both a foreground and a background or platform specific features
  85. /// </summary>
  86. /// <remarks>
  87. /// <see cref="Attribute"/>s are needed to map colors to terminal capabilities that might lack colors, on color
  88. /// scenarios, they encode both the foreground and the background color and are used in the <see cref="ColorScheme"/>
  89. /// class to define color schemes that can be used in your application.
  90. /// </remarks>
  91. public struct Attribute {
  92. /// <summary>
  93. /// The color attribute value.
  94. /// </summary>
  95. public int Value { get; }
  96. /// <summary>
  97. /// The foreground color.
  98. /// </summary>
  99. public Color Foreground { get; }
  100. /// <summary>
  101. /// The background color.
  102. /// </summary>
  103. public Color Background { get; }
  104. /// <summary>
  105. /// Initializes a new instance of the <see cref="Attribute"/> struct.
  106. /// </summary>
  107. /// <param name="value">Value.</param>
  108. /// <param name="foreground">Foreground</param>
  109. /// <param name="background">Background</param>
  110. public Attribute (int value, Color foreground = new Color (), Color background = new Color ())
  111. {
  112. Value = value;
  113. Foreground = foreground;
  114. Background = background;
  115. }
  116. /// <summary>
  117. /// Initializes a new instance of the <see cref="Attribute"/> struct.
  118. /// </summary>
  119. /// <param name="foreground">Foreground</param>
  120. /// <param name="background">Background</param>
  121. public Attribute (Color foreground = new Color (), Color background = new Color ())
  122. {
  123. Value = (int)foreground | ((int)background << 4);
  124. Foreground = foreground;
  125. Background = background;
  126. }
  127. /// <summary>
  128. /// Implicit conversion from an <see cref="Attribute"/> to the underlying Int32 representation
  129. /// </summary>
  130. /// <returns>The integer value stored in the attribute.</returns>
  131. /// <param name="c">The attribute to convert</param>
  132. public static implicit operator int (Attribute c) => c.Value;
  133. /// <summary>
  134. /// Implicitly convert an integer value into an <see cref="Attribute"/>
  135. /// </summary>
  136. /// <returns>An attribute with the specified integer value.</returns>
  137. /// <param name="v">value</param>
  138. public static implicit operator Attribute (int v) => new Attribute (v);
  139. /// <summary>
  140. /// Creates an <see cref="Attribute"/> from the specified foreground and background.
  141. /// </summary>
  142. /// <returns>The make.</returns>
  143. /// <param name="foreground">Foreground color to use.</param>
  144. /// <param name="background">Background color to use.</param>
  145. public static Attribute Make (Color foreground, Color background)
  146. {
  147. if (Application.Driver == null)
  148. throw new InvalidOperationException ("The Application has not been initialized");
  149. return Application.Driver.MakeAttribute (foreground, background);
  150. }
  151. /// <summary>
  152. /// Gets the current <see cref="Attribute"/> from the driver.
  153. /// </summary>
  154. /// <returns>The current attribute.</returns>
  155. public static Attribute Get ()
  156. {
  157. if (Application.Driver == null)
  158. throw new InvalidOperationException ("The Application has not been initialized");
  159. return Application.Driver.GetAttribute ();
  160. }
  161. }
  162. /// <summary>
  163. /// Color scheme definitions, they cover some common scenarios and are used
  164. /// typically in containers such as <see cref="Window"/> and <see cref="FrameView"/> to set the scheme that is used by all the
  165. /// views contained inside.
  166. /// </summary>
  167. public class ColorScheme : IEquatable<ColorScheme> {
  168. Attribute _normal;
  169. Attribute _focus;
  170. Attribute _hotNormal;
  171. Attribute _hotFocus;
  172. Attribute _disabled;
  173. internal string caller = "";
  174. /// <summary>
  175. /// The default color for text, when the view is not focused.
  176. /// </summary>
  177. public Attribute Normal { get { return _normal; } set { _normal = SetAttribute (value); } }
  178. /// <summary>
  179. /// The color for text when the view has the focus.
  180. /// </summary>
  181. public Attribute Focus { get { return _focus; } set { _focus = SetAttribute (value); } }
  182. /// <summary>
  183. /// The color for the hotkey when a view is not focused
  184. /// </summary>
  185. public Attribute HotNormal { get { return _hotNormal; } set { _hotNormal = SetAttribute (value); } }
  186. /// <summary>
  187. /// The color for the hotkey when the view is focused.
  188. /// </summary>
  189. public Attribute HotFocus { get { return _hotFocus; } set { _hotFocus = SetAttribute (value); } }
  190. /// <summary>
  191. /// The default color for text, when the view is disabled.
  192. /// </summary>
  193. public Attribute Disabled { get { return _disabled; } set { _disabled = SetAttribute (value); } }
  194. bool preparingScheme = false;
  195. Attribute SetAttribute (Attribute attribute, [CallerMemberName] string callerMemberName = null)
  196. {
  197. if (!Application._initialized && !preparingScheme)
  198. return attribute;
  199. if (preparingScheme)
  200. return attribute;
  201. preparingScheme = true;
  202. switch (caller) {
  203. case "TopLevel":
  204. switch (callerMemberName) {
  205. case "Normal":
  206. HotNormal = Application.Driver.MakeAttribute (HotNormal.Foreground, attribute.Background);
  207. break;
  208. case "Focus":
  209. HotFocus = Application.Driver.MakeAttribute (HotFocus.Foreground, attribute.Background);
  210. break;
  211. case "HotNormal":
  212. HotFocus = Application.Driver.MakeAttribute (attribute.Foreground, HotFocus.Background);
  213. break;
  214. case "HotFocus":
  215. HotNormal = Application.Driver.MakeAttribute (attribute.Foreground, HotNormal.Background);
  216. if (Focus.Foreground != attribute.Background)
  217. Focus = Application.Driver.MakeAttribute (Focus.Foreground, attribute.Background);
  218. break;
  219. }
  220. break;
  221. case "Base":
  222. switch (callerMemberName) {
  223. case "Normal":
  224. HotNormal = Application.Driver.MakeAttribute (HotNormal.Foreground, attribute.Background);
  225. break;
  226. case "Focus":
  227. HotFocus = Application.Driver.MakeAttribute (HotFocus.Foreground, attribute.Background);
  228. break;
  229. case "HotNormal":
  230. HotFocus = Application.Driver.MakeAttribute (attribute.Foreground, HotFocus.Background);
  231. Normal = Application.Driver.MakeAttribute (Normal.Foreground, attribute.Background);
  232. break;
  233. case "HotFocus":
  234. HotNormal = Application.Driver.MakeAttribute (attribute.Foreground, HotNormal.Background);
  235. if (Focus.Foreground != attribute.Background)
  236. Focus = Application.Driver.MakeAttribute (Focus.Foreground, attribute.Background);
  237. break;
  238. }
  239. break;
  240. case "Menu":
  241. switch (callerMemberName) {
  242. case "Normal":
  243. if (Focus.Background != attribute.Background)
  244. Focus = Application.Driver.MakeAttribute (attribute.Foreground, Focus.Background);
  245. HotNormal = Application.Driver.MakeAttribute (HotNormal.Foreground, attribute.Background);
  246. Disabled = Application.Driver.MakeAttribute (Disabled.Foreground, attribute.Background);
  247. break;
  248. case "Focus":
  249. Normal = Application.Driver.MakeAttribute (attribute.Foreground, Normal.Background);
  250. HotFocus = Application.Driver.MakeAttribute (HotFocus.Foreground, attribute.Background);
  251. break;
  252. case "HotNormal":
  253. if (Focus.Background != attribute.Background)
  254. HotFocus = Application.Driver.MakeAttribute (attribute.Foreground, HotFocus.Background);
  255. Normal = Application.Driver.MakeAttribute (Normal.Foreground, attribute.Background);
  256. Disabled = Application.Driver.MakeAttribute (Disabled.Foreground, attribute.Background);
  257. break;
  258. case "HotFocus":
  259. HotNormal = Application.Driver.MakeAttribute (attribute.Foreground, HotNormal.Background);
  260. if (Focus.Foreground != attribute.Background)
  261. Focus = Application.Driver.MakeAttribute (Focus.Foreground, attribute.Background);
  262. break;
  263. case "Disabled":
  264. if (Focus.Background != attribute.Background)
  265. HotFocus = Application.Driver.MakeAttribute (attribute.Foreground, HotFocus.Background);
  266. Normal = Application.Driver.MakeAttribute (Normal.Foreground, attribute.Background);
  267. HotNormal = Application.Driver.MakeAttribute (HotNormal.Foreground, attribute.Background);
  268. break;
  269. }
  270. break;
  271. case "Dialog":
  272. switch (callerMemberName) {
  273. case "Normal":
  274. if (Focus.Background != attribute.Background)
  275. Focus = Application.Driver.MakeAttribute (attribute.Foreground, Focus.Background);
  276. HotNormal = Application.Driver.MakeAttribute (HotNormal.Foreground, attribute.Background);
  277. break;
  278. case "Focus":
  279. Normal = Application.Driver.MakeAttribute (attribute.Foreground, Normal.Background);
  280. HotFocus = Application.Driver.MakeAttribute (HotFocus.Foreground, attribute.Background);
  281. break;
  282. case "HotNormal":
  283. if (Focus.Background != attribute.Background)
  284. HotFocus = Application.Driver.MakeAttribute (attribute.Foreground, HotFocus.Background);
  285. if (Normal.Foreground != attribute.Background)
  286. Normal = Application.Driver.MakeAttribute (Normal.Foreground, attribute.Background);
  287. break;
  288. case "HotFocus":
  289. HotNormal = Application.Driver.MakeAttribute (attribute.Foreground, HotNormal.Background);
  290. if (Focus.Foreground != attribute.Background)
  291. Focus = Application.Driver.MakeAttribute (Focus.Foreground, attribute.Background);
  292. break;
  293. }
  294. break;
  295. case "Error":
  296. switch (callerMemberName) {
  297. case "Normal":
  298. HotNormal = Application.Driver.MakeAttribute (HotNormal.Foreground, attribute.Background);
  299. HotFocus = Application.Driver.MakeAttribute (HotFocus.Foreground, attribute.Background);
  300. break;
  301. case "HotNormal":
  302. case "HotFocus":
  303. HotFocus = Application.Driver.MakeAttribute (attribute.Foreground, attribute.Background);
  304. Normal = Application.Driver.MakeAttribute (Normal.Foreground, attribute.Background);
  305. break;
  306. }
  307. break;
  308. }
  309. preparingScheme = false;
  310. return attribute;
  311. }
  312. /// <summary>
  313. /// Compares two <see cref="ColorScheme"/> objects for equality.
  314. /// </summary>
  315. /// <param name="obj"></param>
  316. /// <returns>true if the two objects are equal</returns>
  317. public override bool Equals (object obj)
  318. {
  319. return Equals (obj as ColorScheme);
  320. }
  321. /// <summary>
  322. /// Compares two <see cref="ColorScheme"/> objects for equality.
  323. /// </summary>
  324. /// <param name="other"></param>
  325. /// <returns>true if the two objects are equal</returns>
  326. public bool Equals (ColorScheme other)
  327. {
  328. return other != null &&
  329. EqualityComparer<Attribute>.Default.Equals (_normal, other._normal) &&
  330. EqualityComparer<Attribute>.Default.Equals (_focus, other._focus) &&
  331. EqualityComparer<Attribute>.Default.Equals (_hotNormal, other._hotNormal) &&
  332. EqualityComparer<Attribute>.Default.Equals (_hotFocus, other._hotFocus) &&
  333. EqualityComparer<Attribute>.Default.Equals (_disabled, other._disabled);
  334. }
  335. /// <summary>
  336. /// Returns a hashcode for this instance.
  337. /// </summary>
  338. /// <returns>hashcode for this instance</returns>
  339. public override int GetHashCode ()
  340. {
  341. int hashCode = -1242460230;
  342. hashCode = hashCode * -1521134295 + _normal.GetHashCode ();
  343. hashCode = hashCode * -1521134295 + _focus.GetHashCode ();
  344. hashCode = hashCode * -1521134295 + _hotNormal.GetHashCode ();
  345. hashCode = hashCode * -1521134295 + _hotFocus.GetHashCode ();
  346. hashCode = hashCode * -1521134295 + _disabled.GetHashCode ();
  347. return hashCode;
  348. }
  349. /// <summary>
  350. /// Compares two <see cref="ColorScheme"/> objects for equality.
  351. /// </summary>
  352. /// <param name="left"></param>
  353. /// <param name="right"></param>
  354. /// <returns><c>true</c> if the two objects are equivalent</returns>
  355. public static bool operator == (ColorScheme left, ColorScheme right)
  356. {
  357. return EqualityComparer<ColorScheme>.Default.Equals (left, right);
  358. }
  359. /// <summary>
  360. /// Compares two <see cref="ColorScheme"/> objects for inequality.
  361. /// </summary>
  362. /// <param name="left"></param>
  363. /// <param name="right"></param>
  364. /// <returns><c>true</c> if the two objects are not equivalent</returns>
  365. public static bool operator != (ColorScheme left, ColorScheme right)
  366. {
  367. return !(left == right);
  368. }
  369. }
  370. /// <summary>
  371. /// The default <see cref="ColorScheme"/>s for the application.
  372. /// </summary>
  373. public static class Colors {
  374. static Colors ()
  375. {
  376. // Use reflection to dynamically create the default set of ColorSchemes from the list defined
  377. // by the class.
  378. ColorSchemes = typeof (Colors).GetProperties ()
  379. .Where (p => p.PropertyType == typeof (ColorScheme))
  380. .Select (p => new KeyValuePair<string, ColorScheme> (p.Name, new ColorScheme ())) // (ColorScheme)p.GetValue (p)))
  381. .ToDictionary (t => t.Key, t => t.Value);
  382. }
  383. /// <summary>
  384. /// The application toplevel color scheme, for the default toplevel views.
  385. /// </summary>
  386. /// <remarks>
  387. /// <para>
  388. /// This API will be deprecated in the future. Use <see cref="Colors.ColorSchemes"/> instead (e.g. <c>edit.ColorScheme = Colors.ColorSchemes["TopLevel"];</c>
  389. /// </para>
  390. /// </remarks>
  391. public static ColorScheme TopLevel { get => GetColorScheme (); set => SetColorScheme (value); }
  392. /// <summary>
  393. /// The base color scheme, for the default toplevel views.
  394. /// </summary>
  395. /// <remarks>
  396. /// <para>
  397. /// This API will be deprecated in the future. Use <see cref="Colors.ColorSchemes"/> instead (e.g. <c>edit.ColorScheme = Colors.ColorSchemes["Base"];</c>
  398. /// </para>
  399. /// </remarks>
  400. public static ColorScheme Base { get => GetColorScheme (); set => SetColorScheme (value); }
  401. /// <summary>
  402. /// The dialog color scheme, for standard popup dialog boxes
  403. /// </summary>
  404. /// <remarks>
  405. /// <para>
  406. /// This API will be deprecated in the future. Use <see cref="Colors.ColorSchemes"/> instead (e.g. <c>edit.ColorScheme = Colors.ColorSchemes["Dialog"];</c>
  407. /// </para>
  408. /// </remarks>
  409. public static ColorScheme Dialog { get => GetColorScheme (); set => SetColorScheme (value); }
  410. /// <summary>
  411. /// The menu bar color
  412. /// </summary>
  413. /// <remarks>
  414. /// <para>
  415. /// This API will be deprecated in the future. Use <see cref="Colors.ColorSchemes"/> instead (e.g. <c>edit.ColorScheme = Colors.ColorSchemes["Menu"];</c>
  416. /// </para>
  417. /// </remarks>
  418. public static ColorScheme Menu { get => GetColorScheme (); set => SetColorScheme (value); }
  419. /// <summary>
  420. /// The color scheme for showing errors.
  421. /// </summary>
  422. /// <remarks>
  423. /// <para>
  424. /// This API will be deprecated in the future. Use <see cref="Colors.ColorSchemes"/> instead (e.g. <c>edit.ColorScheme = Colors.ColorSchemes["Error"];</c>
  425. /// </para>
  426. /// </remarks>
  427. public static ColorScheme Error { get => GetColorScheme (); set => SetColorScheme (value); }
  428. static ColorScheme GetColorScheme ([CallerMemberName] string callerMemberName = null)
  429. {
  430. return ColorSchemes [callerMemberName];
  431. }
  432. static void SetColorScheme (ColorScheme colorScheme, [CallerMemberName] string callerMemberName = null)
  433. {
  434. ColorSchemes [callerMemberName] = colorScheme;
  435. colorScheme.caller = callerMemberName;
  436. }
  437. /// <summary>
  438. /// Provides the defined <see cref="ColorScheme"/>s.
  439. /// </summary>
  440. public static Dictionary<string, ColorScheme> ColorSchemes { get; }
  441. }
  442. /// <summary>
  443. /// Cursors Visibility that are displayed
  444. /// </summary>
  445. //
  446. // Hexa value are set as 0xAABBCCDD where :
  447. //
  448. // AA stand for the TERMINFO DECSUSR parameter value to be used under Linux & MacOS
  449. // BB stand for the NCurses curs_set parameter value to be used under Linux & MacOS
  450. // CC stand for the CONSOLE_CURSOR_INFO.bVisible parameter value to be used under Windows
  451. // DD stand for the CONSOLE_CURSOR_INFO.dwSize parameter value to be used under Windows
  452. //
  453. public enum CursorVisibility {
  454. /// <summary>
  455. /// Cursor caret has default
  456. /// </summary>
  457. Default = 0x00010119,
  458. /// <summary>
  459. /// Cursor caret is hidden
  460. /// </summary>
  461. Invisible = 0x03000019,
  462. /// <summary>
  463. /// Cursor caret is normally shown as an _
  464. /// </summary>
  465. /// <remarks>Under Windows, this is equivalent to <see ref="UnderscoreBlinking"/></remarks>
  466. Underline = 0x04010019,
  467. /// <summary>
  468. /// Cursor caret is normally shown as an blinking _
  469. /// </summary>
  470. UnderlineBlinking = 0x03010019,
  471. /// <summary>
  472. /// Cursor caret is displayed a bar |
  473. /// </summary>
  474. /// <remarks>Works under Xterm-like terminal otherwise this is equivalent to <see ref="UnderscoreBlinking"/></remarks>
  475. Vertical = 0x06010019,
  476. /// <summary>
  477. /// Cursor caret is displayed a blinking bar |
  478. /// </summary>
  479. /// <remarks>Works under Xterm-like terminal otherwise this is equivalent to <see ref="UnderscoreBlinking"/></remarks>
  480. VerticalBlinking = 0x05010019,
  481. /// <summary>
  482. /// Cursor caret is displayed a block ▉
  483. /// </summary>
  484. /// <remarks>Works under Xterm-like terminal otherwise this is equivalent to <see ref="BlockBlinking"/></remarks>
  485. Box = 0x02020064,
  486. /// <summary>
  487. /// Cursor caret is displayed a block ▉
  488. /// </summary>
  489. BoxBlinking = 0x01020064,
  490. }
  491. ///// <summary>
  492. ///// Special characters that can be drawn with
  493. ///// </summary>
  494. //public enum SpecialChar {
  495. // /// <summary>
  496. // /// Horizontal line character.
  497. // /// </summary>
  498. // HLine,
  499. // /// <summary>
  500. // /// Vertical line character.
  501. // /// </summary>
  502. // VLine,
  503. // /// <summary>
  504. // /// Stipple pattern
  505. // /// </summary>
  506. // Stipple,
  507. // /// <summary>
  508. // /// Diamond character
  509. // /// </summary>
  510. // Diamond,
  511. // /// <summary>
  512. // /// Upper left corner
  513. // /// </summary>
  514. // ULCorner,
  515. // /// <summary>
  516. // /// Lower left corner
  517. // /// </summary>
  518. // LLCorner,
  519. // /// <summary>
  520. // /// Upper right corner
  521. // /// </summary>
  522. // URCorner,
  523. // /// <summary>
  524. // /// Lower right corner
  525. // /// </summary>
  526. // LRCorner,
  527. // /// <summary>
  528. // /// Left tee
  529. // /// </summary>
  530. // LeftTee,
  531. // /// <summary>
  532. // /// Right tee
  533. // /// </summary>
  534. // RightTee,
  535. // /// <summary>
  536. // /// Top tee
  537. // /// </summary>
  538. // TopTee,
  539. // /// <summary>
  540. // /// The bottom tee.
  541. // /// </summary>
  542. // BottomTee,
  543. //}
  544. /// <summary>
  545. /// ConsoleDriver is an abstract class that defines the requirements for a console driver.
  546. /// There are currently three implementations: <see cref="CursesDriver"/> (for Unix and Mac), <see cref="WindowsDriver"/>, and <see cref="NetDriver"/> that uses the .NET Console API.
  547. /// </summary>
  548. public abstract class ConsoleDriver {
  549. /// <summary>
  550. /// The handler fired when the terminal is resized.
  551. /// </summary>
  552. protected Action TerminalResized;
  553. /// <summary>
  554. /// The current number of columns in the terminal.
  555. /// </summary>
  556. public abstract int Cols { get; }
  557. /// <summary>
  558. /// The current number of rows in the terminal.
  559. /// </summary>
  560. public abstract int Rows { get; }
  561. /// <summary>
  562. /// The current top in the terminal.
  563. /// </summary>
  564. public abstract int Top { get; }
  565. /// <summary>
  566. /// If false height is measured by the window height and thus no scrolling.
  567. /// If true then height is measured by the buffer height, enabling scrolling.
  568. /// </summary>
  569. public abstract bool HeightAsBuffer { get; set; }
  570. /// <summary>
  571. /// Initializes the driver
  572. /// </summary>
  573. /// <param name="terminalResized">Method to invoke when the terminal is resized.</param>
  574. public abstract void Init (Action terminalResized);
  575. /// <summary>
  576. /// Moves the cursor to the specified column and row.
  577. /// </summary>
  578. /// <param name="col">Column to move the cursor to.</param>
  579. /// <param name="row">Row to move the cursor to.</param>
  580. public abstract void Move (int col, int row);
  581. /// <summary>
  582. /// Adds the specified rune to the display at the current cursor position
  583. /// </summary>
  584. /// <param name="rune">Rune to add.</param>
  585. public abstract void AddRune (Rune rune);
  586. /// <summary>
  587. /// Ensures a Rune is not a control character and can be displayed by translating characters below 0x20
  588. /// to equivalent, printable, Unicode chars.
  589. /// </summary>
  590. /// <param name="c">Rune to translate</param>
  591. /// <returns></returns>
  592. public static Rune MakePrintable (Rune c)
  593. {
  594. if (c <= 0x1F || (c >= 0x80 && c <= 0x9F)) {
  595. // ASCII (C0) control characters.
  596. // C1 control characters (https://www.aivosto.com/articles/control-characters.html#c1)
  597. return new Rune (c + 0x2400);
  598. } else {
  599. return c;
  600. }
  601. }
  602. /// <summary>
  603. /// Adds the specified
  604. /// </summary>
  605. /// <param name="str">String.</param>
  606. public abstract void AddStr (ustring str);
  607. /// <summary>
  608. /// Prepare the driver and set the key and mouse events handlers.
  609. /// </summary>
  610. /// <param name="mainLoop">The main loop.</param>
  611. /// <param name="keyHandler">The handler for ProcessKey</param>
  612. /// <param name="keyDownHandler">The handler for key down events</param>
  613. /// <param name="keyUpHandler">The handler for key up events</param>
  614. /// <param name="mouseHandler">The handler for mouse events</param>
  615. public abstract void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler);
  616. /// <summary>
  617. /// Updates the screen to reflect all the changes that have been done to the display buffer
  618. /// </summary>
  619. public abstract void Refresh ();
  620. /// <summary>
  621. /// Updates the location of the cursor position
  622. /// </summary>
  623. public abstract void UpdateCursor ();
  624. /// <summary>
  625. /// Retreive the cursor caret visibility
  626. /// </summary>
  627. /// <param name="visibility">The current <see cref="CursorVisibility"/></param>
  628. /// <returns>true upon success</returns>
  629. public abstract bool GetCursorVisibility (out CursorVisibility visibility);
  630. /// <summary>
  631. /// Change the cursor caret visibility
  632. /// </summary>
  633. /// <param name="visibility">The wished <see cref="CursorVisibility"/></param>
  634. /// <returns>true upon success</returns>
  635. public abstract bool SetCursorVisibility (CursorVisibility visibility);
  636. /// <summary>
  637. /// Ensure the cursor visibility
  638. /// </summary>
  639. /// <returns>true upon success</returns>
  640. public abstract bool EnsureCursorVisibility ();
  641. /// <summary>
  642. /// Ends the execution of the console driver.
  643. /// </summary>
  644. public abstract void End ();
  645. /// <summary>
  646. /// Redraws the physical screen with the contents that have been queued up via any of the printing commands.
  647. /// </summary>
  648. public abstract void UpdateScreen ();
  649. /// <summary>
  650. /// Selects the specified attribute as the attribute to use for future calls to AddRune, AddString.
  651. /// </summary>
  652. /// <param name="c">C.</param>
  653. public abstract void SetAttribute (Attribute c);
  654. /// <summary>
  655. /// Set Colors from limit sets of colors.
  656. /// </summary>
  657. /// <param name="foreground">Foreground.</param>
  658. /// <param name="background">Background.</param>
  659. public abstract void SetColors (ConsoleColor foreground, ConsoleColor background);
  660. // Advanced uses - set colors to any pre-set pairs, you would need to init_color
  661. // that independently with the R, G, B values.
  662. /// <summary>
  663. /// Advanced uses - set colors to any pre-set pairs, you would need to init_color
  664. /// that independently with the R, G, B values.
  665. /// </summary>
  666. /// <param name="foregroundColorId">Foreground color identifier.</param>
  667. /// <param name="backgroundColorId">Background color identifier.</param>
  668. public abstract void SetColors (short foregroundColorId, short backgroundColorId);
  669. /// <summary>
  670. /// Set the handler when the terminal is resized.
  671. /// </summary>
  672. /// <param name="terminalResized"></param>
  673. public void SetTerminalResized (Action terminalResized)
  674. {
  675. TerminalResized = terminalResized;
  676. }
  677. /// <summary>
  678. /// Draws the title for a Window-style view incorporating padding.
  679. /// </summary>
  680. /// <param name="region">Screen relative region where the frame will be drawn.</param>
  681. /// <param name="title">The title for the window. The title will only be drawn if <c>title</c> is not null or empty and paddingTop is greater than 0.</param>
  682. /// <param name="paddingLeft">Number of columns to pad on the left (if 0 the border will not appear on the left).</param>
  683. /// <param name="paddingTop">Number of rows to pad on the top (if 0 the border and title will not appear on the top).</param>
  684. /// <param name="paddingRight">Number of columns to pad on the right (if 0 the border will not appear on the right).</param>
  685. /// <param name="paddingBottom">Number of rows to pad on the bottom (if 0 the border will not appear on the bottom).</param>
  686. /// <param name="textAlignment">Not yet implemented.</param>
  687. /// <remarks></remarks>
  688. public virtual void DrawWindowTitle (Rect region, ustring title, int paddingLeft, int paddingTop, int paddingRight, int paddingBottom, TextAlignment textAlignment = TextAlignment.Left)
  689. {
  690. var width = region.Width - (paddingLeft + 2) * 2;
  691. if (!ustring.IsNullOrEmpty (title) && width > 4 && region.Y + paddingTop <= region.Y + paddingBottom) {
  692. Move (region.X + 1 + paddingLeft, region.Y + paddingTop);
  693. AddRune (' ');
  694. var str = title.RuneCount >= width ? title [0, width - 2] : title;
  695. AddStr (str);
  696. AddRune (' ');
  697. }
  698. }
  699. /// <summary>
  700. /// Enables diagnostic functions
  701. /// </summary>
  702. [Flags]
  703. public enum DiagnosticFlags : uint {
  704. /// <summary>
  705. /// All diagnostics off
  706. /// </summary>
  707. Off = 0b_0000_0000,
  708. /// <summary>
  709. /// When enabled, <see cref="DrawWindowFrame(Rect, int, int, int, int, bool, bool)"/> will draw a
  710. /// ruler in the frame for any side with a padding value greater than 0.
  711. /// </summary>
  712. FrameRuler = 0b_0000_0001,
  713. /// <summary>
  714. /// When Enabled, <see cref="DrawWindowFrame(Rect, int, int, int, int, bool, bool)"/> will use
  715. /// 'L', 'R', 'T', and 'B' for padding instead of ' '.
  716. /// </summary>
  717. FramePadding = 0b_0000_0010,
  718. }
  719. /// <summary>
  720. /// Set flags to enable/disable <see cref="ConsoleDriver"/> diagnostics.
  721. /// </summary>
  722. public static DiagnosticFlags Diagnostics { get; set; }
  723. /// <summary>
  724. /// Draws a frame for a window with padding and an optional visible border inside the padding.
  725. /// </summary>
  726. /// <param name="region">Screen relative region where the frame will be drawn.</param>
  727. /// <param name="paddingLeft">Number of columns to pad on the left (if 0 the border will not appear on the left).</param>
  728. /// <param name="paddingTop">Number of rows to pad on the top (if 0 the border and title will not appear on the top).</param>
  729. /// <param name="paddingRight">Number of columns to pad on the right (if 0 the border will not appear on the right).</param>
  730. /// <param name="paddingBottom">Number of rows to pad on the bottom (if 0 the border will not appear on the bottom).</param>
  731. /// <param name="border">If set to <c>true</c> and any padding dimension is > 0 the border will be drawn.</param>
  732. /// <param name="fill">If set to <c>true</c> it will clear the content area (the area inside the padding) with the current color, otherwise the content area will be left untouched.</param>
  733. public virtual void DrawWindowFrame (Rect region, int paddingLeft = 0, int paddingTop = 0, int paddingRight = 0, int paddingBottom = 0, bool border = true, bool fill = false)
  734. {
  735. char clearChar = ' ';
  736. char leftChar = clearChar;
  737. char rightChar = clearChar;
  738. char topChar = clearChar;
  739. char bottomChar = clearChar;
  740. if ((Diagnostics & DiagnosticFlags.FramePadding) == DiagnosticFlags.FramePadding) {
  741. leftChar = 'L';
  742. rightChar = 'R';
  743. topChar = 'T';
  744. bottomChar = 'B';
  745. clearChar = 'C';
  746. }
  747. void AddRuneAt (int col, int row, Rune ch)
  748. {
  749. Move (col, row);
  750. AddRune (ch);
  751. }
  752. // fwidth is count of hLine chars
  753. int fwidth = (int)(region.Width - (paddingRight + paddingLeft));
  754. // fheight is count of vLine chars
  755. int fheight = (int)(region.Height - (paddingBottom + paddingTop));
  756. // fleft is location of left frame line
  757. int fleft = region.X + paddingLeft - 1;
  758. // fright is location of right frame line
  759. int fright = fleft + fwidth + 1;
  760. // ftop is location of top frame line
  761. int ftop = region.Y + paddingTop - 1;
  762. // fbottom is locaiton of bottom frame line
  763. int fbottom = ftop + fheight + 1;
  764. Rune hLine = border ? HLine : clearChar;
  765. Rune vLine = border ? VLine : clearChar;
  766. Rune uRCorner = border ? URCorner : clearChar;
  767. Rune uLCorner = border ? ULCorner : clearChar;
  768. Rune lLCorner = border ? LLCorner : clearChar;
  769. Rune lRCorner = border ? LRCorner : clearChar;
  770. // Outside top
  771. if (paddingTop > 1) {
  772. for (int r = region.Y; r < ftop; r++) {
  773. for (int c = region.X; c < region.X + region.Width; c++) {
  774. AddRuneAt (c, r, topChar);
  775. }
  776. }
  777. }
  778. // Outside top-left
  779. for (int c = region.X; c < fleft; c++) {
  780. AddRuneAt (c, ftop, leftChar);
  781. }
  782. // Frame top-left corner
  783. AddRuneAt (fleft, ftop, paddingTop >= 0 ? (paddingLeft >= 0 ? uLCorner : hLine) : leftChar);
  784. // Frame top
  785. for (int c = fleft + 1; c < fleft + 1 + fwidth; c++) {
  786. AddRuneAt (c, ftop, paddingTop > 0 ? hLine : topChar);
  787. }
  788. // Frame top-right corner
  789. if (fright > fleft) {
  790. AddRuneAt (fright, ftop, paddingTop >= 0 ? (paddingRight >= 0 ? uRCorner : hLine) : rightChar);
  791. }
  792. // Outside top-right corner
  793. for (int c = fright + 1; c < fright + paddingRight; c++) {
  794. AddRuneAt (c, ftop, rightChar);
  795. }
  796. // Left, Fill, Right
  797. if (fbottom > ftop) {
  798. for (int r = ftop + 1; r < fbottom; r++) {
  799. // Outside left
  800. for (int c = region.X; c < fleft; c++) {
  801. AddRuneAt (c, r, leftChar);
  802. }
  803. // Frame left
  804. AddRuneAt (fleft, r, paddingLeft > 0 ? vLine : leftChar);
  805. // Fill
  806. if (fill) {
  807. for (int x = fleft + 1; x < fright; x++) {
  808. AddRuneAt (x, r, clearChar);
  809. }
  810. }
  811. // Frame right
  812. if (fright > fleft) {
  813. var v = vLine;
  814. if ((Diagnostics & DiagnosticFlags.FrameRuler) == DiagnosticFlags.FrameRuler) {
  815. v = (char)(((int)'0') + ((r - ftop) % 10)); // vLine;
  816. }
  817. AddRuneAt (fright, r, paddingRight > 0 ? v : rightChar);
  818. }
  819. // Outside right
  820. for (int c = fright + 1; c < fright + paddingRight; c++) {
  821. AddRuneAt (c, r, rightChar);
  822. }
  823. }
  824. // Outside Bottom
  825. for (int c = region.X; c < region.X + region.Width; c++) {
  826. AddRuneAt (c, fbottom, leftChar);
  827. }
  828. // Frame bottom-left
  829. AddRuneAt (fleft, fbottom, paddingLeft > 0 ? lLCorner : leftChar);
  830. if (fright > fleft) {
  831. // Frame bottom
  832. for (int c = fleft + 1; c < fright; c++) {
  833. var h = hLine;
  834. if ((Diagnostics & DiagnosticFlags.FrameRuler) == DiagnosticFlags.FrameRuler) {
  835. h = (char)(((int)'0') + ((c - fleft) % 10)); // hLine;
  836. }
  837. AddRuneAt (c, fbottom, paddingBottom > 0 ? h : bottomChar);
  838. }
  839. // Frame bottom-right
  840. AddRuneAt (fright, fbottom, paddingRight > 0 ? (paddingBottom > 0 ? lRCorner : hLine) : rightChar);
  841. }
  842. // Outside right
  843. for (int c = fright + 1; c < fright + paddingRight; c++) {
  844. AddRuneAt (c, fbottom, rightChar);
  845. }
  846. }
  847. // Out bottom - ensure top is always drawn if we overlap
  848. if (paddingBottom > 0) {
  849. for (int r = fbottom + 1; r < fbottom + paddingBottom; r++) {
  850. for (int c = region.X; c < region.X + region.Width; c++) {
  851. AddRuneAt (c, r, bottomChar);
  852. }
  853. }
  854. }
  855. }
  856. /// <summary>
  857. /// Draws a frame on the specified region with the specified padding around the frame.
  858. /// </summary>
  859. /// <param name="region">Screen relative region where the frame will be drawn.</param>
  860. /// <param name="padding">Padding to add on the sides.</param>
  861. /// <param name="fill">If set to <c>true</c> it will clear the contents with the current color, otherwise the contents will be left untouched.</param>
  862. /// <remarks>This API has been superseded by <see cref="DrawWindowFrame(Rect, int, int, int, int, bool, bool)"/>.</remarks>
  863. /// <remarks>This API is equivalent to calling <c>DrawWindowFrame(Rect, p - 1, p - 1, p - 1, p - 1)</c>. In other words,
  864. /// A padding value of 0 means there is actually a one cell border.
  865. /// </remarks>
  866. public virtual void DrawFrame (Rect region, int padding, bool fill)
  867. {
  868. // DrawFrame assumes the border is always at least one row/col thick
  869. // DrawWindowFrame assumes a padding of 0 means NO padding and no frame
  870. DrawWindowFrame (new Rect (region.X, region.Y, region.Width, region.Height),
  871. padding + 1, padding + 1, padding + 1, padding + 1, border: false, fill: fill);
  872. }
  873. /// <summary>
  874. /// Suspend the application, typically needs to save the state, suspend the app and upon return, reset the console driver.
  875. /// </summary>
  876. public abstract void Suspend ();
  877. Rect clip;
  878. /// <summary>
  879. /// Controls the current clipping region that AddRune/AddStr is subject to.
  880. /// </summary>
  881. /// <value>The clip.</value>
  882. public Rect Clip {
  883. get => clip;
  884. set => this.clip = value;
  885. }
  886. /// <summary>
  887. /// Start of mouse moves.
  888. /// </summary>
  889. public abstract void StartReportingMouseMoves ();
  890. /// <summary>
  891. /// Stop reporting mouses moves.
  892. /// </summary>
  893. public abstract void StopReportingMouseMoves ();
  894. /// <summary>
  895. /// Disables the cooked event processing from the mouse driver. At startup, it is assumed mouse events are cooked.
  896. /// </summary>
  897. public abstract void UncookMouse ();
  898. /// <summary>
  899. /// Enables the cooked event processing from the mouse driver
  900. /// </summary>
  901. public abstract void CookMouse ();
  902. /// <summary>
  903. /// Horizontal line character.
  904. /// </summary>
  905. public Rune HLine = '\u2500';
  906. /// <summary>
  907. /// Vertical line character.
  908. /// </summary>
  909. public Rune VLine = '\u2502';
  910. /// <summary>
  911. /// Stipple pattern
  912. /// </summary>
  913. public Rune Stipple = '\u2591';
  914. /// <summary>
  915. /// Diamond character
  916. /// </summary>
  917. public Rune Diamond = '\u25ca';
  918. /// <summary>
  919. /// Upper left corner
  920. /// </summary>
  921. public Rune ULCorner = '\u250C';
  922. /// <summary>
  923. /// Lower left corner
  924. /// </summary>
  925. public Rune LLCorner = '\u2514';
  926. /// <summary>
  927. /// Upper right corner
  928. /// </summary>
  929. public Rune URCorner = '\u2510';
  930. /// <summary>
  931. /// Lower right corner
  932. /// </summary>
  933. public Rune LRCorner = '\u2518';
  934. /// <summary>
  935. /// Left tee
  936. /// </summary>
  937. public Rune LeftTee = '\u251c';
  938. /// <summary>
  939. /// Right tee
  940. /// </summary>
  941. public Rune RightTee = '\u2524';
  942. /// <summary>
  943. /// Top tee
  944. /// </summary>
  945. public Rune TopTee = '\u252c';
  946. /// <summary>
  947. /// The bottom tee.
  948. /// </summary>
  949. public Rune BottomTee = '\u2534';
  950. /// <summary>
  951. /// Checkmark.
  952. /// </summary>
  953. public Rune Checked = '\u221a';
  954. /// <summary>
  955. /// Un-checked checkmark.
  956. /// </summary>
  957. public Rune UnChecked = '\u2574';
  958. /// <summary>
  959. /// Selected mark.
  960. /// </summary>
  961. public Rune Selected = '\u25cf';
  962. /// <summary>
  963. /// Un-selected selected mark.
  964. /// </summary>
  965. public Rune UnSelected = '\u25cc';
  966. /// <summary>
  967. /// Right Arrow.
  968. /// </summary>
  969. public Rune RightArrow = '\u25ba';
  970. /// <summary>
  971. /// Left Arrow.
  972. /// </summary>
  973. public Rune LeftArrow = '\u25c4';
  974. /// <summary>
  975. /// Down Arrow.
  976. /// </summary>
  977. public Rune DownArrow = '\u25bc';
  978. /// <summary>
  979. /// Up Arrow.
  980. /// </summary>
  981. public Rune UpArrow = '\u25b2';
  982. /// <summary>
  983. /// Left indicator for default action (e.g. for <see cref="Button"/>).
  984. /// </summary>
  985. public Rune LeftDefaultIndicator = '\u25e6';
  986. /// <summary>
  987. /// Right indicator for default action (e.g. for <see cref="Button"/>).
  988. /// </summary>
  989. public Rune RightDefaultIndicator = '\u25e6';
  990. /// <summary>
  991. /// Left frame/bracket (e.g. '[' for <see cref="Button"/>).
  992. /// </summary>
  993. public Rune LeftBracket = '[';
  994. /// <summary>
  995. /// Right frame/bracket (e.g. ']' for <see cref="Button"/>).
  996. /// </summary>
  997. public Rune RightBracket = ']';
  998. /// <summary>
  999. /// On Segment indicator for meter views (e.g. <see cref="ProgressBar"/>.
  1000. /// </summary>
  1001. public Rune OnMeterSegment = '\u258c';
  1002. /// <summary>
  1003. /// Off Segment indicator for meter views (e.g. <see cref="ProgressBar"/>.
  1004. /// </summary>
  1005. public Rune OffMeterSegement = ' ';
  1006. /// <summary>
  1007. /// Make the attribute for the foreground and background colors.
  1008. /// </summary>
  1009. /// <param name="fore">Foreground.</param>
  1010. /// <param name="back">Background.</param>
  1011. /// <returns></returns>
  1012. public abstract Attribute MakeAttribute (Color fore, Color back);
  1013. /// <summary>
  1014. /// Gets the current <see cref="Attribute"/>.
  1015. /// </summary>
  1016. /// <returns>The current attribute.</returns>
  1017. public abstract Attribute GetAttribute ();
  1018. }
  1019. }