AnsiEscapeSequenceRequest.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Describes an ongoing ANSI request sent to the console.
  5. /// Use <see cref="ResponseReceived"/> to handle the response
  6. /// when console answers the request.
  7. /// </summary>
  8. public class AnsiEscapeSequenceRequest
  9. {
  10. internal readonly object _responseLock = new (); // Per-instance lock
  11. /// <summary>
  12. /// Request to send e.g. see
  13. /// <see>
  14. /// <cref>EscSeqUtils.CSI_SendDeviceAttributes.Request</cref>
  15. /// </see>
  16. /// </summary>
  17. public required string Request { get; init; }
  18. /// <summary>
  19. /// Response received from the request.
  20. /// </summary>
  21. public string Response { get; internal set; } = string.Empty;
  22. /// <summary>
  23. /// Invoked when the console responds with an ANSI response code that matches the
  24. /// <see cref="Terminator"/>
  25. /// </summary>
  26. public event EventHandler<AnsiEscapeSequenceResponse>? ResponseReceived;
  27. /// <summary>
  28. /// <para>
  29. /// The terminator that uniquely identifies the type of response as responded
  30. /// by the console. e.g. for
  31. /// <see>
  32. /// <cref>EscSeqUtils.CSI_SendDeviceAttributes.Request</cref>
  33. /// </see>
  34. /// the terminator is
  35. /// <see>
  36. /// <cref>EscSeqUtils.CSI_SendDeviceAttributes.Terminator</cref>
  37. /// </see>
  38. /// .
  39. /// </para>
  40. /// <para>
  41. /// After sending a request, the first response with matching terminator will be matched
  42. /// to the oldest outstanding request.
  43. /// </para>
  44. /// </summary>
  45. public required string Terminator { get; init; }
  46. /// <summary>
  47. /// Execute an ANSI escape sequence escape which may return a response or error.
  48. /// </summary>
  49. /// <param name="ansiRequest">The ANSI escape sequence to request.</param>
  50. /// <param name="result">
  51. /// When this method returns <see langword="true"/>, an object containing the response with an empty
  52. /// error.
  53. /// </param>
  54. /// <returns>A <see cref="AnsiEscapeSequenceResponse"/> with the response, error, terminator and value.</returns>
  55. public static bool TryExecuteAnsiRequest (AnsiEscapeSequenceRequest ansiRequest, out AnsiEscapeSequenceResponse result)
  56. {
  57. var error = new StringBuilder ();
  58. var savedIsReportingMouseMoves = false;
  59. ConsoleDriver? driver = null;
  60. var values = new string? [] { null };
  61. try
  62. {
  63. driver = Application.Driver;
  64. savedIsReportingMouseMoves = driver!.IsReportingMouseMoves;
  65. if (savedIsReportingMouseMoves)
  66. {
  67. driver.StopReportingMouseMoves ();
  68. }
  69. // Send the ANSI escape sequence
  70. ansiRequest.Response = driver.WriteAnsiRequest (ansiRequest);
  71. if (!string.IsNullOrEmpty (ansiRequest.Response) && !ansiRequest.Response.StartsWith (EscSeqUtils.KeyEsc))
  72. {
  73. throw new InvalidOperationException ("Invalid escape character!");
  74. }
  75. if (string.IsNullOrEmpty (ansiRequest.Terminator))
  76. {
  77. throw new InvalidOperationException ("Terminator request is empty.");
  78. }
  79. if (!ansiRequest.Response.EndsWith (ansiRequest.Terminator [^1]))
  80. {
  81. char resp = string.IsNullOrEmpty (ansiRequest.Response) ? ' ' : ansiRequest.Response.Last ();
  82. throw new InvalidOperationException ($"Terminator ends with '{resp}'\nand doesn't end with: '{ansiRequest.Terminator [^1]}'");
  83. }
  84. }
  85. catch (Exception ex)
  86. {
  87. error.AppendLine ($"Error executing ANSI request:\n{ex.Message}");
  88. }
  89. finally
  90. {
  91. if (string.IsNullOrEmpty (error.ToString ()))
  92. {
  93. (string? c1Control, string? code, values, string? terminator) = EscSeqUtils.GetEscapeResult (ansiRequest.Response.ToCharArray ());
  94. }
  95. if (savedIsReportingMouseMoves)
  96. {
  97. driver?.StartReportingMouseMoves ();
  98. }
  99. }
  100. AnsiEscapeSequenceResponse ansiResponse = new ()
  101. {
  102. Response = ansiRequest.Response, Error = error.ToString (),
  103. Terminator = string.IsNullOrEmpty (ansiRequest.Response) ? "" : ansiRequest.Response [^1].ToString (), Value = values [0]
  104. };
  105. // Invoke the event if it's subscribed
  106. ansiRequest.ResponseReceived?.Invoke (ansiRequest, ansiResponse);
  107. result = ansiResponse;
  108. return string.IsNullOrWhiteSpace (result.Error) && !string.IsNullOrWhiteSpace (result.Response);
  109. }
  110. /// <summary>
  111. /// The value expected in the response e.g.
  112. /// <see>
  113. /// <cref>EscSeqUtils.CSI_ReportTerminalSizeInChars.Value</cref>
  114. /// </see>
  115. /// which will have a 't' as terminator but also other different request may return the same terminator with a
  116. /// different value.
  117. /// </summary>
  118. public string? Value { get; init; }
  119. internal void RaiseResponseFromInput (AnsiEscapeSequenceRequest ansiRequest, string response) { ResponseFromInput?.Invoke (ansiRequest, response); }
  120. internal event EventHandler<string>? ResponseFromInput;
  121. }