#nullable enable
namespace Terminal.Gui;
internal abstract class AnsiResponseParserBase : IAnsiResponseParser
{
///
/// Responses we are expecting to come in.
///
protected readonly List expectedResponses = new ();
///
/// Collection of responses that we .
///
protected readonly List lateResponses = new ();
///
/// Responses that you want to look out for that will come in continuously e.g. mouse events.
/// Key is the terminator.
///
protected readonly List persistentExpectations = new ();
private AnsiResponseParserState _state = AnsiResponseParserState.Normal;
// Current state of the parser
public AnsiResponseParserState State
{
get => _state;
protected set
{
StateChangedAt = DateTime.Now;
_state = value;
}
}
protected readonly IHeld heldContent;
///
/// When was last changed.
///
public DateTime StateChangedAt { get; private set; } = DateTime.Now;
// These all are valid terminators on ansi responses,
// see CSI in https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-ordered-by-the-final-character_s
// No - N or O
protected readonly HashSet _knownTerminators = new (new []
{
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
// No - N or O
'P', 'Q', 'R', 'S', 'T', 'W', 'X', 'Z',
'^', '`', '~',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'l', 'm', 'n',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
});
protected AnsiResponseParserBase (IHeld heldContent)
{
this.heldContent = heldContent;
}
protected void ResetState ()
{
State = AnsiResponseParserState.Normal;
heldContent.ClearHeld ();
}
///
/// Processes an input collection of objects long.
/// You must provide the indexers to return the objects and the action to append
/// to output stream.
///
/// The character representation of element i of your input collection
/// The actual element in the collection (e.g. char or Tuple<char,T>)
///
/// Action to invoke when parser confirms an element of the current collection or a previous
/// call's collection should be appended to the current output (i.e. append to your output List/StringBuilder).
///
/// The total number of elements in your collection
protected void ProcessInputBase (
Func getCharAtIndex,
Func getObjectAtIndex,
Action