ConsoleDriver.cs 34 KB

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