AnsiEscapeSequenceRequest.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. // Send the ANSI escape sequence
  69. ansiRequest.Response = driver.WriteAnsiRequest (ansiRequest);
  70. if (!string.IsNullOrEmpty (ansiRequest.Response) && !ansiRequest.Response.StartsWith (EscSeqUtils.KeyEsc))
  71. {
  72. throw new InvalidOperationException ("Invalid escape character!");
  73. }
  74. if (string.IsNullOrEmpty (ansiRequest.Terminator))
  75. {
  76. throw new InvalidOperationException ("Terminator request is empty.");
  77. }
  78. if (!ansiRequest.Response.EndsWith (ansiRequest.Terminator [^1]))
  79. {
  80. throw new InvalidOperationException ($"Terminator doesn't ends with: '{ansiRequest.Terminator [^1]}'");
  81. }
  82. }
  83. catch (Exception ex)
  84. {
  85. error.AppendLine ($"Error executing ANSI request: {ex.Message}");
  86. }
  87. finally
  88. {
  89. if (string.IsNullOrEmpty (error.ToString ()))
  90. {
  91. (string? c1Control, string? code, values, string? terminator) = EscSeqUtils.GetEscapeResult (ansiRequest.Response.ToCharArray ());
  92. }
  93. if (savedIsReportingMouseMoves)
  94. {
  95. driver.StartReportingMouseMoves ();
  96. }
  97. }
  98. AnsiEscapeSequenceResponse ansiResponse = new ()
  99. {
  100. Response = ansiRequest.Response, Error = error.ToString (),
  101. Terminator = string.IsNullOrEmpty (ansiRequest.Response) ? "" : ansiRequest.Response [^1].ToString (), Value = values [0]
  102. };
  103. // Invoke the event if it's subscribed
  104. ansiRequest.ResponseReceived?.Invoke (ansiRequest, ansiResponse);
  105. result = ansiResponse;
  106. return string.IsNullOrWhiteSpace (result.Error) && !string.IsNullOrWhiteSpace (result.Response);
  107. }
  108. /// <summary>
  109. /// The value expected in the response e.g.
  110. /// <see>
  111. /// <cref>EscSeqUtils.CSI_ReportTerminalSizeInChars.Value</cref>
  112. /// </see>
  113. /// which will have a 't' as terminator but also other different request may return the same terminator with a
  114. /// different value.
  115. /// </summary>
  116. public string? Value { get; init; }
  117. }