AnsiEscapeSequenceRequest.cs 4.9 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 console answers the request.
  7. /// </summary>
  8. public class AnsiEscapeSequenceRequest
  9. {
  10. /// <summary>
  11. /// Request to send e.g. see
  12. /// <see>
  13. /// <cref>EscSeqUtils.CSI_SendDeviceAttributes.Request</cref>
  14. /// </see>
  15. /// </summary>
  16. public required string Request { get; init; }
  17. /// <summary>
  18. /// Response received from the request.
  19. /// </summary>
  20. public string Response { get; internal set; } = string.Empty;
  21. /// <summary>
  22. /// Invoked when the console responds with an ANSI response code that matches the
  23. /// <see cref="Terminator"/>
  24. /// </summary>
  25. public event EventHandler<AnsiEscapeSequenceResponse>? ResponseReceived;
  26. /// <summary>
  27. /// <para>
  28. /// The terminator that uniquely identifies the type of response as responded
  29. /// by the console. e.g. for
  30. /// <see>
  31. /// <cref>EscSeqUtils.CSI_SendDeviceAttributes.Request</cref>
  32. /// </see>
  33. /// the terminator is
  34. /// <see>
  35. /// <cref>EscSeqUtils.CSI_SendDeviceAttributes.Terminator</cref>
  36. /// </see>
  37. /// .
  38. /// </para>
  39. /// <para>
  40. /// After sending a request, the first response with matching terminator will be matched
  41. /// to the oldest outstanding request.
  42. /// </para>
  43. /// </summary>
  44. public required string Terminator { get; init; }
  45. /// <summary>
  46. /// Execute an ANSI escape sequence escape which may return a response or error.
  47. /// </summary>
  48. /// <param name="ansiRequest">The ANSI escape sequence to request.</param>
  49. /// <param name="result">
  50. /// When this method returns <see langword="true"/>, an object containing the response with an empty
  51. /// error.
  52. /// </param>
  53. /// <returns>A <see cref="AnsiEscapeSequenceResponse"/> with the response, error, terminator and value.</returns>
  54. public static bool TryExecuteAnsiRequest (AnsiEscapeSequenceRequest ansiRequest, out AnsiEscapeSequenceResponse result)
  55. {
  56. var error = new StringBuilder ();
  57. var savedIsReportingMouseMoves = false;
  58. ConsoleDriver? driver = null;
  59. var values = new string? [] { null };
  60. try
  61. {
  62. driver = Application.Driver;
  63. savedIsReportingMouseMoves = driver!.IsReportingMouseMoves;
  64. if (savedIsReportingMouseMoves)
  65. {
  66. driver.StopReportingMouseMoves ();
  67. }
  68. driver!.IsSuspendRead = true;
  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. throw new InvalidOperationException ($"Terminator doesn't ends with: '{ansiRequest.Terminator [^1]}'");
  82. }
  83. }
  84. catch (Exception ex)
  85. {
  86. error.AppendLine ($"Error executing ANSI request: {ex.Message}");
  87. }
  88. finally
  89. {
  90. if (string.IsNullOrEmpty (error.ToString ()))
  91. {
  92. (string? c1Control, string? code, values, string? terminator) = EscSeqUtils.GetEscapeResult (ansiRequest.Response.ToCharArray ());
  93. }
  94. if (savedIsReportingMouseMoves)
  95. {
  96. driver!.IsSuspendRead = false;
  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. }