EscSeqUtils.cs 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
  1. #nullable enable
  2. using Terminal.Gui.ConsoleDrivers;
  3. namespace Terminal.Gui;
  4. // QUESTION: Should this class be refactored into separate classes for:
  5. // QUESTION: CSI definitions
  6. // QUESTION: Primitives like DecodeEsqReq
  7. // QUESTION: Screen/Color/Cursor handling
  8. // QUESTION: Mouse handling
  9. // QUESTION: Keyboard handling
  10. /// <summary>
  11. /// Provides a platform-independent API for managing ANSI escape sequences.
  12. /// </summary>
  13. /// <remarks>
  14. /// Useful resources:
  15. /// * https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
  16. /// * https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
  17. /// * https://vt100.net/
  18. /// </remarks>
  19. public static class EscSeqUtils
  20. {
  21. // TODO: One type per file - Move this enum to a separate file.
  22. /// <summary>
  23. /// Options for ANSI ESC "[xJ" - Clears part of the screen.
  24. /// </summary>
  25. public enum ClearScreenOptions
  26. {
  27. /// <summary>
  28. /// If n is 0 (or missing), clear from cursor to end of screen.
  29. /// </summary>
  30. CursorToEndOfScreen = 0,
  31. /// <summary>
  32. /// If n is 1, clear from cursor to beginning of the screen.
  33. /// </summary>
  34. CursorToBeginningOfScreen = 1,
  35. /// <summary>
  36. /// If n is 2, clear entire screen (and moves cursor to upper left on DOS ANSI.SYS).
  37. /// </summary>
  38. EntireScreen = 2,
  39. /// <summary>
  40. /// If n is 3, clear entire screen and delete all lines saved in the scrollback buffer
  41. /// </summary>
  42. EntireScreenAndScrollbackBuffer = 3
  43. }
  44. // QUESTION: I wonder if EscSeqUtils.CSI_... should be more strongly typed such that this (and Terminator could be
  45. // QUESTION: public required CSIRequests Request { get; init; }
  46. // QUESTION: public required CSITerminators Terminator { get; init; }
  47. /// <summary>
  48. /// Escape key code (ASCII 27/0x1B).
  49. /// </summary>
  50. public const char KeyEsc = (char)KeyCode.Esc;
  51. /// <summary>
  52. /// ESC [ - The CSI (Control Sequence Introducer).
  53. /// </summary>
  54. public const string CSI = "\u001B[";
  55. /// <summary>
  56. /// ESC [ ? 1047 h - Activate xterm alternative buffer (no backscroll)
  57. /// </summary>
  58. /// <remarks>
  59. /// From
  60. /// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-ordered-by-the-final-character_s_
  61. /// Use Alternate Screen Buffer, xterm.
  62. /// </remarks>
  63. public static readonly string CSI_ActivateAltBufferNoBackscroll = CSI + "?1047h";
  64. /// <summary>
  65. /// ESC [ ? 1003 l - Disable any mouse event tracking.
  66. /// </summary>
  67. public static readonly string CSI_DisableAnyEventMouse = CSI + "?1003l";
  68. /// <summary>
  69. /// ESC [ ? 1006 l - Disable SGR (Select Graphic Rendition).
  70. /// </summary>
  71. public static readonly string CSI_DisableSgrExtModeMouse = CSI + "?1006l";
  72. /// <summary>
  73. /// ESC [ ? 1015 l - Disable URXVT (Unicode Extended Virtual Terminal).
  74. /// </summary>
  75. public static readonly string CSI_DisableUrxvtExtModeMouse = CSI + "?1015l";
  76. /// <summary>
  77. /// ESC [ ? 1003 h - Enable mouse event tracking.
  78. /// </summary>
  79. public static readonly string CSI_EnableAnyEventMouse = CSI + "?1003h";
  80. /// <summary>
  81. /// ESC [ ? 1006 h - Enable SGR (Select Graphic Rendition).
  82. /// </summary>
  83. public static readonly string CSI_EnableSgrExtModeMouse = CSI + "?1006h";
  84. /// <summary>
  85. /// ESC [ ? 1015 h - Enable URXVT (Unicode Extended Virtual Terminal).
  86. /// </summary>
  87. public static readonly string CSI_EnableUrxvtExtModeMouse = CSI + "?1015h";
  88. /// <summary>
  89. /// ESC [ ? 1047 l - Restore xterm working buffer (with backscroll)
  90. /// </summary>
  91. /// <remarks>
  92. /// From
  93. /// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-ordered-by-the-final-character_s_
  94. /// Use Normal Screen Buffer, xterm. Clear the screen first if in the Alternate Screen Buffer.
  95. /// </remarks>
  96. public static readonly string CSI_RestoreAltBufferWithBackscroll = CSI + "?1047l";
  97. /// <summary>
  98. /// ESC [ ? 1049 l - Restore cursor position and restore xterm working buffer (with backscroll)
  99. /// </summary>
  100. /// <remarks>
  101. /// From
  102. /// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-ordered-by-the-final-character_s_
  103. /// Use Normal Screen Buffer and restore cursor as in DECRC, xterm.
  104. /// resource.This combines the effects of the 1047 and 1048 modes.
  105. /// </remarks>
  106. public static readonly string CSI_RestoreCursorAndRestoreAltBufferWithBackscroll = CSI + "?1049l";
  107. /// <summary>
  108. /// ESC [ ? 1049 h - Save cursor position and activate xterm alternative buffer (no backscroll)
  109. /// </summary>
  110. /// <remarks>
  111. /// From
  112. /// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-ordered-by-the-final-character_s_
  113. /// Save cursor as in DECSC, xterm. After saving the cursor, switch to the Alternate Screen Buffer,
  114. /// clearing it first.
  115. /// This control combines the effects of the 1047 and 1048 modes.
  116. /// Use this with terminfo-based applications rather than the 47 mode.
  117. /// </remarks>
  118. public static readonly string CSI_SaveCursorAndActivateAltBufferNoBackscroll = CSI + "?1049h";
  119. //private static bool isButtonReleased;
  120. private static bool _isButtonClicked;
  121. private static bool _isButtonDoubleClicked;
  122. //private static MouseFlags? lastMouseButtonReleased;
  123. // QUESTION: What's the difference between isButtonClicked and isButtonPressed?
  124. // Some clarity or comments would be handy, here.
  125. // It also seems like some enforcement of valid states might be a good idea.
  126. private static bool _isButtonPressed;
  127. private static bool _isButtonTripleClicked;
  128. private static MouseFlags? _lastMouseButtonPressed;
  129. private static Point? _point;
  130. /// <summary>
  131. /// Control sequence for disabling mouse events.
  132. /// </summary>
  133. public static string CSI_DisableMouseEvents { get; set; } =
  134. CSI_DisableAnyEventMouse + CSI_DisableUrxvtExtModeMouse + CSI_DisableSgrExtModeMouse;
  135. /// <summary>
  136. /// Control sequence for enabling mouse events.
  137. /// </summary>
  138. public static string CSI_EnableMouseEvents { get; set; } =
  139. CSI_EnableAnyEventMouse + CSI_EnableUrxvtExtModeMouse + CSI_EnableSgrExtModeMouse;
  140. /// <summary>
  141. /// ESC [ x J - Clears part of the screen. See <see cref="ClearScreenOptions"/>.
  142. /// </summary>
  143. /// <param name="option"></param>
  144. /// <returns></returns>
  145. public static string CSI_ClearScreen (ClearScreenOptions option) { return $"{CSI}{(int)option}J"; }
  146. /// <summary>
  147. /// Specify the incomplete <see cref="ConsoleKeyInfo"/> array not yet recognized as valid ANSI escape sequence.
  148. /// </summary>
  149. public static ConsoleKeyInfo []? IncompleteCkInfos { get; set; }
  150. /// <summary>
  151. /// Represent a response that was requested by an invalid terminator.
  152. /// </summary>
  153. public static string? InvalidRequestTerminator { get; set; }
  154. /// <summary>
  155. /// Decodes an ANSI escape sequence.
  156. /// </summary>
  157. /// <param name="escSeqRequests">The <see cref="EscSeqRequests"/> which may contain a request.</param>
  158. /// <param name="newConsoleKeyInfo">The <see cref="ConsoleKeyInfo"/> which may change.</param>
  159. /// <param name="key">The <see cref="ConsoleKey"/> which may change.</param>
  160. /// <param name="cki">The <see cref="ConsoleKeyInfo"/> array.</param>
  161. /// <param name="mod">The <see cref="ConsoleModifiers"/> which may change.</param>
  162. /// <param name="c1Control">The control returned by the <see cref="GetC1ControlChar"/> method.</param>
  163. /// <param name="code">The code returned by the <see cref="GetEscapeResult(char[])"/> method.</param>
  164. /// <param name="values">The values returned by the <see cref="GetEscapeResult(char[])"/> method.</param>
  165. /// <param name="terminator">The terminator returned by the <see cref="GetEscapeResult(char[])"/> method.</param>
  166. /// <param name="isMouse">Indicates if the escape sequence is a mouse event.</param>
  167. /// <param name="buttonState">The <see cref="MouseFlags"/> button state.</param>
  168. /// <param name="pos">The <see cref="MouseFlags"/> position.</param>
  169. /// <param name="seqReqStatus">The <see cref="EscSeqReqStatus"/> object.</param>
  170. /// <param name="continuousButtonPressedHandler">The handler that will process the event.</param>
  171. public static void DecodeEscSeq (
  172. EscSeqRequests? escSeqRequests,
  173. ref ConsoleKeyInfo newConsoleKeyInfo,
  174. ref ConsoleKey key,
  175. ConsoleKeyInfo [] cki,
  176. ref ConsoleModifiers mod,
  177. out string c1Control,
  178. out string code,
  179. out string [] values,
  180. out string terminator,
  181. out bool isMouse,
  182. out List<MouseFlags> buttonState,
  183. out Point pos,
  184. out EscSeqReqStatus? seqReqStatus,
  185. Action<MouseFlags, Point> continuousButtonPressedHandler
  186. )
  187. {
  188. char [] kChars = GetKeyCharArray (cki);
  189. (c1Control, code, values, terminator) = GetEscapeResult (kChars);
  190. isMouse = false;
  191. buttonState = [0];
  192. pos = default (Point);
  193. seqReqStatus = null;
  194. var keyChar = '\0';
  195. switch (c1Control)
  196. {
  197. case "ESC":
  198. if (values is null && string.IsNullOrEmpty (terminator))
  199. {
  200. key = ConsoleKey.Escape;
  201. newConsoleKeyInfo = new (
  202. cki [0].KeyChar,
  203. key,
  204. (mod & ConsoleModifiers.Shift) != 0,
  205. (mod & ConsoleModifiers.Alt) != 0,
  206. (mod & ConsoleModifiers.Control) != 0);
  207. }
  208. else if ((uint)cki [1].KeyChar >= 1 && (uint)cki [1].KeyChar <= 26 && (uint)cki [1].KeyChar != '\n' && (uint)cki [1].KeyChar != '\r')
  209. {
  210. key = (ConsoleKey)(char)(cki [1].KeyChar + (uint)ConsoleKey.A - 1);
  211. mod = ConsoleModifiers.Alt | ConsoleModifiers.Control;
  212. newConsoleKeyInfo = new (
  213. cki [1].KeyChar,
  214. key,
  215. (mod & ConsoleModifiers.Shift) != 0,
  216. (mod & ConsoleModifiers.Alt) != 0,
  217. (mod & ConsoleModifiers.Control) != 0);
  218. }
  219. else if (cki [1].KeyChar >= 65 && cki [1].KeyChar <= 90)
  220. {
  221. key = (ConsoleKey)cki [1].KeyChar;
  222. mod = ConsoleModifiers.Shift | ConsoleModifiers.Alt;
  223. newConsoleKeyInfo = new (
  224. cki [1].KeyChar,
  225. (ConsoleKey)Math.Min ((uint)key, 255),
  226. (mod & ConsoleModifiers.Shift) != 0,
  227. (mod & ConsoleModifiers.Alt) != 0,
  228. (mod & ConsoleModifiers.Control) != 0);
  229. }
  230. else if (cki [1].KeyChar >= 97 && cki [1].KeyChar <= 122)
  231. {
  232. key = (ConsoleKey)cki [1].KeyChar.ToString ().ToUpper () [0];
  233. mod = ConsoleModifiers.Alt;
  234. newConsoleKeyInfo = new (
  235. cki [1].KeyChar,
  236. (ConsoleKey)Math.Min ((uint)key, 255),
  237. (mod & ConsoleModifiers.Shift) != 0,
  238. (mod & ConsoleModifiers.Alt) != 0,
  239. (mod & ConsoleModifiers.Control) != 0);
  240. }
  241. else if (cki [1].KeyChar is '\0' or ' ')
  242. {
  243. key = ConsoleKey.Spacebar;
  244. if (kChars.Length > 1 && kChars [1] == '\0')
  245. {
  246. mod = ConsoleModifiers.Alt | ConsoleModifiers.Control;
  247. }
  248. else
  249. {
  250. mod = ConsoleModifiers.Shift | ConsoleModifiers.Alt;
  251. }
  252. newConsoleKeyInfo = new (
  253. cki [1].KeyChar,
  254. (ConsoleKey)Math.Min ((uint)key, 255),
  255. (mod & ConsoleModifiers.Shift) != 0,
  256. (mod & ConsoleModifiers.Alt) != 0,
  257. (mod & ConsoleModifiers.Control) != 0);
  258. }
  259. else if (cki [1].KeyChar is '\n' or '\r')
  260. {
  261. key = ConsoleKey.Enter;
  262. if (kChars.Length > 1 && kChars [1] == '\n')
  263. {
  264. mod = ConsoleModifiers.Alt | ConsoleModifiers.Control;
  265. }
  266. else
  267. {
  268. mod = ConsoleModifiers.Shift | ConsoleModifiers.Alt;
  269. }
  270. newConsoleKeyInfo = new (
  271. cki [1].KeyChar,
  272. (ConsoleKey)Math.Min ((uint)key, 255),
  273. (mod & ConsoleModifiers.Shift) != 0,
  274. (mod & ConsoleModifiers.Alt) != 0,
  275. (mod & ConsoleModifiers.Control) != 0);
  276. }
  277. else
  278. {
  279. key = (ConsoleKey)cki [1].KeyChar;
  280. mod = ConsoleModifiers.Alt;
  281. newConsoleKeyInfo = new (
  282. cki [1].KeyChar,
  283. (ConsoleKey)Math.Min ((uint)key, 255),
  284. (mod & ConsoleModifiers.Shift) != 0,
  285. (mod & ConsoleModifiers.Alt) != 0,
  286. (mod & ConsoleModifiers.Control) != 0);
  287. }
  288. break;
  289. case "SS3":
  290. key = GetConsoleKey (terminator [0], values [0], ref mod, ref keyChar);
  291. newConsoleKeyInfo = new (
  292. keyChar,
  293. key,
  294. (mod & ConsoleModifiers.Shift) != 0,
  295. (mod & ConsoleModifiers.Alt) != 0,
  296. (mod & ConsoleModifiers.Control) != 0);
  297. break;
  298. case "CSI":
  299. if (!string.IsNullOrEmpty (code) && code == "<")
  300. {
  301. GetMouse (cki, out buttonState, out pos, continuousButtonPressedHandler);
  302. isMouse = true;
  303. return;
  304. }
  305. if (escSeqRequests is { } && escSeqRequests.HasResponse (terminator, out seqReqStatus))
  306. {
  307. escSeqRequests.Remove (seqReqStatus);
  308. return;
  309. }
  310. if (!string.IsNullOrEmpty (terminator))
  311. {
  312. System.Diagnostics.Debug.Assert (terminator.Length == 1);
  313. key = GetConsoleKey (terminator [0], values [0], ref mod, ref keyChar);
  314. if (key != 0 && values.Length > 1)
  315. {
  316. mod |= GetConsoleModifiers (values [1]);
  317. }
  318. if (keyChar != 0 || key != 0 || mod != 0)
  319. {
  320. newConsoleKeyInfo = new (
  321. keyChar,
  322. key,
  323. (mod & ConsoleModifiers.Shift) != 0,
  324. (mod & ConsoleModifiers.Alt) != 0,
  325. (mod & ConsoleModifiers.Control) != 0);
  326. }
  327. else
  328. {
  329. // It's request response that wasn't handled by a valid request terminator
  330. System.Diagnostics.Debug.Assert (escSeqRequests is null or { Statuses.Count: > 0 });
  331. InvalidRequestTerminator = ToString (cki);
  332. }
  333. }
  334. else
  335. {
  336. // BUGBUG: See https://github.com/gui-cs/Terminal.Gui/issues/2803
  337. // This is caused by NetDriver depending on Console.KeyAvailable?
  338. //throw new InvalidOperationException ("CSI response, but there's no terminator");
  339. IncompleteCkInfos = cki;
  340. }
  341. break;
  342. default:
  343. newConsoleKeyInfo = MapConsoleKeyInfo (cki [0]);
  344. key = newConsoleKeyInfo.Key;
  345. mod = newConsoleKeyInfo.Modifiers;
  346. break;
  347. }
  348. }
  349. /// <summary>
  350. /// Gets the c1Control used in the called escape sequence.
  351. /// </summary>
  352. /// <param name="c">The char used.</param>
  353. /// <returns>The c1Control.</returns>
  354. [Pure]
  355. public static string GetC1ControlChar (in char c)
  356. {
  357. // These control characters are used in the vtXXX emulation.
  358. return c switch
  359. {
  360. 'D' => "IND", // Index
  361. 'E' => "NEL", // Next Line
  362. 'H' => "HTS", // Tab Set
  363. 'M' => "RI", // Reverse Index
  364. 'N' => "SS2", // Single Shift Select of G2 Character Set: affects next character only
  365. 'O' => "SS3", // Single Shift Select of G3 Character Set: affects next character only
  366. 'P' => "DCS", // Device Control String
  367. 'V' => "SPA", // Start of Guarded Area
  368. 'W' => "EPA", // End of Guarded Area
  369. 'X' => "SOS", // Start of String
  370. 'Z' => "DECID", // Return Terminal ID Obsolete form of CSI c (DA)
  371. '[' => "CSI", // Control Sequence Introducer
  372. '\\' => "ST", // String Terminator
  373. ']' => "OSC", // Operating System Command
  374. '^' => "PM", // Privacy Message
  375. '_' => "APC", // Application Program Command
  376. _ => string.Empty
  377. };
  378. }
  379. /// <summary>
  380. /// Gets the <see cref="ConsoleKey"/> depending on terminating and value.
  381. /// </summary>
  382. /// <param name="terminator">
  383. /// The terminator indicating a reply to <see cref="CSI_SendDeviceAttributes"/> or
  384. /// <see cref="CSI_SendDeviceAttributes2"/>.
  385. /// </param>
  386. /// <param name="value">The value.</param>
  387. /// <param name="mod">The <see cref="ConsoleModifiers"/> which may change.</param>
  388. /// <param name="keyChar">Normally is '\0' but on some cases may need other value.</param>
  389. /// <returns>The <see cref="ConsoleKey"/> and probably the <see cref="ConsoleModifiers"/>.</returns>
  390. public static ConsoleKey GetConsoleKey (char terminator, string? value, ref ConsoleModifiers mod, ref char keyChar)
  391. {
  392. if (terminator == 'Z')
  393. {
  394. mod |= ConsoleModifiers.Shift;
  395. }
  396. if (terminator == 'l')
  397. {
  398. keyChar = '+';
  399. }
  400. if (terminator == 'm')
  401. {
  402. keyChar = '-';
  403. }
  404. return (terminator, value) switch
  405. {
  406. ('A', _) => ConsoleKey.UpArrow,
  407. ('B', _) => ConsoleKey.DownArrow,
  408. ('C', _) => ConsoleKey.RightArrow,
  409. ('D', _) => ConsoleKey.LeftArrow,
  410. ('E', _) => ConsoleKey.Clear,
  411. ('F', _) => ConsoleKey.End,
  412. ('H', _) => ConsoleKey.Home,
  413. ('P', _) => ConsoleKey.F1,
  414. ('Q', _) => ConsoleKey.F2,
  415. ('R', _) => ConsoleKey.F3,
  416. ('S', _) => ConsoleKey.F4,
  417. ('Z', _) => ConsoleKey.Tab,
  418. ('~', "2") => ConsoleKey.Insert,
  419. ('~', "3") => ConsoleKey.Delete,
  420. ('~', "5") => ConsoleKey.PageUp,
  421. ('~', "6") => ConsoleKey.PageDown,
  422. ('~', "15") => ConsoleKey.F5,
  423. ('~', "17") => ConsoleKey.F6,
  424. ('~', "18") => ConsoleKey.F7,
  425. ('~', "19") => ConsoleKey.F8,
  426. ('~', "20") => ConsoleKey.F9,
  427. ('~', "21") => ConsoleKey.F10,
  428. ('~', "23") => ConsoleKey.F11,
  429. ('~', "24") => ConsoleKey.F12,
  430. // These terminators are used by macOS on a numeric keypad without keys modifiers
  431. ('l', null) => ConsoleKey.Add,
  432. ('m', null) => ConsoleKey.Subtract,
  433. ('p', null) => ConsoleKey.Insert,
  434. ('q', null) => ConsoleKey.End,
  435. ('r', null) => ConsoleKey.DownArrow,
  436. ('s', null) => ConsoleKey.PageDown,
  437. ('t', null) => ConsoleKey.LeftArrow,
  438. ('u', null) => ConsoleKey.Clear,
  439. ('v', null) => ConsoleKey.RightArrow,
  440. ('w', null) => ConsoleKey.Home,
  441. ('x', null) => ConsoleKey.UpArrow,
  442. ('y', null) => ConsoleKey.PageUp,
  443. (_, _) => 0
  444. };
  445. }
  446. /// <summary>
  447. /// Gets the <see cref="ConsoleModifiers"/> from the value.
  448. /// </summary>
  449. /// <param name="value">The value.</param>
  450. /// <returns>The <see cref="ConsoleModifiers"/> or zero.</returns>
  451. public static ConsoleModifiers GetConsoleModifiers (string? value)
  452. {
  453. return value switch
  454. {
  455. "2" => ConsoleModifiers.Shift,
  456. "3" => ConsoleModifiers.Alt,
  457. "4" => ConsoleModifiers.Shift | ConsoleModifiers.Alt,
  458. "5" => ConsoleModifiers.Control,
  459. "6" => ConsoleModifiers.Shift | ConsoleModifiers.Control,
  460. "7" => ConsoleModifiers.Alt | ConsoleModifiers.Control,
  461. "8" => ConsoleModifiers.Shift | ConsoleModifiers.Alt | ConsoleModifiers.Control,
  462. _ => 0
  463. };
  464. }
  465. #nullable restore
  466. /// <summary>
  467. /// Gets all the needed information about an escape sequence.
  468. /// </summary>
  469. /// <param name="kChar">The array with all chars.</param>
  470. /// <returns>
  471. /// The c1Control returned by <see cref="GetC1ControlChar"/>, code, values and terminating.
  472. /// </returns>
  473. public static (string c1Control, string code, string [] values, string terminating) GetEscapeResult (char [] kChar)
  474. {
  475. if (kChar is null || kChar.Length == 0 || (kChar.Length == 1 && kChar [0] != KeyEsc))
  476. {
  477. return (null, null, null, null);
  478. }
  479. if (kChar [0] != KeyEsc)
  480. {
  481. throw new InvalidOperationException ("Invalid escape character!");
  482. }
  483. if (kChar.Length == 1)
  484. {
  485. return ("ESC", null, null, null);
  486. }
  487. if (kChar.Length == 2)
  488. {
  489. return ("ESC", null, null, kChar [1].ToString ());
  490. }
  491. string c1Control = GetC1ControlChar (kChar [1]);
  492. string code = null;
  493. int nSep = kChar.Count (static x => x == ';') + 1;
  494. var values = new string [nSep];
  495. var valueIdx = 0;
  496. var terminating = string.Empty;
  497. for (var i = 2; i < kChar.Length; i++)
  498. {
  499. char c = kChar [i];
  500. if (char.IsDigit (c))
  501. {
  502. // PERF: Ouch
  503. values [valueIdx] += c.ToString ();
  504. }
  505. else if (c == ';')
  506. {
  507. valueIdx++;
  508. }
  509. else if (valueIdx == nSep - 1 || i == kChar.Length - 1)
  510. {
  511. // PERF: Ouch
  512. terminating += c.ToString ();
  513. }
  514. else
  515. {
  516. // PERF: Ouch
  517. code += c.ToString ();
  518. }
  519. }
  520. return (c1Control, code, values, terminating);
  521. }
  522. /// <summary>
  523. /// A helper to get only the <see cref="ConsoleKeyInfo.KeyChar"/> from the <see cref="ConsoleKeyInfo"/> array.
  524. /// </summary>
  525. /// <param name="cki"></param>
  526. /// <returns>The char array of the escape sequence.</returns>
  527. // PERF: This is expensive
  528. public static char [] GetKeyCharArray (ConsoleKeyInfo [] cki)
  529. {
  530. char [] kChar = [];
  531. var length = 0;
  532. foreach (ConsoleKeyInfo kc in cki)
  533. {
  534. length++;
  535. Array.Resize (ref kChar, length);
  536. kChar [length - 1] = kc.KeyChar;
  537. }
  538. return kChar;
  539. }
  540. /// <summary>
  541. /// Gets the <see cref="MouseFlags"/> mouse button flags and the position.
  542. /// </summary>
  543. /// <param name="cki">The <see cref="ConsoleKeyInfo"/> array.</param>
  544. /// <param name="mouseFlags">The mouse button flags.</param>
  545. /// <param name="pos">The mouse position.</param>
  546. /// <param name="continuousButtonPressedHandler">The handler that will process the event.</param>
  547. public static void GetMouse (
  548. ConsoleKeyInfo [] cki,
  549. out List<MouseFlags> mouseFlags,
  550. out Point pos,
  551. Action<MouseFlags, Point> continuousButtonPressedHandler
  552. )
  553. {
  554. MouseFlags buttonState = 0;
  555. pos = Point.Empty;
  556. var buttonCode = 0;
  557. var foundButtonCode = false;
  558. var foundPoint = 0;
  559. string value = string.Empty;
  560. char [] kChar = GetKeyCharArray (cki);
  561. // PERF: This loop could benefit from use of Spans and other strategies to avoid copies.
  562. //System.Diagnostics.Debug.WriteLine ($"kChar: {new string (kChar)}");
  563. for (var i = 0; i < kChar.Length; i++)
  564. {
  565. // PERF: Copy
  566. char c = kChar [i];
  567. if (c == '<')
  568. {
  569. foundButtonCode = true;
  570. }
  571. else if (foundButtonCode && c != ';')
  572. {
  573. // PERF: Ouch
  574. value += c.ToString ();
  575. }
  576. else if (c == ';')
  577. {
  578. if (foundButtonCode)
  579. {
  580. foundButtonCode = false;
  581. buttonCode = int.Parse (value);
  582. }
  583. if (foundPoint == 1)
  584. {
  585. pos.X = int.Parse (value) - 1;
  586. }
  587. value = string.Empty;
  588. foundPoint++;
  589. }
  590. else if (foundPoint > 0 && c != 'm' && c != 'M')
  591. {
  592. value += c.ToString ();
  593. }
  594. else if (c == 'm' || c == 'M')
  595. {
  596. //pos.Y = int.Parse (value) + Console.WindowTop - 1;
  597. pos.Y = int.Parse (value) - 1;
  598. switch (buttonCode)
  599. {
  600. case 0:
  601. case 8:
  602. case 16:
  603. case 24:
  604. case 32:
  605. case 36:
  606. case 40:
  607. case 48:
  608. case 56:
  609. buttonState = c == 'M'
  610. ? MouseFlags.Button1Pressed
  611. : MouseFlags.Button1Released;
  612. break;
  613. case 1:
  614. case 9:
  615. case 17:
  616. case 25:
  617. case 33:
  618. case 37:
  619. case 41:
  620. case 45:
  621. case 49:
  622. case 53:
  623. case 57:
  624. case 61:
  625. buttonState = c == 'M'
  626. ? MouseFlags.Button2Pressed
  627. : MouseFlags.Button2Released;
  628. break;
  629. case 2:
  630. case 10:
  631. case 14:
  632. case 18:
  633. case 22:
  634. case 26:
  635. case 30:
  636. case 34:
  637. case 42:
  638. case 46:
  639. case 50:
  640. case 54:
  641. case 58:
  642. case 62:
  643. buttonState = c == 'M'
  644. ? MouseFlags.Button3Pressed
  645. : MouseFlags.Button3Released;
  646. break;
  647. case 35:
  648. //// Needed for Windows OS
  649. //if (isButtonPressed && c == 'm'
  650. // && (lastMouseEvent.ButtonState == MouseFlags.Button1Pressed
  651. // || lastMouseEvent.ButtonState == MouseFlags.Button2Pressed
  652. // || lastMouseEvent.ButtonState == MouseFlags.Button3Pressed)) {
  653. // switch (lastMouseEvent.ButtonState) {
  654. // case MouseFlags.Button1Pressed:
  655. // buttonState = MouseFlags.Button1Released;
  656. // break;
  657. // case MouseFlags.Button2Pressed:
  658. // buttonState = MouseFlags.Button2Released;
  659. // break;
  660. // case MouseFlags.Button3Pressed:
  661. // buttonState = MouseFlags.Button3Released;
  662. // break;
  663. // }
  664. //} else {
  665. // buttonState = MouseFlags.ReportMousePosition;
  666. //}
  667. //break;
  668. case 39:
  669. case 43:
  670. case 47:
  671. case 51:
  672. case 55:
  673. case 59:
  674. case 63:
  675. buttonState = MouseFlags.ReportMousePosition;
  676. break;
  677. case 64:
  678. buttonState = MouseFlags.WheeledUp;
  679. break;
  680. case 65:
  681. buttonState = MouseFlags.WheeledDown;
  682. break;
  683. case 68:
  684. case 72:
  685. case 80:
  686. buttonState = MouseFlags.WheeledLeft; // Shift/Ctrl+WheeledUp
  687. break;
  688. case 69:
  689. case 73:
  690. case 81:
  691. buttonState = MouseFlags.WheeledRight; // Shift/Ctrl+WheeledDown
  692. break;
  693. }
  694. // Modifiers.
  695. switch (buttonCode)
  696. {
  697. case 8:
  698. case 9:
  699. case 10:
  700. case 43:
  701. buttonState |= MouseFlags.ButtonAlt;
  702. break;
  703. case 14:
  704. case 47:
  705. buttonState |= MouseFlags.ButtonAlt | MouseFlags.ButtonShift;
  706. break;
  707. case 16:
  708. case 17:
  709. case 18:
  710. case 51:
  711. buttonState |= MouseFlags.ButtonCtrl;
  712. break;
  713. case 22:
  714. case 55:
  715. buttonState |= MouseFlags.ButtonCtrl | MouseFlags.ButtonShift;
  716. break;
  717. case 24:
  718. case 25:
  719. case 26:
  720. case 59:
  721. buttonState |= MouseFlags.ButtonCtrl | MouseFlags.ButtonAlt;
  722. break;
  723. case 30:
  724. case 63:
  725. buttonState |= MouseFlags.ButtonCtrl | MouseFlags.ButtonShift | MouseFlags.ButtonAlt;
  726. break;
  727. case 32:
  728. case 33:
  729. case 34:
  730. buttonState |= MouseFlags.ReportMousePosition;
  731. break;
  732. case 36:
  733. case 37:
  734. buttonState |= MouseFlags.ReportMousePosition | MouseFlags.ButtonShift;
  735. break;
  736. case 39:
  737. case 68:
  738. case 69:
  739. buttonState |= MouseFlags.ButtonShift;
  740. break;
  741. case 40:
  742. case 41:
  743. case 42:
  744. buttonState |= MouseFlags.ReportMousePosition | MouseFlags.ButtonAlt;
  745. break;
  746. case 45:
  747. case 46:
  748. buttonState |= MouseFlags.ReportMousePosition | MouseFlags.ButtonAlt | MouseFlags.ButtonShift;
  749. break;
  750. case 48:
  751. case 49:
  752. case 50:
  753. buttonState |= MouseFlags.ReportMousePosition | MouseFlags.ButtonCtrl;
  754. break;
  755. case 53:
  756. case 54:
  757. buttonState |= MouseFlags.ReportMousePosition | MouseFlags.ButtonCtrl | MouseFlags.ButtonShift;
  758. break;
  759. case 56:
  760. case 57:
  761. case 58:
  762. buttonState |= MouseFlags.ReportMousePosition | MouseFlags.ButtonCtrl | MouseFlags.ButtonAlt;
  763. break;
  764. case 61:
  765. case 62:
  766. buttonState |= MouseFlags.ReportMousePosition | MouseFlags.ButtonCtrl | MouseFlags.ButtonShift | MouseFlags.ButtonAlt;
  767. break;
  768. }
  769. }
  770. }
  771. mouseFlags = [MouseFlags.AllEvents];
  772. if (_lastMouseButtonPressed != null
  773. && !_isButtonPressed
  774. && !buttonState.HasFlag (MouseFlags.ReportMousePosition)
  775. && !buttonState.HasFlag (MouseFlags.Button1Released)
  776. && !buttonState.HasFlag (MouseFlags.Button2Released)
  777. && !buttonState.HasFlag (MouseFlags.Button3Released)
  778. && !buttonState.HasFlag (MouseFlags.Button4Released))
  779. {
  780. _lastMouseButtonPressed = null;
  781. _isButtonPressed = false;
  782. }
  783. if ((!_isButtonClicked
  784. && !_isButtonDoubleClicked
  785. && (buttonState == MouseFlags.Button1Pressed
  786. || buttonState == MouseFlags.Button2Pressed
  787. || buttonState == MouseFlags.Button3Pressed
  788. || buttonState == MouseFlags.Button4Pressed)
  789. && _lastMouseButtonPressed is null)
  790. || (_isButtonPressed && _lastMouseButtonPressed is { } && buttonState.HasFlag (MouseFlags.ReportMousePosition)))
  791. {
  792. mouseFlags [0] = buttonState;
  793. _lastMouseButtonPressed = buttonState;
  794. _isButtonPressed = true;
  795. _point = pos;
  796. if ((mouseFlags [0] & MouseFlags.ReportMousePosition) == 0)
  797. {
  798. Application.MainLoop?.AddIdle (
  799. () =>
  800. {
  801. // INTENT: What's this trying to do?
  802. // The task itself is not awaited.
  803. Task.Run (
  804. async () => await ProcessContinuousButtonPressedAsync (
  805. buttonState,
  806. continuousButtonPressedHandler));
  807. return false;
  808. });
  809. }
  810. else if (mouseFlags [0].HasFlag (MouseFlags.ReportMousePosition))
  811. {
  812. _point = pos;
  813. // The isButtonPressed must always be true, otherwise we can lose the feature
  814. // If mouse flags has ReportMousePosition this feature won't run
  815. // but is always prepared with the new location
  816. //isButtonPressed = false;
  817. }
  818. }
  819. else if (_isButtonDoubleClicked
  820. && (buttonState == MouseFlags.Button1Pressed
  821. || buttonState == MouseFlags.Button2Pressed
  822. || buttonState == MouseFlags.Button3Pressed
  823. || buttonState == MouseFlags.Button4Pressed))
  824. {
  825. mouseFlags [0] = GetButtonTripleClicked (buttonState);
  826. _isButtonDoubleClicked = false;
  827. _isButtonTripleClicked = true;
  828. }
  829. else if (_isButtonClicked
  830. && (buttonState == MouseFlags.Button1Pressed
  831. || buttonState == MouseFlags.Button2Pressed
  832. || buttonState == MouseFlags.Button3Pressed
  833. || buttonState == MouseFlags.Button4Pressed))
  834. {
  835. mouseFlags [0] = GetButtonDoubleClicked (buttonState);
  836. _isButtonClicked = false;
  837. _isButtonDoubleClicked = true;
  838. Application.MainLoop?.AddIdle (
  839. () =>
  840. {
  841. Task.Run (async () => await ProcessButtonDoubleClickedAsync ());
  842. return false;
  843. });
  844. }
  845. //else if (isButtonReleased && !isButtonClicked && buttonState == MouseFlags.ReportMousePosition) {
  846. // mouseFlag [0] = GetButtonClicked ((MouseFlags)lastMouseButtonReleased);
  847. // lastMouseButtonReleased = null;
  848. // isButtonReleased = false;
  849. // isButtonClicked = true;
  850. // Application.MainLoop.AddIdle (() => {
  851. // Task.Run (async () => await ProcessButtonClickedAsync ());
  852. // return false;
  853. // });
  854. //}
  855. else if (!_isButtonClicked
  856. && !_isButtonDoubleClicked
  857. && (buttonState == MouseFlags.Button1Released
  858. || buttonState == MouseFlags.Button2Released
  859. || buttonState == MouseFlags.Button3Released
  860. || buttonState == MouseFlags.Button4Released))
  861. {
  862. mouseFlags [0] = buttonState;
  863. _isButtonPressed = false;
  864. if (_isButtonTripleClicked)
  865. {
  866. _isButtonTripleClicked = false;
  867. }
  868. else if (pos.X == _point?.X && pos.Y == _point?.Y)
  869. {
  870. mouseFlags.Add (GetButtonClicked (buttonState));
  871. _isButtonClicked = true;
  872. Application.MainLoop?.AddIdle (
  873. () =>
  874. {
  875. Task.Run (async () => await ProcessButtonClickedAsync ());
  876. return false;
  877. });
  878. }
  879. _point = pos;
  880. //if ((lastMouseButtonPressed & MouseFlags.ReportMousePosition) == 0) {
  881. // lastMouseButtonReleased = buttonState;
  882. // isButtonPressed = false;
  883. // isButtonReleased = true;
  884. //} else {
  885. // lastMouseButtonPressed = null;
  886. // isButtonPressed = false;
  887. //}
  888. }
  889. else if (buttonState == MouseFlags.WheeledUp)
  890. {
  891. mouseFlags [0] = MouseFlags.WheeledUp;
  892. }
  893. else if (buttonState == MouseFlags.WheeledDown)
  894. {
  895. mouseFlags [0] = MouseFlags.WheeledDown;
  896. }
  897. else if (buttonState == MouseFlags.WheeledLeft)
  898. {
  899. mouseFlags [0] = MouseFlags.WheeledLeft;
  900. }
  901. else if (buttonState == MouseFlags.WheeledRight)
  902. {
  903. mouseFlags [0] = MouseFlags.WheeledRight;
  904. }
  905. else if (buttonState == MouseFlags.ReportMousePosition)
  906. {
  907. mouseFlags [0] = MouseFlags.ReportMousePosition;
  908. }
  909. else
  910. {
  911. mouseFlags [0] = buttonState;
  912. //foreach (var flag in buttonState.GetUniqueFlags()) {
  913. // mouseFlag [0] |= flag;
  914. //}
  915. }
  916. mouseFlags [0] = SetControlKeyStates (buttonState, mouseFlags [0]);
  917. //buttonState = mouseFlags;
  918. //System.Diagnostics.Debug.WriteLine ($"buttonState: {buttonState} X: {pos.X} Y: {pos.Y}");
  919. //foreach (var mf in mouseFlags) {
  920. // System.Diagnostics.Debug.WriteLine ($"mouseFlags: {mf} X: {pos.X} Y: {pos.Y}");
  921. //}
  922. }
  923. /// <summary>
  924. /// Ensures a console key is mapped to one that works correctly with ANSI escape sequences.
  925. /// </summary>
  926. /// <param name="consoleKeyInfo">The <see cref="ConsoleKeyInfo"/>.</param>
  927. /// <returns>The <see cref="ConsoleKeyInfo"/> modified.</returns>
  928. public static ConsoleKeyInfo MapConsoleKeyInfo (ConsoleKeyInfo consoleKeyInfo)
  929. {
  930. ConsoleKeyInfo newConsoleKeyInfo = consoleKeyInfo;
  931. ConsoleKey key = ConsoleKey.None;
  932. char keyChar = consoleKeyInfo.KeyChar;
  933. switch ((uint)keyChar)
  934. {
  935. case 0:
  936. if (consoleKeyInfo.Key == (ConsoleKey)64)
  937. { // Ctrl+Space in Windows.
  938. newConsoleKeyInfo = new (
  939. consoleKeyInfo.KeyChar,
  940. ConsoleKey.Spacebar,
  941. (consoleKeyInfo.Modifiers & ConsoleModifiers.Shift) != 0,
  942. (consoleKeyInfo.Modifiers & ConsoleModifiers.Alt) != 0,
  943. (consoleKeyInfo.Modifiers & ConsoleModifiers.Control) != 0);
  944. }
  945. else if (consoleKeyInfo.Key == ConsoleKey.None)
  946. {
  947. newConsoleKeyInfo = new (
  948. consoleKeyInfo.KeyChar,
  949. ConsoleKey.Spacebar,
  950. (consoleKeyInfo.Modifiers & ConsoleModifiers.Shift) != 0,
  951. (consoleKeyInfo.Modifiers & ConsoleModifiers.Alt) != 0,
  952. true);
  953. }
  954. break;
  955. case uint n when n is > 0 and <= KeyEsc:
  956. if (consoleKeyInfo is { Key: 0, KeyChar: '\t' })
  957. {
  958. key = ConsoleKey.Tab;
  959. newConsoleKeyInfo = new (
  960. consoleKeyInfo.KeyChar,
  961. key,
  962. (consoleKeyInfo.Modifiers & ConsoleModifiers.Shift) != 0,
  963. (consoleKeyInfo.Modifiers & ConsoleModifiers.Alt) != 0,
  964. (consoleKeyInfo.Modifiers & ConsoleModifiers.Control) != 0);
  965. }
  966. else if (consoleKeyInfo is { Key: 0, KeyChar: '\r' })
  967. {
  968. key = ConsoleKey.Enter;
  969. newConsoleKeyInfo = new (
  970. consoleKeyInfo.KeyChar,
  971. key,
  972. (consoleKeyInfo.Modifiers & ConsoleModifiers.Shift) != 0,
  973. (consoleKeyInfo.Modifiers & ConsoleModifiers.Alt) != 0,
  974. (consoleKeyInfo.Modifiers & ConsoleModifiers.Control) != 0);
  975. }
  976. else if (consoleKeyInfo is { Key: 0, KeyChar: '\n' })
  977. {
  978. key = ConsoleKey.Enter;
  979. newConsoleKeyInfo = new (
  980. consoleKeyInfo.KeyChar,
  981. key,
  982. (consoleKeyInfo.Modifiers & ConsoleModifiers.Shift) != 0,
  983. (consoleKeyInfo.Modifiers & ConsoleModifiers.Alt) != 0,
  984. true);
  985. }
  986. else if (consoleKeyInfo.Key == 0)
  987. {
  988. key = (ConsoleKey)(char)(consoleKeyInfo.KeyChar + (uint)ConsoleKey.A - 1);
  989. newConsoleKeyInfo = new (
  990. consoleKeyInfo.KeyChar,
  991. key,
  992. (consoleKeyInfo.Modifiers & ConsoleModifiers.Shift) != 0,
  993. (consoleKeyInfo.Modifiers & ConsoleModifiers.Alt) != 0,
  994. true);
  995. }
  996. break;
  997. case 127: // DEL
  998. key = ConsoleKey.Backspace;
  999. newConsoleKeyInfo = new (
  1000. consoleKeyInfo.KeyChar,
  1001. key,
  1002. (consoleKeyInfo.Modifiers & ConsoleModifiers.Shift) != 0,
  1003. (consoleKeyInfo.Modifiers & ConsoleModifiers.Alt) != 0,
  1004. (consoleKeyInfo.Modifiers & ConsoleModifiers.Control) != 0);
  1005. break;
  1006. default:
  1007. uint ck = ConsoleKeyMapping.MapKeyCodeToConsoleKey ((KeyCode)consoleKeyInfo.KeyChar, out bool isConsoleKey);
  1008. if (isConsoleKey)
  1009. {
  1010. key = (ConsoleKey)ck;
  1011. }
  1012. newConsoleKeyInfo = new (
  1013. consoleKeyInfo.KeyChar,
  1014. key,
  1015. GetShiftMod (consoleKeyInfo.Modifiers),
  1016. (consoleKeyInfo.Modifiers & ConsoleModifiers.Alt) != 0,
  1017. (consoleKeyInfo.Modifiers & ConsoleModifiers.Control) != 0);
  1018. break;
  1019. }
  1020. return newConsoleKeyInfo;
  1021. bool GetShiftMod (ConsoleModifiers modifiers)
  1022. {
  1023. if (consoleKeyInfo.KeyChar is >= (char)ConsoleKey.A and <= (char)ConsoleKey.Z && modifiers == ConsoleModifiers.None)
  1024. {
  1025. return true;
  1026. }
  1027. return (modifiers & ConsoleModifiers.Shift) != 0;
  1028. }
  1029. }
  1030. private static MouseFlags _lastMouseFlags;
  1031. /// <summary>
  1032. /// Provides a handler to be invoked when mouse continuous button pressed is processed.
  1033. /// </summary>
  1034. public static event EventHandler<MouseEventArgs> ContinuousButtonPressed;
  1035. /// <summary>
  1036. /// Provides a default mouse event handler that can be used by any driver.
  1037. /// </summary>
  1038. /// <param name="mouseFlag">The mouse flags event.</param>
  1039. /// <param name="pos">The mouse position.</param>
  1040. public static void ProcessMouseEvent (MouseFlags mouseFlag, Point pos)
  1041. {
  1042. bool WasButtonReleased (MouseFlags flag)
  1043. {
  1044. return flag.HasFlag (MouseFlags.Button1Released)
  1045. || flag.HasFlag (MouseFlags.Button2Released)
  1046. || flag.HasFlag (MouseFlags.Button3Released)
  1047. || flag.HasFlag (MouseFlags.Button4Released);
  1048. }
  1049. bool IsButtonNotPressed (MouseFlags flag)
  1050. {
  1051. return !flag.HasFlag (MouseFlags.Button1Pressed)
  1052. && !flag.HasFlag (MouseFlags.Button2Pressed)
  1053. && !flag.HasFlag (MouseFlags.Button3Pressed)
  1054. && !flag.HasFlag (MouseFlags.Button4Pressed);
  1055. }
  1056. bool IsButtonClickedOrDoubleClicked (MouseFlags flag)
  1057. {
  1058. return flag.HasFlag (MouseFlags.Button1Clicked)
  1059. || flag.HasFlag (MouseFlags.Button2Clicked)
  1060. || flag.HasFlag (MouseFlags.Button3Clicked)
  1061. || flag.HasFlag (MouseFlags.Button4Clicked)
  1062. || flag.HasFlag (MouseFlags.Button1DoubleClicked)
  1063. || flag.HasFlag (MouseFlags.Button2DoubleClicked)
  1064. || flag.HasFlag (MouseFlags.Button3DoubleClicked)
  1065. || flag.HasFlag (MouseFlags.Button4DoubleClicked);
  1066. }
  1067. if ((WasButtonReleased (mouseFlag) && IsButtonNotPressed (_lastMouseFlags)) || (IsButtonClickedOrDoubleClicked (mouseFlag) && _lastMouseFlags == 0))
  1068. {
  1069. return;
  1070. }
  1071. _lastMouseFlags = mouseFlag;
  1072. var me = new MouseEventArgs { Flags = mouseFlag, Position = pos };
  1073. ContinuousButtonPressed?.Invoke ((mouseFlag, pos), me);
  1074. }
  1075. /// <summary>
  1076. /// A helper to resize the <see cref="ConsoleKeyInfo"/> as needed.
  1077. /// </summary>
  1078. /// <param name="consoleKeyInfo">The <see cref="ConsoleKeyInfo"/>.</param>
  1079. /// <param name="cki">The <see cref="ConsoleKeyInfo"/> array to resize.</param>
  1080. /// <returns>The <see cref="ConsoleKeyInfo"/> resized.</returns>
  1081. public static ConsoleKeyInfo [] ResizeArray (ConsoleKeyInfo consoleKeyInfo, ConsoleKeyInfo [] cki)
  1082. {
  1083. Array.Resize (ref cki, cki is null ? 1 : cki.Length + 1);
  1084. cki [^1] = consoleKeyInfo;
  1085. return cki;
  1086. }
  1087. /// <summary>
  1088. /// Insert a <see cref="ConsoleKeyInfo"/> array into the another <see cref="ConsoleKeyInfo"/> array at the specified
  1089. /// index.
  1090. /// </summary>
  1091. /// <param name="toInsert">The array to insert.</param>
  1092. /// <param name="cki">The array where will be added the array.</param>
  1093. /// <param name="index">The start index to insert the array, default is 0.</param>
  1094. /// <returns>The <see cref="ConsoleKeyInfo"/> array with another array inserted.</returns>
  1095. public static ConsoleKeyInfo [] InsertArray ([CanBeNull] ConsoleKeyInfo [] toInsert, ConsoleKeyInfo [] cki, int index = 0)
  1096. {
  1097. if (toInsert is null)
  1098. {
  1099. return cki;
  1100. }
  1101. if (cki is null)
  1102. {
  1103. return toInsert;
  1104. }
  1105. if (index < 0)
  1106. {
  1107. index = 0;
  1108. }
  1109. ConsoleKeyInfo [] backupCki = cki.Clone () as ConsoleKeyInfo [];
  1110. Array.Resize (ref cki, cki.Length + toInsert.Length);
  1111. for (var i = 0; i < cki.Length; i++)
  1112. {
  1113. if (i == index)
  1114. {
  1115. for (var j = 0; j < toInsert.Length; j++)
  1116. {
  1117. cki [i] = toInsert [j];
  1118. i++;
  1119. }
  1120. for (int k = index; k < backupCki!.Length; k++)
  1121. {
  1122. cki [i] = backupCki [k];
  1123. i++;
  1124. }
  1125. }
  1126. else
  1127. {
  1128. cki [i] = backupCki! [i];
  1129. }
  1130. }
  1131. return cki;
  1132. }
  1133. private static MouseFlags GetButtonClicked (MouseFlags mouseFlag)
  1134. {
  1135. MouseFlags mf = default;
  1136. switch (mouseFlag)
  1137. {
  1138. case MouseFlags.Button1Released:
  1139. mf = MouseFlags.Button1Clicked;
  1140. break;
  1141. case MouseFlags.Button2Released:
  1142. mf = MouseFlags.Button2Clicked;
  1143. break;
  1144. case MouseFlags.Button3Released:
  1145. mf = MouseFlags.Button3Clicked;
  1146. break;
  1147. }
  1148. return mf;
  1149. }
  1150. private static MouseFlags GetButtonDoubleClicked (MouseFlags mouseFlag)
  1151. {
  1152. MouseFlags mf = default;
  1153. switch (mouseFlag)
  1154. {
  1155. case MouseFlags.Button1Pressed:
  1156. mf = MouseFlags.Button1DoubleClicked;
  1157. break;
  1158. case MouseFlags.Button2Pressed:
  1159. mf = MouseFlags.Button2DoubleClicked;
  1160. break;
  1161. case MouseFlags.Button3Pressed:
  1162. mf = MouseFlags.Button3DoubleClicked;
  1163. break;
  1164. }
  1165. return mf;
  1166. }
  1167. private static MouseFlags GetButtonTripleClicked (MouseFlags mouseFlag)
  1168. {
  1169. MouseFlags mf = default;
  1170. switch (mouseFlag)
  1171. {
  1172. case MouseFlags.Button1Pressed:
  1173. mf = MouseFlags.Button1TripleClicked;
  1174. break;
  1175. case MouseFlags.Button2Pressed:
  1176. mf = MouseFlags.Button2TripleClicked;
  1177. break;
  1178. case MouseFlags.Button3Pressed:
  1179. mf = MouseFlags.Button3TripleClicked;
  1180. break;
  1181. }
  1182. return mf;
  1183. }
  1184. private static async Task ProcessButtonClickedAsync ()
  1185. {
  1186. await Task.Delay (300);
  1187. _isButtonClicked = false;
  1188. }
  1189. private static async Task ProcessButtonDoubleClickedAsync ()
  1190. {
  1191. await Task.Delay (300);
  1192. _isButtonDoubleClicked = false;
  1193. }
  1194. private static async Task ProcessContinuousButtonPressedAsync (MouseFlags mouseFlag, Action<MouseFlags, Point> continuousButtonPressedHandler)
  1195. {
  1196. // PERF: Pause and poll in a hot loop.
  1197. // This should be replaced with event dispatch and a synchronization primitive such as AutoResetEvent.
  1198. // Will make a massive difference in responsiveness.
  1199. while (_isButtonPressed)
  1200. {
  1201. await Task.Delay (100);
  1202. View view = Application.WantContinuousButtonPressedView;
  1203. if (view is null)
  1204. {
  1205. break;
  1206. }
  1207. if (_isButtonPressed && _lastMouseButtonPressed is { } && (mouseFlag & MouseFlags.ReportMousePosition) == 0)
  1208. {
  1209. Application.Invoke (() => continuousButtonPressedHandler (mouseFlag, _point ?? Point.Empty));
  1210. }
  1211. }
  1212. }
  1213. private static MouseFlags SetControlKeyStates (MouseFlags buttonState, MouseFlags mouseFlag)
  1214. {
  1215. if ((buttonState & MouseFlags.ButtonCtrl) != 0 && (mouseFlag & MouseFlags.ButtonCtrl) == 0)
  1216. {
  1217. mouseFlag |= MouseFlags.ButtonCtrl;
  1218. }
  1219. if ((buttonState & MouseFlags.ButtonShift) != 0 && (mouseFlag & MouseFlags.ButtonShift) == 0)
  1220. {
  1221. mouseFlag |= MouseFlags.ButtonShift;
  1222. }
  1223. if ((buttonState & MouseFlags.ButtonAlt) != 0 && (mouseFlag & MouseFlags.ButtonAlt) == 0)
  1224. {
  1225. mouseFlag |= MouseFlags.ButtonAlt;
  1226. }
  1227. return mouseFlag;
  1228. }
  1229. /// <summary>
  1230. /// Split a raw string into a list of string with the correct ansi escape sequence.
  1231. /// </summary>
  1232. /// <param name="rawData">The raw string containing one or many ansi escape sequence.</param>
  1233. /// <returns>A list with a valid ansi escape sequence.</returns>
  1234. public static List<string> SplitEscapeRawString (string rawData)
  1235. {
  1236. List<string> splitList = [];
  1237. var isEscSeq = false;
  1238. var split = string.Empty;
  1239. char previousChar = '\0';
  1240. for (var i = 0; i < rawData.Length; i++)
  1241. {
  1242. char c = rawData [i];
  1243. if (c == '\u001B')
  1244. {
  1245. isEscSeq = true;
  1246. split = AddAndClearSplit ();
  1247. split += c.ToString ();
  1248. }
  1249. else if (!isEscSeq && c >= Key.Space)
  1250. {
  1251. split = AddAndClearSplit ();
  1252. splitList.Add (c.ToString ());
  1253. }
  1254. else if ((previousChar != '\u001B' && c <= Key.Space) || (previousChar != '\u001B' && c == 127)
  1255. || (char.IsLetter (previousChar) && char.IsLower (c) && char.IsLetter (c))
  1256. || (!string.IsNullOrEmpty (split) && split.Length > 2 && char.IsLetter (previousChar) && char.IsLetter (c)))
  1257. {
  1258. isEscSeq = false;
  1259. split = AddAndClearSplit ();
  1260. splitList.Add (c.ToString ());
  1261. }
  1262. else
  1263. {
  1264. split += c.ToString ();
  1265. }
  1266. if (!string.IsNullOrEmpty (split) && i == rawData.Length - 1)
  1267. {
  1268. splitList.Add (split);
  1269. }
  1270. previousChar = c;
  1271. }
  1272. return splitList;
  1273. string AddAndClearSplit ()
  1274. {
  1275. if (!string.IsNullOrEmpty (split))
  1276. {
  1277. splitList.Add (split);
  1278. split = string.Empty;
  1279. }
  1280. return split;
  1281. }
  1282. }
  1283. /// <summary>
  1284. /// Convert a <see cref="ConsoleKeyInfo"/> array to string.
  1285. /// </summary>
  1286. /// <param name="consoleKeyInfos"></param>
  1287. /// <returns>The string representing the array.</returns>
  1288. public static string ToString (ConsoleKeyInfo [] consoleKeyInfos)
  1289. {
  1290. StringBuilder sb = new ();
  1291. foreach (ConsoleKeyInfo keyChar in consoleKeyInfos)
  1292. {
  1293. sb.Append (keyChar.KeyChar);
  1294. }
  1295. return sb.ToString ();
  1296. }
  1297. /// <summary>
  1298. /// Convert a string to <see cref="ConsoleKeyInfo"/> array.
  1299. /// </summary>
  1300. /// <param name="ansi"></param>
  1301. /// <returns>The <see cref="ConsoleKeyInfo"/>representing the string.</returns>
  1302. public static ConsoleKeyInfo [] ToConsoleKeyInfoArray (string ansi)
  1303. {
  1304. if (ansi is null)
  1305. {
  1306. return null;
  1307. }
  1308. ConsoleKeyInfo [] cki = new ConsoleKeyInfo [ansi.Length];
  1309. for (var i = 0; i < ansi.Length; i++)
  1310. {
  1311. char c = ansi [i];
  1312. cki [i] = new (c, 0, false, false, false);
  1313. }
  1314. return cki;
  1315. }
  1316. #region Cursor
  1317. //ESC [ M - RI Reverse Index – Performs the reverse operation of \n, moves cursor up one line, maintains horizontal position, scrolls buffer if necessary*
  1318. /// <summary>
  1319. /// ESC [ 7 - Save Cursor Position in Memory**
  1320. /// </summary>
  1321. public static readonly string CSI_SaveCursorPosition = CSI + "7";
  1322. /// <summary>
  1323. /// ESC [ 8 - DECSR Restore Cursor Position from Memory**
  1324. /// </summary>
  1325. public static readonly string CSI_RestoreCursorPosition = CSI + "8";
  1326. /// <summary>
  1327. /// ESC [ 8 ; height ; width t - Set Terminal Window Size
  1328. /// https://terminalguide.namepad.de/seq/csi_st-8/
  1329. /// </summary>
  1330. public static string CSI_SetTerminalWindowSize (int height, int width) { return $"{CSI}8;{height};{width}t"; }
  1331. //ESC [ < n > A - CUU - Cursor Up Cursor up by < n >
  1332. //ESC [ < n > B - CUD - Cursor Down Cursor down by < n >
  1333. //ESC [ < n > C - CUF - Cursor Forward Cursor forward (Right) by < n >
  1334. //ESC [ < n > D - CUB - Cursor Backward Cursor backward (Left) by < n >
  1335. //ESC [ < n > E - CNL - Cursor Next Line - Cursor down < n > lines from current position
  1336. //ESC [ < n > F - CPL - Cursor Previous Line Cursor up < n > lines from current position
  1337. //ESC [ < n > G - CHA - Cursor Horizontal Absolute Cursor moves to < n > th position horizontally in the current line
  1338. //ESC [ < n > d - VPA - Vertical Line Position Absolute Cursor moves to the < n > th position vertically in the current column
  1339. /// <summary>
  1340. /// ESC [ y ; x H - CUP Cursor Position - Cursor moves to x ; y coordinate within the viewport, where x is the column
  1341. /// of the y line
  1342. /// </summary>
  1343. /// <param name="row">Origin is (1,1).</param>
  1344. /// <param name="col">Origin is (1,1).</param>
  1345. /// <returns></returns>
  1346. public static string CSI_SetCursorPosition (int row, int col) { return $"{CSI}{row};{col}H"; }
  1347. //ESC [ <y> ; <x> f - HVP Horizontal Vertical Position* Cursor moves to<x>; <y> coordinate within the viewport, where <x> is the column of the<y> line
  1348. //ESC [ s - ANSISYSSC Save Cursor – Ansi.sys emulation **With no parameters, performs a save cursor operation like DECSC
  1349. //ESC [ u - ANSISYSRC Restore Cursor – Ansi.sys emulation **With no parameters, performs a restore cursor operation like DECRC
  1350. //ESC [ ? 12 h - ATT160 Text Cursor Enable Blinking Start the cursor blinking
  1351. //ESC [ ? 12 l - ATT160 Text Cursor Disable Blinking Stop blinking the cursor
  1352. /// <summary>
  1353. /// ESC [ ? 25 h - DECTCEM Text Cursor Enable Mode Show Show the cursor
  1354. /// </summary>
  1355. public static readonly string CSI_ShowCursor = CSI + "?25h";
  1356. /// <summary>
  1357. /// ESC [ ? 25 l - DECTCEM Text Cursor Enable Mode Hide Hide the cursor
  1358. /// </summary>
  1359. public static readonly string CSI_HideCursor = CSI + "?25l";
  1360. //ESC [ ? 12 h - ATT160 Text Cursor Enable Blinking Start the cursor blinking
  1361. //ESC [ ? 12 l - ATT160 Text Cursor Disable Blinking Stop blinking the cursor
  1362. //ESC [ ? 25 h - DECTCEM Text Cursor Enable Mode Show Show the cursor
  1363. //ESC [ ? 25 l - DECTCEM Text Cursor Enable Mode Hide Hide the cursor
  1364. /// <summary>
  1365. /// Styles for ANSI ESC "[x q" - Set Cursor Style
  1366. /// </summary>
  1367. public enum DECSCUSR_Style
  1368. {
  1369. /// <summary>
  1370. /// DECSCUSR - User Shape - Default cursor shape configured by the user
  1371. /// </summary>
  1372. UserShape = 0,
  1373. /// <summary>
  1374. /// DECSCUSR - Blinking Block - Blinking block cursor shape
  1375. /// </summary>
  1376. BlinkingBlock = 1,
  1377. /// <summary>
  1378. /// DECSCUSR - Steady Block - Steady block cursor shape
  1379. /// </summary>
  1380. SteadyBlock = 2,
  1381. /// <summary>
  1382. /// DECSCUSR - Blinking Underline - Blinking underline cursor shape
  1383. /// </summary>
  1384. BlinkingUnderline = 3,
  1385. /// <summary>
  1386. /// DECSCUSR - Steady Underline - Steady underline cursor shape
  1387. /// </summary>
  1388. SteadyUnderline = 4,
  1389. /// <summary>
  1390. /// DECSCUSR - Blinking Bar - Blinking bar cursor shape
  1391. /// </summary>
  1392. BlinkingBar = 5,
  1393. /// <summary>
  1394. /// DECSCUSR - Steady Bar - Steady bar cursor shape
  1395. /// </summary>
  1396. SteadyBar = 6
  1397. }
  1398. /// <summary>
  1399. /// ESC [ n SP q - Select Cursor Style (DECSCUSR)
  1400. /// https://terminalguide.namepad.de/seq/csi_sq_t_space/
  1401. /// </summary>
  1402. /// <param name="style"></param>
  1403. /// <returns></returns>
  1404. public static string CSI_SetCursorStyle (DECSCUSR_Style style) { return $"{CSI}{(int)style} q"; }
  1405. #endregion
  1406. #region Colors
  1407. /// <summary>
  1408. /// ESC [ (n) m - SGR - Set Graphics Rendition - Set the format of the screen and text as specified by (n)
  1409. /// This command is special in that the (n) position can accept between 0 and 16 parameters separated by semicolons.
  1410. /// When no parameters are specified, it is treated the same as a single 0 parameter.
  1411. /// https://terminalguide.namepad.de/seq/csi_sm/
  1412. /// </summary>
  1413. public static string CSI_SetGraphicsRendition (params int [] parameters) { return $"{CSI}{string.Join (";", parameters)}m"; }
  1414. /// <summary>
  1415. /// ESC [ (n) m - Uses <see cref="CSI_SetGraphicsRendition(int[])"/> to set the foreground color.
  1416. /// </summary>
  1417. /// <param name="code">One of the 16 color codes.</param>
  1418. /// <returns></returns>
  1419. public static string CSI_SetForegroundColor (AnsiColorCode code) { return CSI_SetGraphicsRendition ((int)code); }
  1420. /// <summary>
  1421. /// ESC [ (n) m - Uses <see cref="CSI_SetGraphicsRendition(int[])"/> to set the background color.
  1422. /// </summary>
  1423. /// <param name="code">One of the 16 color codes.</param>
  1424. /// <returns></returns>
  1425. public static string CSI_SetBackgroundColor (AnsiColorCode code) { return CSI_SetGraphicsRendition ((int)code + 10); }
  1426. /// <summary>
  1427. /// ESC[38;5;{id}m - Set foreground color (256 colors)
  1428. /// </summary>
  1429. public static string CSI_SetForegroundColor256 (int color) { return $"{CSI}38;5;{color}m"; }
  1430. /// <summary>
  1431. /// ESC[48;5;{id}m - Set background color (256 colors)
  1432. /// </summary>
  1433. public static string CSI_SetBackgroundColor256 (int color) { return $"{CSI}48;5;{color}m"; }
  1434. /// <summary>
  1435. /// ESC[38;2;{r};{g};{b}m Set foreground color as RGB.
  1436. /// </summary>
  1437. public static string CSI_SetForegroundColorRGB (int r, int g, int b) { return $"{CSI}38;2;{r};{g};{b}m"; }
  1438. /// <summary>
  1439. /// ESC[48;2;{r};{g};{b}m Set background color as RGB.
  1440. /// </summary>
  1441. public static string CSI_SetBackgroundColorRGB (int r, int g, int b) { return $"{CSI}48;2;{r};{g};{b}m"; }
  1442. #endregion
  1443. #region Requests
  1444. /// <summary>
  1445. /// ESC [ ? 6 n - Request Cursor Position Report (?) (DECXCPR)
  1446. /// https://terminalguide.namepad.de/seq/csi_sn__p-6/
  1447. /// The terminal reply to <see cref="CSI_RequestCursorPositionReport"/>. ESC [ ? (y) ; (x) ; 1 R
  1448. /// </summary>
  1449. public static readonly AnsiEscapeSequenceRequest CSI_RequestCursorPositionReport = new () { Request = CSI + "?6n", Terminator = "R" };
  1450. /// <summary>
  1451. /// ESC [ 0 c - Send Device Attributes (Primary DA)
  1452. /// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Application-Program-Command-functions
  1453. /// https://www.xfree86.org/current/ctlseqs.html
  1454. /// Windows Terminal v1.17 and below emits “\x1b[?1;0c”, indicating "VT101 with No Options".
  1455. /// Windows Terminal v1.18+ emits: \x1b[?61;6;7;22;23;24;28;32;42c"
  1456. /// See https://github.com/microsoft/terminal/pull/14906
  1457. /// 61 - The device conforms to level 1 of the character cell display architecture
  1458. /// (See https://github.com/microsoft/terminal/issues/15693#issuecomment-1633304497)
  1459. /// 6 = Selective erase
  1460. /// 7 = Soft fonts
  1461. /// 22 = Color text
  1462. /// 23 = Greek character sets
  1463. /// 24 = Turkish character sets
  1464. /// 28 = Rectangular area operations
  1465. /// 32 = Text macros
  1466. /// 42 = ISO Latin-2 character set
  1467. /// The terminator indicating a reply to <see cref="CSI_SendDeviceAttributes"/> or
  1468. /// <see cref="CSI_SendDeviceAttributes2"/>
  1469. /// </summary>
  1470. public static readonly AnsiEscapeSequenceRequest CSI_SendDeviceAttributes = new () { Request = CSI + "0c", Terminator = "c" };
  1471. /// <summary>
  1472. /// ESC [ > 0 c - Send Device Attributes (Secondary DA)
  1473. /// Windows Terminal v1.18+ emits: "\x1b[>0;10;1c" (vt100, firmware version 1.0, vt220)
  1474. /// The terminator indicating a reply to <see cref="CSI_SendDeviceAttributes"/> or
  1475. /// <see cref="CSI_SendDeviceAttributes2"/>
  1476. /// </summary>
  1477. public static readonly AnsiEscapeSequenceRequest CSI_SendDeviceAttributes2 = new () { Request = CSI + ">0c", Terminator = "c" };
  1478. /*
  1479. TODO: depends on https://github.com/gui-cs/Terminal.Gui/pull/3768
  1480. /// <summary>
  1481. /// CSI 16 t - Request sixel resolution (width and height in pixels)
  1482. /// </summary>
  1483. public static readonly AnsiEscapeSequenceRequest CSI_RequestSixelResolution = new () { Request = CSI + "16t", Terminator = "t" };
  1484. /// <summary>
  1485. /// CSI 14 t - Request window size in pixels (width x height)
  1486. /// </summary>
  1487. public static readonly AnsiEscapeSequenceRequest CSI_RequestWindowSizeInPixels = new () { Request = CSI + "14t", Terminator = "t" };
  1488. */
  1489. /// <summary>
  1490. /// CSI 1 8 t | yes | yes | yes | report window size in chars
  1491. /// https://terminalguide.namepad.de/seq/csi_st-18/
  1492. /// The terminator indicating a reply to <see cref="CSI_ReportTerminalSizeInChars"/> : ESC [ 8 ; height ; width t
  1493. /// </summary>
  1494. public static readonly AnsiEscapeSequenceRequest CSI_ReportTerminalSizeInChars = new () { Request = CSI + "18t", Terminator = "t", ExpectedResponseValue = "8" };
  1495. #endregion
  1496. }