|
| 1 | +#nullable enable |
| 2 | +namespace Terminal.Gui; |
| 3 | + |
| 4 | +/// <summary> |
| 5 | +/// Describes an ongoing ANSI request sent to the console. |
| 6 | +/// Use <see cref="ResponseReceived"/> to handle the response |
| 7 | +/// when console answers the request. |
| 8 | +/// </summary> |
| 9 | +public class AnsiEscapeSequenceRequest |
| 10 | +{ |
| 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 | + |
| 19 | + /// <summary> |
| 20 | + /// Invoked when the console responds with an ANSI response code that matches the |
| 21 | + /// <see cref="Terminator"/> |
| 22 | + /// </summary> |
| 23 | + public event EventHandler<AnsiEscapeSequenceResponse>? ResponseReceived; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// <para> |
| 27 | + /// The terminator that uniquely identifies the type of response as responded |
| 28 | + /// by the console. e.g. for |
| 29 | + /// <see> |
| 30 | + /// <cref>EscSeqUtils.CSI_SendDeviceAttributes.Request</cref> |
| 31 | + /// </see> |
| 32 | + /// the terminator is |
| 33 | + /// <see> |
| 34 | + /// <cref>EscSeqUtils.CSI_SendDeviceAttributes.Terminator</cref> |
| 35 | + /// </see> |
| 36 | + /// . |
| 37 | + /// </para> |
| 38 | + /// <para> |
| 39 | + /// After sending a request, the first response with matching terminator will be matched |
| 40 | + /// to the oldest outstanding request. |
| 41 | + /// </para> |
| 42 | + /// </summary> |
| 43 | + public required string Terminator { get; init; } |
| 44 | + |
| 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 response = new StringBuilder (); |
| 57 | + var error = new StringBuilder (); |
| 58 | + var savedIsReportingMouseMoves = false; |
| 59 | + NetDriver? netDriver = null; |
| 60 | + var values = new string? [] { null }; |
| 61 | + |
| 62 | + try |
| 63 | + { |
| 64 | + switch (Application.Driver) |
| 65 | + { |
| 66 | + case NetDriver: |
| 67 | + netDriver = Application.Driver as NetDriver; |
| 68 | + savedIsReportingMouseMoves = netDriver!.IsReportingMouseMoves; |
| 69 | + |
| 70 | + if (savedIsReportingMouseMoves) |
| 71 | + { |
| 72 | + netDriver.StopReportingMouseMoves (); |
| 73 | + } |
| 74 | + |
| 75 | + while (Console.KeyAvailable) |
| 76 | + { |
| 77 | + netDriver._mainLoopDriver._netEvents._waitForStart.Set (); |
| 78 | + netDriver._mainLoopDriver._netEvents._waitForStart.Reset (); |
| 79 | + |
| 80 | + netDriver._mainLoopDriver._netEvents._forceRead = true; |
| 81 | + } |
| 82 | + |
| 83 | + netDriver._mainLoopDriver._netEvents._forceRead = false; |
| 84 | + |
| 85 | + break; |
| 86 | + case CursesDriver cursesDriver: |
| 87 | + savedIsReportingMouseMoves = cursesDriver.IsReportingMouseMoves; |
| 88 | + |
| 89 | + if (savedIsReportingMouseMoves) |
| 90 | + { |
| 91 | + cursesDriver.StopReportingMouseMoves (); |
| 92 | + } |
| 93 | + |
| 94 | + break; |
| 95 | + } |
| 96 | + |
| 97 | + if (netDriver is { }) |
| 98 | + { |
| 99 | + NetEvents._suspendRead = true; |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + Thread.Sleep (100); // Allow time for mouse stopping and to flush the input buffer |
| 104 | + |
| 105 | + // Flush the input buffer to avoid reading stale input |
| 106 | + while (Console.KeyAvailable) |
| 107 | + { |
| 108 | + Console.ReadKey (true); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // Send the ANSI escape sequence |
| 113 | + Console.Write (ansiRequest.Request); |
| 114 | + Console.Out.Flush (); // Ensure the request is sent |
| 115 | + |
| 116 | + // Read the response from stdin (response should come back as input) |
| 117 | + Thread.Sleep (100); // Allow time for the terminal to respond |
| 118 | + |
| 119 | + // Read input until no more characters are available or the terminator is encountered |
| 120 | + while (Console.KeyAvailable) |
| 121 | + { |
| 122 | + // Peek the next key |
| 123 | + ConsoleKeyInfo keyInfo = Console.ReadKey (true); // true to not display on the console |
| 124 | + |
| 125 | + // Append the current key to the response |
| 126 | + response.Append (keyInfo.KeyChar); |
| 127 | + |
| 128 | + // Read until no key is available if no terminator was specified or |
| 129 | + // check if the key is terminator (ANSI escape sequence ends) |
| 130 | + if (!string.IsNullOrEmpty (ansiRequest.Terminator) && keyInfo.KeyChar == ansiRequest.Terminator [^1]) |
| 131 | + { |
| 132 | + // Break out of the loop when terminator is found |
| 133 | + break; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if (string.IsNullOrEmpty (ansiRequest.Terminator)) |
| 138 | + { |
| 139 | + error.AppendLine ("Terminator request is empty."); |
| 140 | + } |
| 141 | + else if (!response.ToString ().EndsWith (ansiRequest.Terminator [^1])) |
| 142 | + { |
| 143 | + throw new InvalidOperationException ($"Terminator doesn't ends with: '{ansiRequest.Terminator [^1]}'"); |
| 144 | + } |
| 145 | + } |
| 146 | + catch (Exception ex) |
| 147 | + { |
| 148 | + error.AppendLine ($"Error executing ANSI request: {ex.Message}"); |
| 149 | + } |
| 150 | + finally |
| 151 | + { |
| 152 | + if (string.IsNullOrEmpty (error.ToString ())) |
| 153 | + { |
| 154 | + (string? c1Control, string? code, values, string? terminator) = EscSeqUtils.GetEscapeResult (response.ToString ().ToCharArray ()); |
| 155 | + } |
| 156 | + |
| 157 | + if (savedIsReportingMouseMoves) |
| 158 | + { |
| 159 | + switch (Application.Driver) |
| 160 | + { |
| 161 | + case NetDriver: |
| 162 | + NetEvents._suspendRead = false; |
| 163 | + netDriver!.StartReportingMouseMoves (); |
| 164 | + |
| 165 | + break; |
| 166 | + case CursesDriver cursesDriver: |
| 167 | + cursesDriver.StartReportingMouseMoves (); |
| 168 | + |
| 169 | + break; |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + AnsiEscapeSequenceResponse ansiResponse = new () |
| 175 | + { |
| 176 | + Response = response.ToString (), Error = error.ToString (), |
| 177 | + Terminator = string.IsNullOrEmpty (response.ToString ()) ? "" : response.ToString () [^1].ToString (), Value = values [0] |
| 178 | + }; |
| 179 | + |
| 180 | + // Invoke the event if it's subscribed |
| 181 | + ansiRequest.ResponseReceived?.Invoke (ansiRequest, ansiResponse); |
| 182 | + |
| 183 | + result = ansiResponse; |
| 184 | + |
| 185 | + return string.IsNullOrWhiteSpace (result.Error) && !string.IsNullOrWhiteSpace (result.Response); |
| 186 | + } |
| 187 | + |
| 188 | + /// <summary> |
| 189 | + /// The value expected in the response e.g. |
| 190 | + /// <see> |
| 191 | + /// <cref>EscSeqUtils.CSI_ReportTerminalSizeInChars.Value</cref> |
| 192 | + /// </see> |
| 193 | + /// which will have a 't' as terminator but also other different request may return the same terminator with a |
| 194 | + /// different value. |
| 195 | + /// </summary> |
| 196 | + public string? Value { get; init; } |
| 197 | +} |
0 commit comments