AnsiEscapeSequenceRequest.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 the console answers the request.
  7. /// </summary>
  8. public class AnsiEscapeSequenceRequest
  9. {
  10. internal readonly object _responseLock = new (); // Per-instance lock
  11. /// <summary>
  12. /// Gets the request string 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. // QUESTION: Could the type of this propperty be AnsiEscapeSequenceResponse? This would remove the
  19. // QUESTION: removal of the redundant Rresponse, Terminator, and ExpectedRespnseValue properties from this class?
  20. // QUESTION: Does string.Empty indicate no response recevied? If not, perhaps make this property nullable?
  21. /// <summary>
  22. /// Gets the response received from the request.
  23. /// </summary>
  24. public string? Response { get; internal set; }
  25. /// <summary>
  26. /// Raised when the console responds with an ANSI response code that matches the
  27. /// <see cref="Terminator"/>
  28. /// </summary>
  29. public event EventHandler<AnsiEscapeSequenceResponse>? ResponseReceived;
  30. /// <summary>
  31. /// <para>
  32. /// Gets the terminator that uniquely identifies the response received from
  33. /// the console. e.g. for
  34. /// <see>
  35. /// <cref>EscSeqUtils.CSI_SendDeviceAttributes.Request</cref>
  36. /// </see>
  37. /// the terminator is
  38. /// <see>
  39. /// <cref>EscSeqUtils.CSI_SendDeviceAttributes.Terminator</cref>
  40. /// </see>
  41. /// .
  42. /// </para>
  43. /// <para>
  44. /// After sending a request, the first response with matching terminator will be matched
  45. /// to the oldest outstanding request.
  46. /// </para>
  47. /// </summary>
  48. public required string Terminator { get; init; }
  49. /// <summary>
  50. /// Attempt an ANSI escape sequence request which may return a response or error.
  51. /// </summary>
  52. /// <param name="ansiRequest">The ANSI escape sequence to request.</param>
  53. /// <param name="result">
  54. /// When this method returns <see langword="true"/>, the response. <see cref="AnsiEscapeSequenceResponse.Error"/> will
  55. /// be <see cref="string.Empty"/>.
  56. /// </param>
  57. /// <returns>A <see cref="AnsiEscapeSequenceResponse"/> with the response, error, terminator, and value.</returns>
  58. public static bool TryRequest (AnsiEscapeSequenceRequest ansiRequest, out AnsiEscapeSequenceResponse result)
  59. {
  60. var error = new StringBuilder ();
  61. var values = new string? [] { null };
  62. try
  63. {
  64. ConsoleDriver? driver = Application.Driver;
  65. // Send the ANSI escape sequence
  66. ansiRequest.Response = driver?.WriteAnsiRequest (ansiRequest)!;
  67. if (!string.IsNullOrEmpty (ansiRequest.Response) && !ansiRequest.Response.StartsWith (AnsiEscapeSequenceRequestUtils.KeyEsc))
  68. {
  69. throw new InvalidOperationException ($"Invalid Response: {ansiRequest.Response}");
  70. }
  71. if (string.IsNullOrEmpty (ansiRequest.Terminator))
  72. {
  73. throw new InvalidOperationException ("Terminator request is empty.");
  74. }
  75. if (string.IsNullOrEmpty (ansiRequest.Response))
  76. {
  77. throw new InvalidOperationException ("Response request is null.");
  78. }
  79. if (!string.IsNullOrEmpty (ansiRequest.Response) && !ansiRequest.Response.EndsWith (ansiRequest.Terminator [^1]))
  80. {
  81. string resp = string.IsNullOrEmpty (ansiRequest.Response) ? "" : ansiRequest.Response.Last ().ToString ();
  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? _, string? _, values, string? _) = AnsiEscapeSequenceRequestUtils.GetEscapeResult (ansiRequest.Response.ToCharArray ());
  94. }
  95. }
  96. AnsiEscapeSequenceResponse ansiResponse = new ()
  97. {
  98. Response = ansiRequest.Response, Error = error.ToString (),
  99. Terminator = string.IsNullOrEmpty (ansiRequest.Response) ? "" : ansiRequest.Response [^1].ToString (), ExpectedResponseValue = values [0]
  100. };
  101. // Invoke the event if it's subscribed
  102. ansiRequest.ResponseReceived?.Invoke (ansiRequest, ansiResponse);
  103. result = ansiResponse;
  104. return string.IsNullOrWhiteSpace (result.Error) && !string.IsNullOrWhiteSpace (result.Response);
  105. }
  106. /// <summary>
  107. /// The value expected in the response after the CSI e.g.
  108. /// <see>
  109. /// <cref>EscSeqUtils.CSI_ReportTerminalSizeInChars.Value</cref>
  110. /// </see>
  111. /// should result in a response of the form <c>ESC [ 8 ; height ; width t</c>. In this case, <see cref="ExpectedResponseValue"/>
  112. /// will be <c>"8"</c>.
  113. /// </summary>
  114. public string? ExpectedResponseValue { get; init; }
  115. internal void RaiseResponseFromInput (AnsiEscapeSequenceRequest ansiRequest, string? response) { ResponseFromInput?.Invoke (ansiRequest, response); }
  116. // QUESTION: What is this for? Please provide a descriptive comment.
  117. internal event EventHandler<string?>? ResponseFromInput;
  118. }