ConsoleDriver.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  1. //
  2. // ConsoleDriver.cs: Base class for Terminal.Gui ConsoleDriver implementations.
  3. //
  4. using NStack;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Diagnostics;
  9. using System.Linq;
  10. using System.Runtime.CompilerServices;
  11. using System.Threading.Tasks;
  12. namespace Terminal.Gui {
  13. /// <summary>
  14. /// Colors that can be used to set the foreground and background colors in console applications.
  15. /// </summary>
  16. /// <remarks>
  17. /// The <see cref="Color.Invalid"/> value indicates either no-color has been set or the color is invalid.
  18. /// </remarks>
  19. [DefaultValue(Invalid)]
  20. public enum Color {
  21. /// <summary>
  22. /// The black color.
  23. /// </summary>
  24. Black,
  25. /// <summary>
  26. /// The blue color.
  27. /// </summary>
  28. Blue,
  29. /// <summary>
  30. /// The green color.
  31. /// </summary>
  32. Green,
  33. /// <summary>
  34. /// The cyan color.
  35. /// </summary>
  36. Cyan,
  37. /// <summary>
  38. /// The red color.
  39. /// </summary>
  40. Red,
  41. /// <summary>
  42. /// The magenta color.
  43. /// </summary>
  44. Magenta,
  45. /// <summary>
  46. /// The brown color.
  47. /// </summary>
  48. Brown,
  49. /// <summary>
  50. /// The gray color.
  51. /// </summary>
  52. Gray,
  53. /// <summary>
  54. /// The dark gray color.
  55. /// </summary>
  56. DarkGray,
  57. /// <summary>
  58. /// The bright bBlue color.
  59. /// </summary>
  60. BrightBlue,
  61. /// <summary>
  62. /// The bright green color.
  63. /// </summary>
  64. BrightGreen,
  65. /// <summary>
  66. /// The bright cyan color.
  67. /// </summary>
  68. BrightCyan,
  69. /// <summary>
  70. /// The bright red color.
  71. /// </summary>
  72. BrightRed,
  73. /// <summary>
  74. /// The bright magenta color.
  75. /// </summary>
  76. BrightMagenta,
  77. /// <summary>
  78. /// The bright yellow color.
  79. /// </summary>
  80. BrightYellow,
  81. /// <summary>
  82. /// The White color.
  83. /// </summary>
  84. White,
  85. /// <summary>
  86. /// Indicates an invalid or un-set color value.
  87. /// </summary>
  88. Invalid = -1
  89. }
  90. /// <summary>
  91. /// Attributes are used as elements that contain both a foreground and a background or platform specific features.
  92. /// </summary>
  93. /// <remarks>
  94. /// <see cref="Attribute"/>s are needed to map colors to terminal capabilities that might lack colors.
  95. /// They encode both the foreground and the background color and are used in the <see cref="ColorScheme"/>
  96. /// class to define color schemes that can be used in an application.
  97. /// </remarks>
  98. public struct Attribute {
  99. /// <summary>
  100. /// The <see cref="ConsoleDriver"/>-specific color attribute value. If <see cref="IsInitialized"/> is <see langword="false"/>
  101. /// the value of this property is invalid (typcially because the Attribute was created before a driver was loaded)
  102. /// and the attribute should be re-made (see <see cref="Make(Color, Color)"/>) before it is used.
  103. /// </summary>
  104. public int Value { get; }
  105. /// <summary>
  106. /// The foreground color.
  107. /// </summary>
  108. public Color Foreground { get; }
  109. /// <summary>
  110. /// The background color.
  111. /// </summary>
  112. public Color Background { get; }
  113. /// <summary>
  114. /// If <see langword="true"/> the attribute has been initialzed by a <see cref="ConsoleDriver"/> and
  115. /// thus has <see cref="Value"/> that is valid for that driver. If <see langword="false"/> the <see cref="Foreground"/>
  116. /// and <see cref="Background"/> colors may have been set (see <see cref="Color.Invalid"/>) but
  117. /// the attribute has not been mapped to a <see cref="ConsoleDriver"/> specific color value.
  118. /// </summary>
  119. /// <remarks>
  120. /// Attributes that have not been initialized must eventually be initialized before being passed to a driver.
  121. /// </remarks>
  122. public bool IsInitialized { get; internal set; }
  123. /// <summary>
  124. /// Initializes a new instance of the <see cref="Attribute"/> struct with only the value passed to
  125. /// and trying to get the colors if defined.
  126. /// </summary>
  127. /// <param name="value">Value.</param>
  128. public Attribute (int value)
  129. {
  130. Color foreground = default;
  131. Color background = default;
  132. if (Application.Driver != null) {
  133. Application.Driver.GetColors (value, out foreground, out background);
  134. }
  135. Value = value;
  136. Foreground = foreground;
  137. Background = background;
  138. IsInitialized = true;
  139. }
  140. /// <summary>
  141. /// Initializes a new instance of the <see cref="Attribute"/> struct.
  142. /// </summary>
  143. /// <param name="value">Value.</param>
  144. /// <param name="foreground">Foreground</param>
  145. /// <param name="background">Background</param>
  146. public Attribute (int value, Color foreground, Color background)
  147. {
  148. Value = value;
  149. Foreground = foreground;
  150. Background = background;
  151. IsInitialized = true;
  152. }
  153. /// <summary>
  154. /// Initializes a new instance of the <see cref="Attribute"/> struct.
  155. /// </summary>
  156. /// <param name="foreground">Foreground</param>
  157. /// <param name="background">Background</param>
  158. public Attribute (Color foreground = new Color (), Color background = new Color ())
  159. {
  160. IsInitialized = true;
  161. Value = Make (foreground, background).Value;
  162. Foreground = foreground;
  163. Background = background;
  164. }
  165. /// <summary>
  166. /// Initializes a new instance of the <see cref="Attribute"/> struct
  167. /// with the same colors for the foreground and background.
  168. /// </summary>
  169. /// <param name="color">The color.</param>
  170. public Attribute (Color color) : this (color, color) { }
  171. /// <summary>
  172. /// Implicit conversion from an <see cref="Attribute"/> to the underlying, driver-specific, Int32 representation
  173. /// of the color.
  174. /// </summary>
  175. /// <returns>The driver-specific color value stored in the attribute.</returns>
  176. /// <param name="c">The attribute to convert</param>
  177. public static implicit operator int (Attribute c) {
  178. Debug.WriteLineIf (!c.IsInitialized, "ConsoleDriver.SetAttribute: Attributes must be initialized by a driver before use.");
  179. //if (!c.IsInitialized) throw new InvalidOperationException ("Attributes must be initialized by driver before use.");
  180. return c.Value;
  181. }
  182. /// <summary>
  183. /// Implicitly convert an driver-specific color value into an <see cref="Attribute"/>
  184. /// </summary>
  185. /// <returns>An attribute with the specified driver-specific color value.</returns>
  186. /// <param name="v">value</param>
  187. public static implicit operator Attribute (int v) => new Attribute (v);
  188. /// <summary>
  189. /// Creates an <see cref="Attribute"/> from the specified foreground and background colors.
  190. /// </summary>
  191. /// <remarks>
  192. /// If a <see cref="ConsoleDriver"/> has not been loaded (<c>Application.Driver == null</c>) this
  193. /// method will return an attribute with <see cref="IsInitialized"/> set to <see langword="false"/>.
  194. /// </remarks>
  195. /// <returns>The new attribute.</returns>
  196. /// <param name="foreground">Foreground color to use.</param>
  197. /// <param name="background">Background color to use.</param>
  198. public static Attribute Make (Color foreground, Color background)
  199. {
  200. if (Application.Driver == null) {
  201. // Create the attribute, but show it's not been initialized
  202. var a = new Attribute (-1, foreground, background);
  203. a.IsInitialized = false;
  204. return a;
  205. }
  206. return Application.Driver.MakeAttribute (foreground, background);
  207. }
  208. /// <summary>
  209. /// Gets the current <see cref="Attribute"/> from the driver.
  210. /// </summary>
  211. /// <returns>The current attribute.</returns>
  212. public static Attribute Get ()
  213. {
  214. if (Application.Driver == null)
  215. throw new InvalidOperationException ("The Application has not been initialized");
  216. return Application.Driver.GetAttribute ();
  217. }
  218. /// <summary>
  219. /// Returns <see langword="true"/> if the Atrribute is valid (both foreground and background have valid color values).
  220. /// </summary>
  221. /// <returns></returns>
  222. public bool IsValid ()
  223. {
  224. return Foreground != Color.Invalid && Background != Color.Invalid;
  225. }
  226. }
  227. /// <summary>
  228. /// Defines the color <see cref="Attribute"/>s for common visible elements in a <see cref="View"/>.
  229. /// Containers such as <see cref="Window"/> and <see cref="FrameView"/> use <see cref="ColorScheme"/> to determine
  230. /// the colors used by sub-views.
  231. /// </summary>
  232. /// <remarks>
  233. /// See also: <see cref="Colors.ColorSchemes"/>.
  234. /// </remarks>
  235. public class ColorScheme : IEquatable<ColorScheme> {
  236. Attribute _normal;
  237. Attribute _focus;
  238. Attribute _hotNormal;
  239. Attribute _hotFocus;
  240. Attribute _disabled;
  241. /// <summary>
  242. /// Used by <see cref="Colors.SetColorScheme(ColorScheme, string)"/> and <see cref="Colors.GetColorScheme(string)"/> to track which ColorScheme
  243. /// is being accessed.
  244. /// </summary>
  245. internal string schemeBeingSet = "";
  246. /// <summary>
  247. /// The foreground and background color for text when the view is not focused, hot, or disabled.
  248. /// </summary>
  249. public Attribute Normal {
  250. get { return _normal; }
  251. set {
  252. if (!value.IsValid ()) {
  253. return;
  254. }
  255. _normal = value;
  256. }
  257. }
  258. /// <summary>
  259. /// The foreground and background color for text when the view has the focus.
  260. /// </summary>
  261. public Attribute Focus {
  262. get { return _focus; }
  263. set {
  264. if (!value.IsValid ()) {
  265. return;
  266. }
  267. _focus = value;
  268. }
  269. }
  270. /// <summary>
  271. /// The foreground and background color for text when the view is highlighted (hot).
  272. /// </summary>
  273. public Attribute HotNormal {
  274. get { return _hotNormal; }
  275. set {
  276. if (!value.IsValid ()) {
  277. return;
  278. }
  279. _hotNormal = value;
  280. }
  281. }
  282. /// <summary>
  283. /// The foreground and background color for text when the view is highlighted (hot) and has focus.
  284. /// </summary>
  285. public Attribute HotFocus {
  286. get { return _hotFocus; }
  287. set {
  288. if (!value.IsValid ()) {
  289. return;
  290. }
  291. _hotFocus = value;
  292. }
  293. }
  294. /// <summary>
  295. /// The default foreground and background color for text, when the view is disabled.
  296. /// </summary>
  297. public Attribute Disabled {
  298. get { return _disabled; }
  299. set {
  300. if (!value.IsValid ()) {
  301. return;
  302. }
  303. _disabled = value;
  304. }
  305. }
  306. /// <summary>
  307. /// Compares two <see cref="ColorScheme"/> objects for equality.
  308. /// </summary>
  309. /// <param name="obj"></param>
  310. /// <returns>true if the two objects are equal</returns>
  311. public override bool Equals (object obj)
  312. {
  313. return Equals (obj as ColorScheme);
  314. }
  315. /// <summary>
  316. /// Compares two <see cref="ColorScheme"/> objects for equality.
  317. /// </summary>
  318. /// <param name="other"></param>
  319. /// <returns>true if the two objects are equal</returns>
  320. public bool Equals (ColorScheme other)
  321. {
  322. return other != null &&
  323. EqualityComparer<Attribute>.Default.Equals (_normal, other._normal) &&
  324. EqualityComparer<Attribute>.Default.Equals (_focus, other._focus) &&
  325. EqualityComparer<Attribute>.Default.Equals (_hotNormal, other._hotNormal) &&
  326. EqualityComparer<Attribute>.Default.Equals (_hotFocus, other._hotFocus) &&
  327. EqualityComparer<Attribute>.Default.Equals (_disabled, other._disabled);
  328. }
  329. /// <summary>
  330. /// Returns a hashcode for this instance.
  331. /// </summary>
  332. /// <returns>hashcode for this instance</returns>
  333. public override int GetHashCode ()
  334. {
  335. int hashCode = -1242460230;
  336. hashCode = hashCode * -1521134295 + _normal.GetHashCode ();
  337. hashCode = hashCode * -1521134295 + _focus.GetHashCode ();
  338. hashCode = hashCode * -1521134295 + _hotNormal.GetHashCode ();
  339. hashCode = hashCode * -1521134295 + _hotFocus.GetHashCode ();
  340. hashCode = hashCode * -1521134295 + _disabled.GetHashCode ();
  341. return hashCode;
  342. }
  343. /// <summary>
  344. /// Compares two <see cref="ColorScheme"/> objects for equality.
  345. /// </summary>
  346. /// <param name="left"></param>
  347. /// <param name="right"></param>
  348. /// <returns><c>true</c> if the two objects are equivalent</returns>
  349. public static bool operator == (ColorScheme left, ColorScheme right)
  350. {
  351. return EqualityComparer<ColorScheme>.Default.Equals (left, right);
  352. }
  353. /// <summary>
  354. /// Compares two <see cref="ColorScheme"/> objects for inequality.
  355. /// </summary>
  356. /// <param name="left"></param>
  357. /// <param name="right"></param>
  358. /// <returns><c>true</c> if the two objects are not equivalent</returns>
  359. public static bool operator != (ColorScheme left, ColorScheme right)
  360. {
  361. return !(left == right);
  362. }
  363. }
  364. /// <summary>
  365. /// The default <see cref="ColorScheme"/>s for the application.
  366. /// </summary>
  367. public static class Colors {
  368. static Colors ()
  369. {
  370. // Use reflection to dynamically create the default set of ColorSchemes from the list defined
  371. // by the class.
  372. ColorSchemes = typeof (Colors).GetProperties ()
  373. .Where (p => p.PropertyType == typeof (ColorScheme))
  374. .Select (p => new KeyValuePair<string, ColorScheme> (p.Name, new ColorScheme ())) // (ColorScheme)p.GetValue (p)))
  375. .ToDictionary (t => t.Key, t => t.Value);
  376. }
  377. /// <summary>
  378. /// The application toplevel color scheme, for the default toplevel views.
  379. /// </summary>
  380. /// <remarks>
  381. /// <para>
  382. /// This API will be deprecated in the future. Use <see cref="Colors.ColorSchemes"/> instead (e.g. <c>edit.ColorScheme = Colors.ColorSchemes["TopLevel"];</c>
  383. /// </para>
  384. /// </remarks>
  385. public static ColorScheme TopLevel { get => GetColorScheme (); set => SetColorScheme (value); }
  386. /// <summary>
  387. /// The base color scheme, for the default toplevel views.
  388. /// </summary>
  389. /// <remarks>
  390. /// <para>
  391. /// This API will be deprecated in the future. Use <see cref="Colors.ColorSchemes"/> instead (e.g. <c>edit.ColorScheme = Colors.ColorSchemes["Base"];</c>
  392. /// </para>
  393. /// </remarks>
  394. public static ColorScheme Base { get => GetColorScheme (); set => SetColorScheme (value); }
  395. /// <summary>
  396. /// The dialog color scheme, for standard popup dialog boxes
  397. /// </summary>
  398. /// <remarks>
  399. /// <para>
  400. /// This API will be deprecated in the future. Use <see cref="Colors.ColorSchemes"/> instead (e.g. <c>edit.ColorScheme = Colors.ColorSchemes["Dialog"];</c>
  401. /// </para>
  402. /// </remarks>
  403. public static ColorScheme Dialog { get => GetColorScheme (); set => SetColorScheme (value); }
  404. /// <summary>
  405. /// The menu bar color
  406. /// </summary>
  407. /// <remarks>
  408. /// <para>
  409. /// This API will be deprecated in the future. Use <see cref="Colors.ColorSchemes"/> instead (e.g. <c>edit.ColorScheme = Colors.ColorSchemes["Menu"];</c>
  410. /// </para>
  411. /// </remarks>
  412. public static ColorScheme Menu { get => GetColorScheme (); set => SetColorScheme (value); }
  413. /// <summary>
  414. /// The color scheme for showing errors.
  415. /// </summary>
  416. /// <remarks>
  417. /// <para>
  418. /// This API will be deprecated in the future. Use <see cref="Colors.ColorSchemes"/> instead (e.g. <c>edit.ColorScheme = Colors.ColorSchemes["Error"];</c>
  419. /// </para>
  420. /// </remarks>
  421. public static ColorScheme Error { get => GetColorScheme (); set => SetColorScheme (value); }
  422. static ColorScheme GetColorScheme ([CallerMemberName] string schemeBeingSet = null)
  423. {
  424. return ColorSchemes [schemeBeingSet];
  425. }
  426. static void SetColorScheme (ColorScheme colorScheme, [CallerMemberName] string schemeBeingSet = null)
  427. {
  428. ColorSchemes [schemeBeingSet] = colorScheme;
  429. colorScheme.schemeBeingSet = schemeBeingSet;
  430. }
  431. /// <summary>
  432. /// Provides the defined <see cref="ColorScheme"/>s.
  433. /// </summary>
  434. public static Dictionary<string, ColorScheme> ColorSchemes { get; }
  435. }
  436. /// <summary>
  437. /// Cursors Visibility that are displayed
  438. /// </summary>
  439. //
  440. // Hexa value are set as 0xAABBCCDD where :
  441. //
  442. // AA stand for the TERMINFO DECSUSR parameter value to be used under Linux & MacOS
  443. // BB stand for the NCurses curs_set parameter value to be used under Linux & MacOS
  444. // CC stand for the CONSOLE_CURSOR_INFO.bVisible parameter value to be used under Windows
  445. // DD stand for the CONSOLE_CURSOR_INFO.dwSize parameter value to be used under Windows
  446. //
  447. public enum CursorVisibility {
  448. /// <summary>
  449. /// Cursor caret has default
  450. /// </summary>
  451. /// <remarks>Works under Xterm-like terminal otherwise this is equivalent to <see ref="Underscore"/>. This default directly depends of the XTerm user configuration settings so it could be Block, I-Beam, Underline with possible blinking.</remarks>
  452. Default = 0x00010119,
  453. /// <summary>
  454. /// Cursor caret is hidden
  455. /// </summary>
  456. Invisible = 0x03000019,
  457. /// <summary>
  458. /// Cursor caret is normally shown as a blinking underline bar _
  459. /// </summary>
  460. Underline = 0x03010119,
  461. /// <summary>
  462. /// Cursor caret is normally shown as a underline bar _
  463. /// </summary>
  464. /// <remarks>Under Windows, this is equivalent to <see ref="UnderscoreBlinking"/></remarks>
  465. UnderlineFix = 0x04010119,
  466. /// <summary>
  467. /// Cursor caret is displayed a blinking vertical bar |
  468. /// </summary>
  469. /// <remarks>Works under Xterm-like terminal otherwise this is equivalent to <see ref="Underscore"/></remarks>
  470. Vertical = 0x05010119,
  471. /// <summary>
  472. /// Cursor caret is displayed a blinking vertical bar |
  473. /// </summary>
  474. /// <remarks>Works under Xterm-like terminal otherwise this is equivalent to <see ref="Underscore"/></remarks>
  475. VerticalFix = 0x06010119,
  476. /// <summary>
  477. /// Cursor caret is displayed as a blinking block ▉
  478. /// </summary>
  479. Box = 0x01020164,
  480. /// <summary>
  481. /// Cursor caret is displayed a block ▉
  482. /// </summary>
  483. /// <remarks>Works under Xterm-like terminal otherwise this is equivalent to <see ref="Block"/></remarks>
  484. BoxFix = 0x02020164,
  485. }
  486. ///// <summary>
  487. ///// Special characters that can be drawn with
  488. ///// </summary>
  489. //public enum SpecialChar {
  490. // /// <summary>
  491. // /// Horizontal line character.
  492. // /// </summary>
  493. // HLine,
  494. // /// <summary>
  495. // /// Vertical line character.
  496. // /// </summary>
  497. // VLine,
  498. // /// <summary>
  499. // /// Stipple pattern
  500. // /// </summary>
  501. // Stipple,
  502. // /// <summary>
  503. // /// Diamond character
  504. // /// </summary>
  505. // Diamond,
  506. // /// <summary>
  507. // /// Upper left corner
  508. // /// </summary>
  509. // ULCorner,
  510. // /// <summary>
  511. // /// Lower left corner
  512. // /// </summary>
  513. // LLCorner,
  514. // /// <summary>
  515. // /// Upper right corner
  516. // /// </summary>
  517. // URCorner,
  518. // /// <summary>
  519. // /// Lower right corner
  520. // /// </summary>
  521. // LRCorner,
  522. // /// <summary>
  523. // /// Left tee
  524. // /// </summary>
  525. // LeftTee,
  526. // /// <summary>
  527. // /// Right tee
  528. // /// </summary>
  529. // RightTee,
  530. // /// <summary>
  531. // /// Top tee
  532. // /// </summary>
  533. // TopTee,
  534. // /// <summary>
  535. // /// The bottom tee.
  536. // /// </summary>
  537. // BottomTee,
  538. //}
  539. /// <summary>
  540. /// ConsoleDriver is an abstract class that defines the requirements for a console driver.
  541. /// 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.
  542. /// </summary>
  543. public abstract class ConsoleDriver {
  544. /// <summary>
  545. /// The handler fired when the terminal is resized.
  546. /// </summary>
  547. protected Action TerminalResized;
  548. /// <summary>
  549. /// The current number of columns in the terminal.
  550. /// </summary>
  551. public abstract int Cols { get; }
  552. /// <summary>
  553. /// The current number of rows in the terminal.
  554. /// </summary>
  555. public abstract int Rows { get; }
  556. /// <summary>
  557. /// The current left in the terminal.
  558. /// </summary>
  559. public abstract int Left { get; }
  560. /// <summary>
  561. /// The current top in the terminal.
  562. /// </summary>
  563. public abstract int Top { get; }
  564. /// <summary>
  565. /// Get the operation system clipboard.
  566. /// </summary>
  567. public abstract IClipboard Clipboard { get; }
  568. /// <summary>
  569. /// If false height is measured by the window height and thus no scrolling.
  570. /// If true then height is measured by the buffer height, enabling scrolling.
  571. /// </summary>
  572. public abstract bool HeightAsBuffer { get; set; }
  573. /// <summary>
  574. /// The format is rows, columns and 3 values on the last column: Rune, Attribute and Dirty Flag
  575. /// </summary>
  576. public virtual int [,,] Contents { get; }
  577. /// <summary>
  578. /// Initializes the driver
  579. /// </summary>
  580. /// <param name="terminalResized">Method to invoke when the terminal is resized.</param>
  581. public abstract void Init (Action terminalResized);
  582. /// <summary>
  583. /// Moves the cursor to the specified column and row.
  584. /// </summary>
  585. /// <param name="col">Column to move the cursor to.</param>
  586. /// <param name="row">Row to move the cursor to.</param>
  587. public abstract void Move (int col, int row);
  588. /// <summary>
  589. /// Adds the specified rune to the display at the current cursor position.
  590. /// </summary>
  591. /// <param name="rune">Rune to add.</param>
  592. public abstract void AddRune (Rune rune);
  593. /// <summary>
  594. /// Ensures a Rune is not a control character and can be displayed by translating characters below 0x20
  595. /// to equivalent, printable, Unicode chars.
  596. /// </summary>
  597. /// <param name="c">Rune to translate</param>
  598. /// <returns></returns>
  599. public static Rune MakePrintable (Rune c)
  600. {
  601. if (c <= 0x1F || (c >= 0X7F && c <= 0x9F)) {
  602. // ASCII (C0) control characters.
  603. // C1 control characters (https://www.aivosto.com/articles/control-characters.html#c1)
  604. return new Rune (c + 0x2400);
  605. }
  606. return c;
  607. }
  608. /// <summary>
  609. /// Ensures that the column and line are in a valid range from the size of the driver.
  610. /// </summary>
  611. /// <param name="col">The column.</param>
  612. /// <param name="row">The row.</param>
  613. /// <param name="clip">The clip.</param>
  614. /// <returns><c>true</c>if it's a valid range,<c>false</c>otherwise.</returns>
  615. public bool IsValidContent (int col, int row, Rect clip) =>
  616. col >= 0 && row >= 0 && col < Cols && row < Rows && clip.Contains (col, row);
  617. /// <summary>
  618. /// Adds the <paramref name="str"/> to the display at the cursor position.
  619. /// </summary>
  620. /// <param name="str">String.</param>
  621. public abstract void AddStr (ustring str);
  622. /// <summary>
  623. /// Prepare the driver and set the key and mouse events handlers.
  624. /// </summary>
  625. /// <param name="mainLoop">The main loop.</param>
  626. /// <param name="keyHandler">The handler for ProcessKey</param>
  627. /// <param name="keyDownHandler">The handler for key down events</param>
  628. /// <param name="keyUpHandler">The handler for key up events</param>
  629. /// <param name="mouseHandler">The handler for mouse events</param>
  630. public abstract void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler);
  631. /// <summary>
  632. /// Updates the screen to reflect all the changes that have been done to the display buffer
  633. /// </summary>
  634. public abstract void Refresh ();
  635. /// <summary>
  636. /// Updates the location of the cursor position
  637. /// </summary>
  638. public abstract void UpdateCursor ();
  639. /// <summary>
  640. /// Retreive the cursor caret visibility
  641. /// </summary>
  642. /// <param name="visibility">The current <see cref="CursorVisibility"/></param>
  643. /// <returns>true upon success</returns>
  644. public abstract bool GetCursorVisibility (out CursorVisibility visibility);
  645. /// <summary>
  646. /// Change the cursor caret visibility
  647. /// </summary>
  648. /// <param name="visibility">The wished <see cref="CursorVisibility"/></param>
  649. /// <returns>true upon success</returns>
  650. public abstract bool SetCursorVisibility (CursorVisibility visibility);
  651. /// <summary>
  652. /// Ensure the cursor visibility
  653. /// </summary>
  654. /// <returns>true upon success</returns>
  655. public abstract bool EnsureCursorVisibility ();
  656. /// <summary>
  657. /// Ends the execution of the console driver.
  658. /// </summary>
  659. public abstract void End ();
  660. /// <summary>
  661. /// Resizes the clip area when the screen is resized.
  662. /// </summary>
  663. public abstract void ResizeScreen ();
  664. /// <summary>
  665. /// Reset and recreate the contents and the driver buffer.
  666. /// </summary>
  667. public abstract void UpdateOffScreen ();
  668. /// <summary>
  669. /// Redraws the physical screen with the contents that have been queued up via any of the printing commands.
  670. /// </summary>
  671. public abstract void UpdateScreen ();
  672. /// <summary>
  673. /// Selects the specified attribute as the attribute to use for future calls to AddRune and AddString.
  674. /// </summary>
  675. /// <remarks>
  676. /// Implementations should call <c>base.SetAttribute(c)</c>.
  677. /// </remarks>
  678. /// <param name="c">C.</param>
  679. public virtual void SetAttribute (Attribute c)
  680. {
  681. Debug.WriteLineIf(!c.IsInitialized, "ConsoleDriver.SetAttribute: Attributes must be initialized before use.");
  682. }
  683. /// <summary>
  684. /// Set Colors from limit sets of colors. Not implemented by any driver: See Issue #2300.
  685. /// </summary>
  686. /// <param name="foreground">Foreground.</param>
  687. /// <param name="background">Background.</param>
  688. public abstract void SetColors (ConsoleColor foreground, ConsoleColor background);
  689. // Advanced uses - set colors to any pre-set pairs, you would need to init_color
  690. // that independently with the R, G, B values.
  691. /// <summary>
  692. /// Advanced uses - set colors to any pre-set pairs, you would need to init_color
  693. /// that independently with the R, G, B values. Not implemented by any driver: See Issue #2300.
  694. /// </summary>
  695. /// <param name="foregroundColorId">Foreground color identifier.</param>
  696. /// <param name="backgroundColorId">Background color identifier.</param>
  697. public abstract void SetColors (short foregroundColorId, short backgroundColorId);
  698. /// <summary>
  699. /// Gets the foreground and background colors based on the value.
  700. /// </summary>
  701. /// <param name="value">The value.</param>
  702. /// <param name="foreground">The foreground.</param>
  703. /// <param name="background">The background.</param>
  704. /// <returns></returns>
  705. public abstract bool GetColors (int value, out Color foreground, out Color background);
  706. /// <summary>
  707. /// Allows sending keys without typing on a keyboard.
  708. /// </summary>
  709. /// <param name="keyChar">The character key.</param>
  710. /// <param name="key">The key.</param>
  711. /// <param name="shift">If shift key is sending.</param>
  712. /// <param name="alt">If alt key is sending.</param>
  713. /// <param name="control">If control key is sending.</param>
  714. public abstract void SendKeys (char keyChar, ConsoleKey key, bool shift, bool alt, bool control);
  715. /// <summary>
  716. /// Set the handler when the terminal is resized.
  717. /// </summary>
  718. /// <param name="terminalResized"></param>
  719. public void SetTerminalResized (Action terminalResized)
  720. {
  721. TerminalResized = terminalResized;
  722. }
  723. /// <summary>
  724. /// Draws the title for a Window-style view incorporating padding.
  725. /// </summary>
  726. /// <param name="region">Screen relative region where the frame will be drawn.</param>
  727. /// <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>
  728. /// <param name="paddingLeft">Number of columns to pad on the left (if 0 the border will not appear on the left).</param>
  729. /// <param name="paddingTop">Number of rows to pad on the top (if 0 the border and title will not appear on the top).</param>
  730. /// <param name="paddingRight">Number of columns to pad on the right (if 0 the border will not appear on the right).</param>
  731. /// <param name="paddingBottom">Number of rows to pad on the bottom (if 0 the border will not appear on the bottom).</param>
  732. /// <param name="textAlignment">Not yet implemented.</param>
  733. /// <remarks></remarks>
  734. public virtual void DrawWindowTitle (Rect region, ustring title, int paddingLeft, int paddingTop, int paddingRight, int paddingBottom, TextAlignment textAlignment = TextAlignment.Left)
  735. {
  736. var width = region.Width - (paddingLeft + 2) * 2;
  737. if (!ustring.IsNullOrEmpty (title) && width > 4 && region.Y + paddingTop <= region.Y + paddingBottom) {
  738. Move (region.X + 1 + paddingLeft, region.Y + paddingTop);
  739. AddRune (' ');
  740. var str = title.Sum (r => Math.Max (Rune.ColumnWidth (r), 1)) >= width
  741. ? TextFormatter.Format (title, width - 2, false, false) [0] : title;
  742. AddStr (str);
  743. AddRune (' ');
  744. }
  745. }
  746. /// <summary>
  747. /// Enables diagnostic functions
  748. /// </summary>
  749. [Flags]
  750. public enum DiagnosticFlags : uint {
  751. /// <summary>
  752. /// All diagnostics off
  753. /// </summary>
  754. Off = 0b_0000_0000,
  755. /// <summary>
  756. /// When enabled, <see cref="DrawWindowFrame(Rect, int, int, int, int, bool, bool, Border)"/> will draw a
  757. /// ruler in the frame for any side with a padding value greater than 0.
  758. /// </summary>
  759. FrameRuler = 0b_0000_0001,
  760. /// <summary>
  761. /// When Enabled, <see cref="DrawWindowFrame(Rect, int, int, int, int, bool, bool, Border)"/> will use
  762. /// 'L', 'R', 'T', and 'B' for padding instead of ' '.
  763. /// </summary>
  764. FramePadding = 0b_0000_0010,
  765. }
  766. /// <summary>
  767. /// Set flags to enable/disable <see cref="ConsoleDriver"/> diagnostics.
  768. /// </summary>
  769. public static DiagnosticFlags Diagnostics { get; set; }
  770. /// <summary>
  771. /// Draws a frame for a window with padding and an optional visible border inside the padding.
  772. /// </summary>
  773. /// <param name="region">Screen relative region where the frame will be drawn.</param>
  774. /// <param name="paddingLeft">Number of columns to pad on the left (if 0 the border will not appear on the left).</param>
  775. /// <param name="paddingTop">Number of rows to pad on the top (if 0 the border and title will not appear on the top).</param>
  776. /// <param name="paddingRight">Number of columns to pad on the right (if 0 the border will not appear on the right).</param>
  777. /// <param name="paddingBottom">Number of rows to pad on the bottom (if 0 the border will not appear on the bottom).</param>
  778. /// <param name="border">If set to <c>true</c> and any padding dimension is > 0 the border will be drawn.</param>
  779. /// <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>
  780. /// <param name="borderContent">The <see cref="Border"/> to be used if defined.</param>
  781. public virtual void DrawWindowFrame (Rect region, int paddingLeft = 0, int paddingTop = 0, int paddingRight = 0,
  782. int paddingBottom = 0, bool border = true, bool fill = false, Border borderContent = null)
  783. {
  784. char clearChar = ' ';
  785. char leftChar = clearChar;
  786. char rightChar = clearChar;
  787. char topChar = clearChar;
  788. char bottomChar = clearChar;
  789. if ((Diagnostics & DiagnosticFlags.FramePadding) == DiagnosticFlags.FramePadding) {
  790. leftChar = 'L';
  791. rightChar = 'R';
  792. topChar = 'T';
  793. bottomChar = 'B';
  794. clearChar = 'C';
  795. }
  796. void AddRuneAt (int col, int row, Rune ch)
  797. {
  798. Move (col, row);
  799. AddRune (ch);
  800. }
  801. // fwidth is count of hLine chars
  802. int fwidth = (int)(region.Width - (paddingRight + paddingLeft));
  803. // fheight is count of vLine chars
  804. int fheight = (int)(region.Height - (paddingBottom + paddingTop));
  805. // fleft is location of left frame line
  806. int fleft = region.X + paddingLeft - 1;
  807. // fright is location of right frame line
  808. int fright = fleft + fwidth + 1;
  809. // ftop is location of top frame line
  810. int ftop = region.Y + paddingTop - 1;
  811. // fbottom is location of bottom frame line
  812. int fbottom = ftop + fheight + 1;
  813. var borderStyle = borderContent == null ? BorderStyle.Single : borderContent.BorderStyle;
  814. Rune hLine = default, vLine = default,
  815. uRCorner = default, uLCorner = default, lLCorner = default, lRCorner = default;
  816. if (border) {
  817. switch (borderStyle) {
  818. case BorderStyle.None:
  819. break;
  820. case BorderStyle.Single:
  821. hLine = HLine;
  822. vLine = VLine;
  823. uRCorner = URCorner;
  824. uLCorner = ULCorner;
  825. lLCorner = LLCorner;
  826. lRCorner = LRCorner;
  827. break;
  828. case BorderStyle.Double:
  829. hLine = HDLine;
  830. vLine = VDLine;
  831. uRCorner = URDCorner;
  832. uLCorner = ULDCorner;
  833. lLCorner = LLDCorner;
  834. lRCorner = LRDCorner;
  835. break;
  836. case BorderStyle.Rounded:
  837. hLine = HRLine;
  838. vLine = VRLine;
  839. uRCorner = URRCorner;
  840. uLCorner = ULRCorner;
  841. lLCorner = LLRCorner;
  842. lRCorner = LRRCorner;
  843. break;
  844. }
  845. } else {
  846. hLine = vLine = uRCorner = uLCorner = lLCorner = lRCorner = clearChar;
  847. }
  848. // Outside top
  849. if (paddingTop > 1) {
  850. for (int r = region.Y; r < ftop; r++) {
  851. for (int c = region.X; c < region.X + region.Width; c++) {
  852. AddRuneAt (c, r, topChar);
  853. }
  854. }
  855. }
  856. // Outside top-left
  857. for (int c = region.X; c < fleft; c++) {
  858. AddRuneAt (c, ftop, leftChar);
  859. }
  860. // Frame top-left corner
  861. AddRuneAt (fleft, ftop, paddingTop >= 0 ? (paddingLeft >= 0 ? uLCorner : hLine) : leftChar);
  862. // Frame top
  863. for (int c = fleft + 1; c < fleft + 1 + fwidth; c++) {
  864. AddRuneAt (c, ftop, paddingTop > 0 ? hLine : topChar);
  865. }
  866. // Frame top-right corner
  867. if (fright > fleft) {
  868. AddRuneAt (fright, ftop, paddingTop >= 0 ? (paddingRight >= 0 ? uRCorner : hLine) : rightChar);
  869. }
  870. // Outside top-right corner
  871. for (int c = fright + 1; c < fright + paddingRight; c++) {
  872. AddRuneAt (c, ftop, rightChar);
  873. }
  874. // Left, Fill, Right
  875. if (fbottom > ftop) {
  876. for (int r = ftop + 1; r < fbottom; r++) {
  877. // Outside left
  878. for (int c = region.X; c < fleft; c++) {
  879. AddRuneAt (c, r, leftChar);
  880. }
  881. // Frame left
  882. AddRuneAt (fleft, r, paddingLeft > 0 ? vLine : leftChar);
  883. // Fill
  884. if (fill) {
  885. for (int x = fleft + 1; x < fright; x++) {
  886. AddRuneAt (x, r, clearChar);
  887. }
  888. }
  889. // Frame right
  890. if (fright > fleft) {
  891. var v = vLine;
  892. if ((Diagnostics & DiagnosticFlags.FrameRuler) == DiagnosticFlags.FrameRuler) {
  893. v = (char)(((int)'0') + ((r - ftop) % 10)); // vLine;
  894. }
  895. AddRuneAt (fright, r, paddingRight > 0 ? v : rightChar);
  896. }
  897. // Outside right
  898. for (int c = fright + 1; c < fright + paddingRight; c++) {
  899. AddRuneAt (c, r, rightChar);
  900. }
  901. }
  902. // Outside Bottom
  903. for (int c = region.X; c < region.X + region.Width; c++) {
  904. AddRuneAt (c, fbottom, leftChar);
  905. }
  906. // Frame bottom-left
  907. AddRuneAt (fleft, fbottom, paddingLeft > 0 ? lLCorner : leftChar);
  908. if (fright > fleft) {
  909. // Frame bottom
  910. for (int c = fleft + 1; c < fright; c++) {
  911. var h = hLine;
  912. if ((Diagnostics & DiagnosticFlags.FrameRuler) == DiagnosticFlags.FrameRuler) {
  913. h = (char)(((int)'0') + ((c - fleft) % 10)); // hLine;
  914. }
  915. AddRuneAt (c, fbottom, paddingBottom > 0 ? h : bottomChar);
  916. }
  917. // Frame bottom-right
  918. AddRuneAt (fright, fbottom, paddingRight > 0 ? (paddingBottom > 0 ? lRCorner : hLine) : rightChar);
  919. }
  920. // Outside right
  921. for (int c = fright + 1; c < fright + paddingRight; c++) {
  922. AddRuneAt (c, fbottom, rightChar);
  923. }
  924. }
  925. // Out bottom - ensure top is always drawn if we overlap
  926. if (paddingBottom > 0) {
  927. for (int r = fbottom + 1; r < fbottom + paddingBottom; r++) {
  928. for (int c = region.X; c < region.X + region.Width; c++) {
  929. AddRuneAt (c, r, bottomChar);
  930. }
  931. }
  932. }
  933. }
  934. /// <summary>
  935. /// Draws a frame on the specified region with the specified padding around the frame.
  936. /// </summary>
  937. /// <param name="region">Screen relative region where the frame will be drawn.</param>
  938. /// <param name="padding">Padding to add on the sides.</param>
  939. /// <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>
  940. /// <remarks>This API has been superseded by <see cref="DrawWindowFrame(Rect, int, int, int, int, bool, bool, Border)"/>.</remarks>
  941. /// <remarks>This API is equivalent to calling <c>DrawWindowFrame(Rect, p - 1, p - 1, p - 1, p - 1)</c>. In other words,
  942. /// A padding value of 0 means there is actually a one cell border.
  943. /// </remarks>
  944. public virtual void DrawFrame (Rect region, int padding, bool fill)
  945. {
  946. // DrawFrame assumes the border is always at least one row/col thick
  947. // DrawWindowFrame assumes a padding of 0 means NO padding and no frame
  948. DrawWindowFrame (new Rect (region.X, region.Y, region.Width, region.Height),
  949. padding + 1, padding + 1, padding + 1, padding + 1, border: false, fill: fill);
  950. }
  951. /// <summary>
  952. /// Suspend the application, typically needs to save the state, suspend the app and upon return, reset the console driver.
  953. /// </summary>
  954. public abstract void Suspend ();
  955. Rect clip;
  956. /// <summary>
  957. /// Controls the current clipping region that AddRune/AddStr is subject to.
  958. /// </summary>
  959. /// <value>The clip.</value>
  960. public Rect Clip {
  961. get => clip;
  962. set => this.clip = value;
  963. }
  964. /// <summary>
  965. /// Start of mouse moves.
  966. /// </summary>
  967. public abstract void StartReportingMouseMoves ();
  968. /// <summary>
  969. /// Stop reporting mouses moves.
  970. /// </summary>
  971. public abstract void StopReportingMouseMoves ();
  972. /// <summary>
  973. /// Disables the cooked event processing from the mouse driver.
  974. /// At startup, it is assumed mouse events are cooked. Not implemented by any driver: See Issue #2300.
  975. /// </summary>
  976. public abstract void UncookMouse ();
  977. /// <summary>
  978. /// Enables the cooked event processing from the mouse driver. Not implemented by any driver: See Issue #2300.
  979. /// </summary>
  980. public abstract void CookMouse ();
  981. /// <summary>
  982. /// Horizontal line character.
  983. /// </summary>
  984. public Rune HLine = '\u2500';
  985. /// <summary>
  986. /// Vertical line character.
  987. /// </summary>
  988. public Rune VLine = '\u2502';
  989. /// <summary>
  990. /// Stipple pattern
  991. /// </summary>
  992. public Rune Stipple = '\u2591';
  993. /// <summary>
  994. /// Diamond character
  995. /// </summary>
  996. public Rune Diamond = '\u25ca';
  997. /// <summary>
  998. /// Upper left corner
  999. /// </summary>
  1000. public Rune ULCorner = '\u250C';
  1001. /// <summary>
  1002. /// Lower left corner
  1003. /// </summary>
  1004. public Rune LLCorner = '\u2514';
  1005. /// <summary>
  1006. /// Upper right corner
  1007. /// </summary>
  1008. public Rune URCorner = '\u2510';
  1009. /// <summary>
  1010. /// Lower right corner
  1011. /// </summary>
  1012. public Rune LRCorner = '\u2518';
  1013. /// <summary>
  1014. /// Left tee
  1015. /// </summary>
  1016. public Rune LeftTee = '\u251c';
  1017. /// <summary>
  1018. /// Right tee
  1019. /// </summary>
  1020. public Rune RightTee = '\u2524';
  1021. /// <summary>
  1022. /// Top tee
  1023. /// </summary>
  1024. public Rune TopTee = '\u252c';
  1025. /// <summary>
  1026. /// The bottom tee.
  1027. /// </summary>
  1028. public Rune BottomTee = '\u2534';
  1029. /// <summary>
  1030. /// Checkmark.
  1031. /// </summary>
  1032. public Rune Checked = '\u221a';
  1033. /// <summary>
  1034. /// Un-checked checkmark.
  1035. /// </summary>
  1036. public Rune UnChecked = '\u2574';
  1037. /// <summary>
  1038. /// Selected mark.
  1039. /// </summary>
  1040. public Rune Selected = '\u25cf';
  1041. /// <summary>
  1042. /// Un-selected selected mark.
  1043. /// </summary>
  1044. public Rune UnSelected = '\u25cc';
  1045. /// <summary>
  1046. /// Right Arrow.
  1047. /// </summary>
  1048. public Rune RightArrow = '\u25ba';
  1049. /// <summary>
  1050. /// Left Arrow.
  1051. /// </summary>
  1052. public Rune LeftArrow = '\u25c4';
  1053. /// <summary>
  1054. /// Down Arrow.
  1055. /// </summary>
  1056. public Rune DownArrow = '\u25bc';
  1057. /// <summary>
  1058. /// Up Arrow.
  1059. /// </summary>
  1060. public Rune UpArrow = '\u25b2';
  1061. /// <summary>
  1062. /// Left indicator for default action (e.g. for <see cref="Button"/>).
  1063. /// </summary>
  1064. public Rune LeftDefaultIndicator = '\u25e6';
  1065. /// <summary>
  1066. /// Right indicator for default action (e.g. for <see cref="Button"/>).
  1067. /// </summary>
  1068. public Rune RightDefaultIndicator = '\u25e6';
  1069. /// <summary>
  1070. /// Left frame/bracket (e.g. '[' for <see cref="Button"/>).
  1071. /// </summary>
  1072. public Rune LeftBracket = '[';
  1073. /// <summary>
  1074. /// Right frame/bracket (e.g. ']' for <see cref="Button"/>).
  1075. /// </summary>
  1076. public Rune RightBracket = ']';
  1077. /// <summary>
  1078. /// Blocks Segment indicator for meter views (e.g. <see cref="ProgressBar"/>.
  1079. /// </summary>
  1080. public Rune BlocksMeterSegment = '\u258c';
  1081. /// <summary>
  1082. /// Continuous Segment indicator for meter views (e.g. <see cref="ProgressBar"/>.
  1083. /// </summary>
  1084. public Rune ContinuousMeterSegment = '\u2588';
  1085. /// <summary>
  1086. /// Horizontal double line character.
  1087. /// </summary>
  1088. public Rune HDLine = '\u2550';
  1089. /// <summary>
  1090. /// Vertical double line character.
  1091. /// </summary>
  1092. public Rune VDLine = '\u2551';
  1093. /// <summary>
  1094. /// Upper left double corner
  1095. /// </summary>
  1096. public Rune ULDCorner = '\u2554';
  1097. /// <summary>
  1098. /// Lower left double corner
  1099. /// </summary>
  1100. public Rune LLDCorner = '\u255a';
  1101. /// <summary>
  1102. /// Upper right double corner
  1103. /// </summary>
  1104. public Rune URDCorner = '\u2557';
  1105. /// <summary>
  1106. /// Lower right double corner
  1107. /// </summary>
  1108. public Rune LRDCorner = '\u255d';
  1109. /// <summary>
  1110. /// Horizontal line character for rounded corners.
  1111. /// </summary>
  1112. public Rune HRLine = '\u2500';
  1113. /// <summary>
  1114. /// Vertical line character for rounded corners.
  1115. /// </summary>
  1116. public Rune VRLine = '\u2502';
  1117. /// <summary>
  1118. /// Upper left rounded corner
  1119. /// </summary>
  1120. public Rune ULRCorner = '\u256d';
  1121. /// <summary>
  1122. /// Lower left rounded corner
  1123. /// </summary>
  1124. public Rune LLRCorner = '\u2570';
  1125. /// <summary>
  1126. /// Upper right rounded corner
  1127. /// </summary>
  1128. public Rune URRCorner = '\u256e';
  1129. /// <summary>
  1130. /// Lower right rounded corner
  1131. /// </summary>
  1132. public Rune LRRCorner = '\u256f';
  1133. /// <summary>
  1134. /// Make the attribute for the foreground and background colors.
  1135. /// </summary>
  1136. /// <param name="fore">Foreground.</param>
  1137. /// <param name="back">Background.</param>
  1138. /// <returns></returns>
  1139. public abstract Attribute MakeAttribute (Color fore, Color back);
  1140. /// <summary>
  1141. /// Gets the current <see cref="Attribute"/>.
  1142. /// </summary>
  1143. /// <returns>The current attribute.</returns>
  1144. public abstract Attribute GetAttribute ();
  1145. /// <summary>
  1146. /// Make the <see cref="Colors"/> for the <see cref="ColorScheme"/>.
  1147. /// </summary>
  1148. /// <param name="foreground">The foreground color.</param>
  1149. /// <param name="background">The background color.</param>
  1150. /// <returns>The attribute for the foreground and background colors.</returns>
  1151. public abstract Attribute MakeColor (Color foreground, Color background);
  1152. /// <summary>
  1153. /// Create all <see cref="Colors"/> with the <see cref="ColorScheme"/> for the console driver.
  1154. /// </summary>
  1155. /// <param name="supportsColors">Flag indicating if colors are supported.</param>
  1156. public void CreateColors (bool supportsColors = true)
  1157. {
  1158. // BUGBUG: No need to create these instances here as they are created in constructor
  1159. //Colors.TopLevel = new ColorScheme ();
  1160. //Colors.Base = new ColorScheme ();
  1161. //Colors.Dialog = new ColorScheme ();
  1162. //Colors.Menu = new ColorScheme ();
  1163. //Colors.Error = new ColorScheme ();
  1164. if (!supportsColors) {
  1165. return;
  1166. }
  1167. // Define the default color theme only if the user has not defined one.
  1168. Colors.TopLevel.Normal = MakeColor (Color.BrightGreen, Color.Black);
  1169. Colors.TopLevel.Focus = MakeColor (Color.White, Color.Cyan);
  1170. Colors.TopLevel.HotNormal = MakeColor (Color.Brown, Color.Black);
  1171. Colors.TopLevel.HotFocus = MakeColor (Color.Blue, Color.Cyan);
  1172. Colors.TopLevel.Disabled = MakeColor (Color.DarkGray, Color.Black);
  1173. Colors.Base.Normal = MakeColor (Color.White, Color.Blue);
  1174. Colors.Base.Focus = MakeColor (Color.Black, Color.Gray);
  1175. Colors.Base.HotNormal = MakeColor (Color.BrightCyan, Color.Blue);
  1176. Colors.Base.HotFocus = MakeColor (Color.BrightBlue, Color.Gray);
  1177. Colors.Base.Disabled = MakeColor (Color.DarkGray, Color.Blue);
  1178. Colors.Dialog.Normal = MakeColor (Color.Black, Color.Gray);
  1179. Colors.Dialog.Focus = MakeColor (Color.White, Color.DarkGray);
  1180. Colors.Dialog.HotNormal = MakeColor (Color.Blue, Color.Gray);
  1181. Colors.Dialog.HotFocus = MakeColor (Color.BrightYellow, Color.DarkGray);
  1182. Colors.Dialog.Disabled = MakeColor (Color.Gray, Color.DarkGray);
  1183. Colors.Menu.Normal = MakeColor (Color.White, Color.DarkGray);
  1184. Colors.Menu.Focus = MakeColor (Color.White, Color.Black);
  1185. Colors.Menu.HotNormal = MakeColor (Color.BrightYellow, Color.DarkGray);
  1186. Colors.Menu.HotFocus = MakeColor (Color.BrightYellow, Color.Black);
  1187. Colors.Menu.Disabled = MakeColor (Color.Gray, Color.DarkGray);
  1188. Colors.Error.Normal = MakeColor (Color.Red, Color.White);
  1189. Colors.Error.Focus = MakeColor (Color.Black, Color.BrightRed);
  1190. Colors.Error.HotNormal = MakeColor (Color.Black, Color.White);
  1191. Colors.Error.HotFocus = MakeColor (Color.White, Color.BrightRed);
  1192. Colors.Error.Disabled = MakeColor (Color.DarkGray, Color.White);
  1193. }
  1194. }
  1195. /// <summary>
  1196. /// Helper class for console drivers to invoke shell commands to interact with the clipboard.
  1197. /// Used primarily by CursesDriver, but also used in Unit tests which is why it is in
  1198. /// ConsoleDriver.cs.
  1199. /// </summary>
  1200. internal static class ClipboardProcessRunner {
  1201. public static (int exitCode, string result) Bash (string commandLine, string inputText = "", bool waitForOutput = false)
  1202. {
  1203. var arguments = $"-c \"{commandLine}\"";
  1204. var (exitCode, result) = Process ("bash", arguments, inputText, waitForOutput);
  1205. return (exitCode, result.TrimEnd ());
  1206. }
  1207. public static (int exitCode, string result) Process (string cmd, string arguments, string input = null, bool waitForOutput = true)
  1208. {
  1209. var output = string.Empty;
  1210. using (Process process = new Process {
  1211. StartInfo = new ProcessStartInfo {
  1212. FileName = cmd,
  1213. Arguments = arguments,
  1214. RedirectStandardOutput = true,
  1215. RedirectStandardError = true,
  1216. RedirectStandardInput = true,
  1217. UseShellExecute = false,
  1218. CreateNoWindow = true,
  1219. }
  1220. }) {
  1221. var eventHandled = new TaskCompletionSource<bool> ();
  1222. process.Start ();
  1223. if (!string.IsNullOrEmpty (input)) {
  1224. process.StandardInput.Write (input);
  1225. process.StandardInput.Close ();
  1226. }
  1227. if (!process.WaitForExit (5000)) {
  1228. var timeoutError = $@"Process timed out. Command line: {process.StartInfo.FileName} {process.StartInfo.Arguments}.";
  1229. throw new TimeoutException (timeoutError);
  1230. }
  1231. if (waitForOutput && process.StandardOutput.Peek () != -1) {
  1232. output = process.StandardOutput.ReadToEnd ();
  1233. }
  1234. if (process.ExitCode > 0) {
  1235. output = $@"Process failed to run. Command line: {cmd} {arguments}.
  1236. Output: {output}
  1237. Error: {process.StandardError.ReadToEnd ()}";
  1238. }
  1239. return (process.ExitCode, output);
  1240. }
  1241. }
  1242. public static bool DoubleWaitForExit (this System.Diagnostics.Process process)
  1243. {
  1244. var result = process.WaitForExit (500);
  1245. if (result) {
  1246. process.WaitForExit ();
  1247. }
  1248. return result;
  1249. }
  1250. public static bool FileExists (this string value)
  1251. {
  1252. return !string.IsNullOrEmpty (value) && !value.Contains ("not found");
  1253. }
  1254. }
  1255. }