#nullable enable using System.Collections.Concurrent; namespace Terminal.Gui; /// /// Represents the status of an ANSI escape sequence request made to the terminal using /// . /// /// public class EscSeqReqStatus { /// Creates a new state of escape sequence request. /// The object. public EscSeqReqStatus (AnsiEscapeSequenceRequest ansiRequest) { AnsiRequest = ansiRequest; } /// Gets the Escape Sequence Terminator (e.g. ESC[8t ... t is the terminator). public AnsiEscapeSequenceRequest AnsiRequest { get; } } // TODO: This class is a singleton. It should use the singleton pattern. /// /// Manages ANSI Escape Sequence requests and responses. The list of contains the /// status of the request. Each request is identified by the terminator (e.g. ESC[8t ... t is the terminator). /// public class EscSeqRequests { /// /// Adds a new request for the ANSI Escape Sequence defined by . Adds a /// instance to list. /// /// The object. /// The driver in use. public void Add (AnsiEscapeSequenceRequest ansiRequest, ConsoleDriver? driver = null) { lock (Statuses) { Statuses.Enqueue (new (ansiRequest)); if (driver is null) { Console.Out.Write (ansiRequest.Request); Console.Out.Flush (); } else { driver.WriteRaw (ansiRequest.Request); } } } /// /// Indicates if a with the exists in the /// list. /// /// /// /// if exist, otherwise. public bool HasResponse (string terminator, out EscSeqReqStatus? seqReqStatus) { lock (Statuses) { Statuses.TryPeek (out seqReqStatus); var result = seqReqStatus?.AnsiRequest.Terminator == terminator; if (result) { return true; } seqReqStatus = null; return false; } } /// /// Removes a request defined by . If a matching is /// found and the number of outstanding requests is greater than 0, the number of outstanding requests is decremented. /// If the number of outstanding requests is 0, the is removed from /// . /// /// The object. public void Remove (EscSeqReqStatus? seqReqStatus) { lock (Statuses) { Statuses.TryDequeue (out var request); if (request != seqReqStatus) { throw new InvalidOperationException ("Both EscSeqReqStatus objects aren't equals."); } } } /// Gets the list. public ConcurrentQueue Statuses { get; } = new (); }