ConsoleDriver.cs 36 KB

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