#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 AnsiEscapeSequenceRequests
{
///
/// Adds a new request for the ANSI Escape Sequence defined by . Adds a
/// instance to list.
///
/// The object.
public static void Add (AnsiEscapeSequenceRequest ansiRequest)
{
lock (ansiRequest._responseLock)
{
Statuses.Enqueue (new (ansiRequest));
}
System.Diagnostics.Debug.Assert (Statuses.Count > 0);
}
///
/// 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, out AnsiEscapeSequenceRequestStatus? seqReqStatus)
{
lock (Statuses)
{
Statuses.TryPeek (out seqReqStatus);
return seqReqStatus?.AnsiRequest.Terminator == terminator;
}
}
///
/// 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 static void Remove (AnsiEscapeSequenceRequestStatus? seqReqStatus)
{
lock (Statuses)
{
Statuses.TryDequeue (out AnsiEscapeSequenceRequestStatus? request);
if (request != seqReqStatus)
{
throw new InvalidOperationException ("Both EscSeqReqStatus objects aren't equals.");
}
}
}
/// Gets the list.
public static ConcurrentQueue Statuses { get; } = new ();
}