diff --git a/Terminal.Gui/ConsoleDrivers/EscSeqUtils/EscSeqUtils.cs b/Terminal.Gui/ConsoleDrivers/EscSeqUtils/EscSeqUtils.cs index 1eb63e34aa..17589d101c 100644 --- a/Terminal.Gui/ConsoleDrivers/EscSeqUtils/EscSeqUtils.cs +++ b/Terminal.Gui/ConsoleDrivers/EscSeqUtils/EscSeqUtils.cs @@ -304,6 +304,91 @@ Action continuousButtonPressedHandler } #nullable enable + /// + /// Execute an ANSI escape sequence escape which may return a response or error. + /// + /// The ANSI escape sequence to request. + /// A tuple with the response and error. + public static (string response, string error) ExecuteAnsiRequest (string ansiRequest) + { + var response = new StringBuilder (); + var error = new StringBuilder (); + var foundEscapeSequence = false; + + try + { + switch (Application.Driver) + { + case NetDriver netDriver: + netDriver.StopReportingMouseMoves (); + + break; + case CursesDriver cursesDriver: + cursesDriver.StopReportingMouseMoves (); + + break; + } + + Thread.Sleep (100); // Allow time for mouse stopping + + // Flush the input buffer to avoid reading stale input + while (Console.KeyAvailable) + { + Console.ReadKey (true); + } + + // Send the ANSI escape sequence + Console.Write (ansiRequest); + Console.Out.Flush (); // Ensure the request is sent + + // Read the response from stdin (response should come back as input) + Thread.Sleep (100); // Allow time for the terminal to respond + + // Read input until no more characters are available or another \u001B is encountered + while (Console.KeyAvailable) + { + // Peek the next key + ConsoleKeyInfo keyInfo = Console.ReadKey (true); // true to not display on the console + + if (keyInfo.KeyChar == '\u001B') // Check if the key is Escape (ANSI escape sequence starts) + { + if (foundEscapeSequence) + { + // If we already found one \u001B, break out of the loop when another is found + break; + } + else + { + foundEscapeSequence = true; // Mark that we've encountered the first escape sequence + } + } + + // Append the current key to the response + response.Append (keyInfo.KeyChar); + } + } + catch (Exception ex) + { + error.AppendLine ($"Error executing ANSI request: {ex.Message}"); + } + finally + { + switch (Application.Driver) + { + case NetDriver netDriver: + netDriver.StartReportingMouseMoves (); + + break; + case CursesDriver cursesDriver: + cursesDriver.StartReportingMouseMoves (); + + break; + } + } + + return (response.ToString (), error.ToString ()); + } + /// /// Gets the c1Control used in the called escape sequence. ///