#nullable enable using System.Collections.Concurrent; namespace Terminal.Gui; /// /// 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 static class EscSeqRequests { /// Gets the list. public static List Statuses { get; } = new (); /// /// Adds a new request for the ANSI Escape Sequence defined by . Adds a /// instance to list. /// /// The terminator. /// The number of requests. public static void Add (string terminator, int numReq = 1) { lock (Statuses) { EscSeqReqStatus? found = Statuses.Find (x => x.Terminator == terminator); if (found is null) { Statuses.Add (new EscSeqReqStatus (terminator, numReq)); } else if (found.NumOutstanding < found.NumRequests) { found.NumOutstanding = Math.Min (found.NumOutstanding + numReq, found.NumRequests); } } } /// /// Clear the property. /// public static void Clear () { lock (Statuses) { Statuses.Clear (); } } /// /// Indicates if a with the exists in the /// list. /// /// /// if exist, otherwise. public static bool HasResponse (string terminator) { lock (Statuses) { EscSeqReqStatus? found = Statuses.Find (x => x.Terminator == terminator); if (found is null) { return false; } if (found is { NumOutstanding: > 0 }) { return true; } // BUGBUG: Why does an API that returns a bool remove the entry from the list? // NetDriver and Unit tests never exercise this line of code. Maybe Curses does? Statuses.Remove (found); 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 terminating string. public static void Remove (string terminator) { lock (Statuses) { EscSeqReqStatus? found = Statuses.Find (x => x.Terminator == terminator); if (found is null) { return; } if (found.NumOutstanding == 0) { Statuses.Remove (found); } else if (found.NumOutstanding > 0) { found.NumOutstanding--; if (found.NumOutstanding == 0) { Statuses.Remove (found); } } } } }