ConsoleDriver.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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 {
  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. }
  294. /// <summary>
  295. /// The default <see cref="ColorScheme"/>s for the application.
  296. /// </summary>
  297. public static class Colors {
  298. static Colors ()
  299. {
  300. // Use reflection to dynamically create the default set of ColorSchemes from the list defiined
  301. // by the class.
  302. ColorSchemes = typeof (Colors).GetProperties ()
  303. .Where(p => p.PropertyType == typeof(ColorScheme))
  304. .Select (p => new KeyValuePair<string, ColorScheme> (p.Name, new ColorScheme())) // (ColorScheme)p.GetValue (p)))
  305. .ToDictionary (t => t.Key, t => t.Value);
  306. }
  307. /// <summary>
  308. /// The application toplevel color scheme, for the default toplevel views.
  309. /// </summary>
  310. /// <remarks>
  311. /// <para>
  312. /// This API will be deprecated in the future. Use <see cref="Colors.ColorSchemes"/> instead (e.g. <c>edit.ColorScheme = Colors.ColorSchemes["TopLevel"];</c>
  313. /// </para>
  314. /// </remarks>
  315. public static ColorScheme TopLevel { get => GetColorScheme (); set => SetColorScheme (value); }
  316. /// <summary>
  317. /// The base color scheme, for the default toplevel views.
  318. /// </summary>
  319. /// <remarks>
  320. /// <para>
  321. /// This API will be deprecated in the future. Use <see cref="Colors.ColorSchemes"/> instead (e.g. <c>edit.ColorScheme = Colors.ColorSchemes["Base"];</c>
  322. /// </para>
  323. /// </remarks>
  324. public static ColorScheme Base { get => GetColorScheme (); set => SetColorScheme (value); }
  325. /// <summary>
  326. /// The dialog color scheme, for standard popup dialog boxes
  327. /// </summary>
  328. /// <remarks>
  329. /// <para>
  330. /// This API will be deprecated in the future. Use <see cref="Colors.ColorSchemes"/> instead (e.g. <c>edit.ColorScheme = Colors.ColorSchemes["Dialog"];</c>
  331. /// </para>
  332. /// </remarks>
  333. public static ColorScheme Dialog { get => GetColorScheme (); set => SetColorScheme (value); }
  334. /// <summary>
  335. /// The menu bar color
  336. /// </summary>
  337. /// <remarks>
  338. /// <para>
  339. /// This API will be deprecated in the future. Use <see cref="Colors.ColorSchemes"/> instead (e.g. <c>edit.ColorScheme = Colors.ColorSchemes["Menu"];</c>
  340. /// </para>
  341. /// </remarks>
  342. public static ColorScheme Menu { get => GetColorScheme (); set => SetColorScheme (value); }
  343. /// <summary>
  344. /// The color scheme for showing errors.
  345. /// </summary>
  346. /// <remarks>
  347. /// <para>
  348. /// This API will be deprecated in the future. Use <see cref="Colors.ColorSchemes"/> instead (e.g. <c>edit.ColorScheme = Colors.ColorSchemes["Error"];</c>
  349. /// </para>
  350. /// </remarks>
  351. public static ColorScheme Error { get => GetColorScheme (); set => SetColorScheme (value); }
  352. static ColorScheme GetColorScheme([CallerMemberName] string callerMemberName = null)
  353. {
  354. return ColorSchemes [callerMemberName];
  355. }
  356. static void SetColorScheme (ColorScheme colorScheme, [CallerMemberName] string callerMemberName = null)
  357. {
  358. ColorSchemes [callerMemberName] = colorScheme;
  359. colorScheme.caller = callerMemberName;
  360. }
  361. /// <summary>
  362. /// Provides the defined <see cref="ColorScheme"/>s.
  363. /// </summary>
  364. public static Dictionary<string, ColorScheme> ColorSchemes { get; }
  365. }
  366. ///// <summary>
  367. ///// Special characters that can be drawn with
  368. ///// </summary>
  369. //public enum SpecialChar {
  370. // /// <summary>
  371. // /// Horizontal line character.
  372. // /// </summary>
  373. // HLine,
  374. // /// <summary>
  375. // /// Vertical line character.
  376. // /// </summary>
  377. // VLine,
  378. // /// <summary>
  379. // /// Stipple pattern
  380. // /// </summary>
  381. // Stipple,
  382. // /// <summary>
  383. // /// Diamond character
  384. // /// </summary>
  385. // Diamond,
  386. // /// <summary>
  387. // /// Upper left corner
  388. // /// </summary>
  389. // ULCorner,
  390. // /// <summary>
  391. // /// Lower left corner
  392. // /// </summary>
  393. // LLCorner,
  394. // /// <summary>
  395. // /// Upper right corner
  396. // /// </summary>
  397. // URCorner,
  398. // /// <summary>
  399. // /// Lower right corner
  400. // /// </summary>
  401. // LRCorner,
  402. // /// <summary>
  403. // /// Left tee
  404. // /// </summary>
  405. // LeftTee,
  406. // /// <summary>
  407. // /// Right tee
  408. // /// </summary>
  409. // RightTee,
  410. // /// <summary>
  411. // /// Top tee
  412. // /// </summary>
  413. // TopTee,
  414. // /// <summary>
  415. // /// The bottom tee.
  416. // /// </summary>
  417. // BottomTee,
  418. //}
  419. /// <summary>
  420. /// ConsoleDriver is an abstract class that defines the requirements for a console driver.
  421. /// 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.
  422. /// </summary>
  423. public abstract class ConsoleDriver {
  424. /// <summary>
  425. /// The handler fired when the terminal is resized.
  426. /// </summary>
  427. protected Action TerminalResized;
  428. /// <summary>
  429. /// The current number of columns in the terminal.
  430. /// </summary>
  431. public abstract int Cols { get; }
  432. /// <summary>
  433. /// The current number of rows in the terminal.
  434. /// </summary>
  435. public abstract int Rows { get; }
  436. /// <summary>
  437. /// Initializes the driver
  438. /// </summary>
  439. /// <param name="terminalResized">Method to invoke when the terminal is resized.</param>
  440. public abstract void Init (Action terminalResized);
  441. /// <summary>
  442. /// Moves the cursor to the specified column and row.
  443. /// </summary>
  444. /// <param name="col">Column to move the cursor to.</param>
  445. /// <param name="row">Row to move the cursor to.</param>
  446. public abstract void Move (int col, int row);
  447. /// <summary>
  448. /// Adds the specified rune to the display at the current cursor position
  449. /// </summary>
  450. /// <param name="rune">Rune to add.</param>
  451. public abstract void AddRune (Rune rune);
  452. /// <summary>
  453. /// Adds the specified
  454. /// </summary>
  455. /// <param name="str">String.</param>
  456. public abstract void AddStr (ustring str);
  457. /// <summary>
  458. /// Prepare the driver and set the key and mouse events handlers.
  459. /// </summary>
  460. /// <param name="mainLoop">The main loop.</param>
  461. /// <param name="keyHandler">The handler for ProcessKey</param>
  462. /// <param name="keyDownHandler">The handler for key down events</param>
  463. /// <param name="keyUpHandler">The handler for key up events</param>
  464. /// <param name="mouseHandler">The handler for mouse events</param>
  465. public abstract void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler);
  466. /// <summary>
  467. /// Updates the screen to reflect all the changes that have been done to the display buffer
  468. /// </summary>
  469. public abstract void Refresh ();
  470. /// <summary>
  471. /// Updates the location of the cursor position
  472. /// </summary>
  473. public abstract void UpdateCursor ();
  474. /// <summary>
  475. /// Ends the execution of the console driver.
  476. /// </summary>
  477. public abstract void End ();
  478. /// <summary>
  479. /// Redraws the physical screen with the contents that have been queued up via any of the printing commands.
  480. /// </summary>
  481. public abstract void UpdateScreen ();
  482. /// <summary>
  483. /// Selects the specified attribute as the attribute to use for future calls to AddRune, AddString.
  484. /// </summary>
  485. /// <param name="c">C.</param>
  486. public abstract void SetAttribute (Attribute c);
  487. /// <summary>
  488. /// Set Colors from limit sets of colors.
  489. /// </summary>
  490. /// <param name="foreground">Foreground.</param>
  491. /// <param name="background">Background.</param>
  492. public abstract void SetColors (ConsoleColor foreground, ConsoleColor background);
  493. // Advanced uses - set colors to any pre-set pairs, you would need to init_color
  494. // that independently with the R, G, B values.
  495. /// <summary>
  496. /// Advanced uses - set colors to any pre-set pairs, you would need to init_color
  497. /// that independently with the R, G, B values.
  498. /// </summary>
  499. /// <param name="foregroundColorId">Foreground color identifier.</param>
  500. /// <param name="backgroundColorId">Background color identifier.</param>
  501. public abstract void SetColors (short foregroundColorId, short backgroundColorId);
  502. /// <summary>
  503. /// Set the handler when the terminal is resized.
  504. /// </summary>
  505. /// <param name="terminalResized"></param>
  506. public void SetTerminalResized (Action terminalResized)
  507. {
  508. TerminalResized = terminalResized;
  509. }
  510. /// <summary>
  511. /// Draws the title for a Window-style view incorporating padding.
  512. /// </summary>
  513. /// <param name="region">Screen relative region where the frame will be drawn.</param>
  514. /// <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>
  515. /// <param name="paddingLeft">Number of columns to pad on the left (if 0 the border will not appear on the left).</param>
  516. /// <param name="paddingTop">Number of rows to pad on the top (if 0 the border and title will not appear on the top).</param>
  517. /// <param name="paddingRight">Number of columns to pad on the right (if 0 the border will not appear on the right).</param>
  518. /// <param name="paddingBottom">Number of rows to pad on the bottom (if 0 the border will not appear on the bottom).</param>
  519. /// <param name="textAlignment">Not yet immplemented.</param>
  520. /// <remarks></remarks>
  521. public virtual void DrawWindowTitle (Rect region, ustring title, int paddingLeft, int paddingTop, int paddingRight, int paddingBottom, TextAlignment textAlignment = TextAlignment.Left)
  522. {
  523. var width = region.Width - (paddingLeft + 2) * 2;
  524. if (!ustring.IsNullOrEmpty (title) && width > 4 && region.Y + paddingTop <= region.Y + paddingBottom) {
  525. Move (region.X + 1 + paddingLeft, region.Y + paddingTop);
  526. AddRune (' ');
  527. var str = title.Length >= width ? title [0, width - 2] : title;
  528. AddStr (str);
  529. AddRune (' ');
  530. }
  531. }
  532. /// <summary>
  533. /// Enables diagnostic funcions
  534. /// </summary>
  535. [Flags]
  536. public enum DiagnosticFlags : uint {
  537. /// <summary>
  538. /// All diagnostics off
  539. /// </summary>
  540. Off = 0b_0000_0000,
  541. /// <summary>
  542. /// When enabled, <see cref="DrawWindowFrame(Rect, int, int, int, int, bool, bool)"/> will draw a
  543. /// ruler in the frame for any side with a padding value greater than 0.
  544. /// </summary>
  545. FrameRuler = 0b_0000_0001,
  546. /// <summary>
  547. /// When Enabled, <see cref="DrawWindowFrame(Rect, int, int, int, int, bool, bool)"/> will use
  548. /// 'L', 'R', 'T', and 'B' for padding instead of ' '.
  549. /// </summary>
  550. FramePadding = 0b_0000_0010,
  551. }
  552. /// <summary>
  553. /// Set flags to enable/disable <see cref="ConsoleDriver"/> diagnostics.
  554. /// </summary>
  555. public static DiagnosticFlags Diagnostics { get; set; }
  556. /// <summary>
  557. /// Draws a frame for a window with padding and an optional visible border inside the padding.
  558. /// </summary>
  559. /// <param name="region">Screen relative region where the frame will be drawn.</param>
  560. /// <param name="paddingLeft">Number of columns to pad on the left (if 0 the border will not appear on the left).</param>
  561. /// <param name="paddingTop">Number of rows to pad on the top (if 0 the border and title will not appear on the top).</param>
  562. /// <param name="paddingRight">Number of columns to pad on the right (if 0 the border will not appear on the right).</param>
  563. /// <param name="paddingBottom">Number of rows to pad on the bottom (if 0 the border will not appear on the bottom).</param>
  564. /// <param name="border">If set to <c>true</c> and any padding dimension is > 0 the border will be drawn.</param>
  565. /// <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>
  566. public virtual void DrawWindowFrame (Rect region, int paddingLeft = 0, int paddingTop = 0, int paddingRight = 0, int paddingBottom = 0, bool border = true, bool fill = false)
  567. {
  568. char clearChar = ' ';
  569. char leftChar = clearChar;
  570. char rightChar = clearChar;
  571. char topChar = clearChar;
  572. char bottomChar = clearChar;
  573. if ((Diagnostics & DiagnosticFlags.FramePadding) == DiagnosticFlags.FramePadding) {
  574. leftChar = 'L';
  575. rightChar = 'R';
  576. topChar = 'T';
  577. bottomChar = 'B';
  578. clearChar = 'C';
  579. }
  580. void AddRuneAt (int col, int row, Rune ch)
  581. {
  582. Move (col, row);
  583. AddRune (ch);
  584. }
  585. // fwidth is count of hLine chars
  586. int fwidth = (int)(region.Width - (paddingRight + paddingLeft));
  587. // fheight is count of vLine chars
  588. int fheight = (int)(region.Height - (paddingBottom + paddingTop));
  589. // fleft is location of left frame line
  590. int fleft = region.X + paddingLeft - 1;
  591. // fright is location of right frame line
  592. int fright = fleft + fwidth + 1;
  593. // ftop is location of top frame line
  594. int ftop = region.Y + paddingTop - 1;
  595. // fbottom is locaiton of bottom frame line
  596. int fbottom = ftop + fheight + 1;
  597. Rune hLine = border ? HLine : clearChar;
  598. Rune vLine = border ? VLine : clearChar;
  599. Rune uRCorner = border ? URCorner : clearChar;
  600. Rune uLCorner = border ? ULCorner : clearChar;
  601. Rune lLCorner = border ? LLCorner : clearChar;
  602. Rune lRCorner = border ? LRCorner : clearChar;
  603. // Outside top
  604. if (paddingTop > 1) {
  605. for (int r = region.Y; r < ftop; r++) {
  606. for (int c = region.X; c < region.X + region.Width; c++) {
  607. AddRuneAt (c, r, topChar);
  608. }
  609. }
  610. }
  611. // Outside top-left
  612. for (int c = region.X; c < fleft; c++) {
  613. AddRuneAt (c, ftop, leftChar);
  614. }
  615. // Frame top-left corner
  616. AddRuneAt (fleft, ftop, paddingTop >= 0 ? (paddingLeft >= 0 ? uLCorner : hLine) : leftChar);
  617. // Frame top
  618. for (int c = fleft + 1; c < fleft + 1 + fwidth; c++) {
  619. AddRuneAt (c, ftop, paddingTop > 0 ? hLine : topChar);
  620. }
  621. // Frame top-right corner
  622. if (fright > fleft) {
  623. AddRuneAt (fright, ftop, paddingTop >= 0 ? (paddingRight >= 0 ? uRCorner : hLine) : rightChar);
  624. }
  625. // Outside top-right corner
  626. for (int c = fright + 1; c < fright + paddingRight; c++) {
  627. AddRuneAt (c, ftop, rightChar);
  628. }
  629. // Left, Fill, Right
  630. if (fbottom > ftop) {
  631. for (int r = ftop + 1; r < fbottom; r++) {
  632. // Outside left
  633. for (int c = region.X; c < fleft; c++) {
  634. AddRuneAt (c, r, leftChar);
  635. }
  636. // Frame left
  637. AddRuneAt (fleft, r, paddingLeft > 0 ? vLine : leftChar);
  638. // Fill
  639. if (fill) {
  640. for (int x = fleft + 1; x < fright; x++) {
  641. AddRuneAt (x, r, clearChar);
  642. }
  643. }
  644. // Frame right
  645. if (fright > fleft) {
  646. var v = vLine;
  647. if ((Diagnostics & DiagnosticFlags.FrameRuler) == DiagnosticFlags.FrameRuler) {
  648. v = (char)(((int)'0') + ((r - ftop) % 10)); // vLine;
  649. }
  650. AddRuneAt (fright, r, paddingRight > 0 ? v : rightChar);
  651. }
  652. // Outside right
  653. for (int c = fright + 1; c < fright + paddingRight; c++) {
  654. AddRuneAt (c, r, rightChar);
  655. }
  656. }
  657. // Outside Bottom
  658. for (int c = region.X; c < region.X + region.Width; c++) {
  659. AddRuneAt (c, fbottom, leftChar);
  660. }
  661. // Frame bottom-left
  662. AddRuneAt (fleft, fbottom, paddingLeft > 0 ? lLCorner : leftChar);
  663. if (fright > fleft) {
  664. // Frame bottom
  665. for (int c = fleft + 1; c < fright; c++) {
  666. var h = hLine;
  667. if ((Diagnostics & DiagnosticFlags.FrameRuler) == DiagnosticFlags.FrameRuler) {
  668. h = (char)(((int)'0') + ((c - fleft) % 10)); // hLine;
  669. }
  670. AddRuneAt (c, fbottom, paddingBottom > 0 ? h : bottomChar);
  671. }
  672. // Frame bottom-right
  673. AddRuneAt (fright, fbottom, paddingRight > 0 ? (paddingBottom > 0 ? lRCorner : hLine) : rightChar);
  674. }
  675. // Outside right
  676. for (int c = fright + 1; c < fright + paddingRight; c++) {
  677. AddRuneAt (c, fbottom, rightChar);
  678. }
  679. }
  680. // Out bottom - ensure top is always drawn if we overlap
  681. if (paddingBottom > 0) {
  682. for (int r = fbottom + 1; r < fbottom + paddingBottom; r++) {
  683. for (int c = region.X; c < region.X + region.Width; c++) {
  684. AddRuneAt (c, r, bottomChar);
  685. }
  686. }
  687. }
  688. }
  689. /// <summary>
  690. /// Draws a frame on the specified region with the specified padding around the frame.
  691. /// </summary>
  692. /// <param name="region">Screen relative region where the frame will be drawn.</param>
  693. /// <param name="padding">Padding to add on the sides.</param>
  694. /// <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>
  695. /// <remarks>This API has been superceded by <see cref="DrawWindowFrame(Rect, int, int, int, int, bool, bool)"/>.</remarks>
  696. /// <remarks>This API is equivlalent to calling <c>DrawWindowFrame(Rect, p - 1, p - 1, p - 1, p - 1)</c>. In other words,
  697. /// A padding value of 0 means there is actually a one cell border.
  698. /// </remarks>
  699. public virtual void DrawFrame (Rect region, int padding, bool fill)
  700. {
  701. // DrawFrame assumes the border is always at least one row/col thick
  702. // DrawWindowFrame assumes a padding of 0 means NO padding and no frame
  703. DrawWindowFrame (new Rect (region.X, region.Y, region.Width, region.Height),
  704. padding + 1, padding + 1, padding + 1, padding + 1, border: false, fill: fill);
  705. }
  706. /// <summary>
  707. /// Suspend the application, typically needs to save the state, suspend the app and upon return, reset the console driver.
  708. /// </summary>
  709. public abstract void Suspend ();
  710. Rect clip;
  711. /// <summary>
  712. /// Controls the current clipping region that AddRune/AddStr is subject to.
  713. /// </summary>
  714. /// <value>The clip.</value>
  715. public Rect Clip {
  716. get => clip;
  717. set => this.clip = value;
  718. }
  719. /// <summary>
  720. /// Start of mouse moves.
  721. /// </summary>
  722. public abstract void StartReportingMouseMoves ();
  723. /// <summary>
  724. /// Stop reporting mouses moves.
  725. /// </summary>
  726. public abstract void StopReportingMouseMoves ();
  727. /// <summary>
  728. /// Disables the cooked event processing from the mouse driver. At startup, it is assumed mouse events are cooked.
  729. /// </summary>
  730. public abstract void UncookMouse ();
  731. /// <summary>
  732. /// Enables the cooked event processing from the mouse driver
  733. /// </summary>
  734. public abstract void CookMouse ();
  735. /// <summary>
  736. /// Horizontal line character.
  737. /// </summary>
  738. public Rune HLine;
  739. /// <summary>
  740. /// Vertical line character.
  741. /// </summary>
  742. public Rune VLine;
  743. /// <summary>
  744. /// Stipple pattern
  745. /// </summary>
  746. public Rune Stipple;
  747. /// <summary>
  748. /// Diamond character
  749. /// </summary>
  750. public Rune Diamond;
  751. /// <summary>
  752. /// Upper left corner
  753. /// </summary>
  754. public Rune ULCorner;
  755. /// <summary>
  756. /// Lower left corner
  757. /// </summary>
  758. public Rune LLCorner;
  759. /// <summary>
  760. /// Upper right corner
  761. /// </summary>
  762. public Rune URCorner;
  763. /// <summary>
  764. /// Lower right corner
  765. /// </summary>
  766. public Rune LRCorner;
  767. /// <summary>
  768. /// Left tee
  769. /// </summary>
  770. public Rune LeftTee;
  771. /// <summary>
  772. /// Right tee
  773. /// </summary>
  774. public Rune RightTee;
  775. /// <summary>
  776. /// Top tee
  777. /// </summary>
  778. public Rune TopTee;
  779. /// <summary>
  780. /// The bottom tee.
  781. /// </summary>
  782. public Rune BottomTee;
  783. /// <summary>
  784. /// Make the attribute for the foreground and background colors.
  785. /// </summary>
  786. /// <param name="fore">Foreground.</param>
  787. /// <param name="back">Background.</param>
  788. /// <returns></returns>
  789. public abstract Attribute MakeAttribute (Color fore, Color back);
  790. }
  791. }