AnsiEscapeSequenceRequests.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #nullable enable
  2. using System.Collections.Concurrent;
  3. namespace Terminal.Gui;
  4. // QUESTION: Can this class be moved/refactored/combined with the new AnsiEscapeSequenceRequest/Response class?
  5. // TODO: This class is a singleton. It should use the singleton pattern.
  6. /// <summary>
  7. /// Manages ANSI Escape Sequence requests and responses. The list of <see cref="AnsiEscapeSequenceRequestStatus"/> contains the
  8. /// status of the request. Each request is identified by the terminator (e.g. ESC[8t ... t is the terminator).
  9. /// </summary>
  10. public class AnsiEscapeSequenceRequests
  11. {
  12. /// <summary>
  13. /// Adds a new request for the ANSI Escape Sequence defined by <paramref name="ansiRequest"/>. Adds a
  14. /// <see cref="AnsiEscapeSequenceRequestStatus"/> instance to <see cref="Statuses"/> list.
  15. /// </summary>
  16. /// <param name="ansiRequest">The <see cref="AnsiEscapeSequenceRequest"/> object.</param>
  17. /// <param name="driver">The driver in use.</param>
  18. public void Add (AnsiEscapeSequenceRequest ansiRequest, ConsoleDriver? driver = null)
  19. {
  20. lock (Statuses)
  21. {
  22. Statuses.Enqueue (new (ansiRequest));
  23. if (driver is null)
  24. {
  25. Console.Out.Write (ansiRequest.Request);
  26. Console.Out.Flush ();
  27. }
  28. else
  29. {
  30. driver.WriteRaw (ansiRequest.Request);
  31. }
  32. }
  33. }
  34. /// <summary>
  35. /// Indicates if a <see cref="AnsiEscapeSequenceRequestStatus"/> with the <paramref name="terminator"/> exists in the
  36. /// <see cref="Statuses"/> list.
  37. /// </summary>
  38. /// <param name="terminator"></param>
  39. /// <param name="seqReqStatus"></param>
  40. /// <returns><see langword="true"/> if exist, <see langword="false"/> otherwise.</returns>
  41. public bool HasResponse (string terminator, out AnsiEscapeSequenceRequestStatus? seqReqStatus)
  42. {
  43. lock (Statuses)
  44. {
  45. Statuses.TryPeek (out seqReqStatus);
  46. var result = seqReqStatus?.AnsiRequest.Terminator == terminator;
  47. if (result)
  48. {
  49. return true;
  50. }
  51. seqReqStatus = null;
  52. return false;
  53. }
  54. }
  55. /// <summary>
  56. /// Removes a request defined by <paramref name="seqReqStatus"/>. If a matching <see cref="AnsiEscapeSequenceRequestStatus"/> is
  57. /// found and the number of outstanding requests is greater than 0, the number of outstanding requests is decremented.
  58. /// If the number of outstanding requests is 0, the <see cref="AnsiEscapeSequenceRequestStatus"/> is removed from
  59. /// <see cref="Statuses"/>.
  60. /// </summary>
  61. /// <param name="seqReqStatus">The <see cref="AnsiEscapeSequenceRequestStatus"/> object.</param>
  62. public void Remove (AnsiEscapeSequenceRequestStatus? seqReqStatus)
  63. {
  64. lock (Statuses)
  65. {
  66. Statuses.TryDequeue (out var request);
  67. if (request != seqReqStatus)
  68. {
  69. throw new InvalidOperationException ("Both EscSeqReqStatus objects aren't equals.");
  70. }
  71. }
  72. }
  73. /// <summary>Gets the <see cref="AnsiEscapeSequenceRequestStatus"/> list.</summary>
  74. public ConcurrentQueue<AnsiEscapeSequenceRequestStatus> Statuses { get; } = new ();
  75. }