using System; using System.Collections.Generic; namespace Terminal.Gui { /// /// Represents the state of an ANSI escape sequence request. /// /// /// This is needed because there are some escape sequence requests responses that are equal /// with some normal escape sequences and thus, will be only considered the responses to the /// requests that were registered with this object. /// public class EscSeqReqStatus { /// /// Gets the terminating. /// public string Terminating { get; } /// /// Gets the number of requests. /// public int NumRequests { get; } /// /// Gets information about unfinished requests. /// public int NumOutstanding { get; set; } /// /// Creates a new state of escape sequence request. /// /// The terminating. /// The number of requests. public EscSeqReqStatus (string terminating, int numOfReq) { Terminating = terminating; NumRequests = NumOutstanding = numOfReq; } } /// /// Manages a list of . /// public class EscSeqReqProc { /// /// Gets the list. /// public List EscSeqReqStats { get; } = new List (); /// /// Adds a new instance to the list. /// /// The terminating. /// The number of requests. public void Add (string terminating, int numOfReq = 1) { lock (EscSeqReqStats) { var found = EscSeqReqStats.Find (x => x.Terminating == terminating); if (found == null) { EscSeqReqStats.Add (new EscSeqReqStatus (terminating, numOfReq)); } else if (found != null && found.NumOutstanding < found.NumRequests) { found.NumOutstanding = Math.Min (found.NumOutstanding + numOfReq, found.NumRequests); } } } /// /// Removes a instance from the list. /// /// The terminating string. public void Remove (string terminating) { lock (EscSeqReqStats) { var found = EscSeqReqStats.Find (x => x.Terminating == terminating); if (found == null) { return; } if (found != null && found.NumOutstanding == 0) { EscSeqReqStats.Remove (found); } else if (found != null && found.NumOutstanding > 0) { found.NumOutstanding--; if (found.NumOutstanding == 0) { EscSeqReqStats.Remove (found); } } } } /// /// Indicates if a with the exist /// in the list. /// /// /// if exist, otherwise. public bool Requested (string terminating) { lock (EscSeqReqStats) { var found = EscSeqReqStats.Find (x => x.Terminating == terminating); if (found == null) { return false; } if (found != null && found.NumOutstanding > 0) { return true; } else { EscSeqReqStats.Remove (found); } return false; } } } }