ConsoleDriver.cs 45 KB

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