ConsoleDriver.cs 47 KB

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