ConsoleDriver.cs 35 KB

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