ConsoleDriver.cs 34 KB

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