ConsoleDriver.cs 37 KB

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