ConsoleDriver.cs 44 KB

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