ConsoleDriver.cs 42 KB

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