EscSeqRequests.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. namespace Terminal.Gui.Drivers;
  2. /// <summary>
  3. /// Manages ANSI Escape Sequence requests and responses. The list of <see cref="EscSeqReqStatus"/>
  4. /// contains the
  5. /// status of the request. Each request is identified by the terminator (e.g. ESC[8t ... t is the terminator).
  6. /// </summary>
  7. public static class EscSeqRequests
  8. {
  9. /// <summary>Gets the <see cref="EscSeqReqStatus"/> list.</summary>
  10. public static List<EscSeqReqStatus> Statuses { get; } = [];
  11. /// <summary>
  12. /// Adds a new request for the ANSI Escape Sequence defined by <paramref name="terminator"/>. Adds a
  13. /// <see cref="EscSeqReqStatus"/> instance to <see cref="Statuses"/> list.
  14. /// </summary>
  15. /// <param name="terminator">The terminator.</param>
  16. /// <param name="numRequests">The number of requests.</param>
  17. public static void Add (string? terminator, int numRequests = 1)
  18. {
  19. ArgumentException.ThrowIfNullOrEmpty (terminator);
  20. int numReq = Math.Max (numRequests, 1);
  21. lock (Statuses)
  22. {
  23. EscSeqReqStatus? found = Statuses.Find (x => x.Terminator == terminator);
  24. if (found is null)
  25. {
  26. Statuses.Add (new (terminator, numReq));
  27. }
  28. else
  29. {
  30. found.NumRequests += numReq;
  31. found.NumOutstanding += numReq;
  32. }
  33. }
  34. }
  35. /// <summary>
  36. /// Clear the <see cref="Statuses"/> property.
  37. /// </summary>
  38. public static void Clear ()
  39. {
  40. lock (Statuses)
  41. {
  42. Statuses.Clear ();
  43. }
  44. }
  45. /// <summary>
  46. /// Indicates if a <see cref="EscSeqReqStatus"/> with the <paramref name="terminator"/> exists in the
  47. /// <see cref="Statuses"/> list.
  48. /// </summary>
  49. /// <param name="terminator"></param>
  50. /// <returns><see langword="true"/> if exist, <see langword="false"/> otherwise.</returns>
  51. public static bool HasResponse (string terminator)
  52. {
  53. lock (Statuses)
  54. {
  55. EscSeqReqStatus? found = Statuses.Find (x => x.Terminator == terminator);
  56. return found is { };
  57. }
  58. }
  59. /// <summary>
  60. /// Removes a request defined by <paramref name="terminator"/>. If a matching <see cref="EscSeqReqStatus"/> is
  61. /// found and the number of outstanding requests is greater than 0, the number of outstanding requests is decremented.
  62. /// If the number of outstanding requests is 0, the <see cref="EscSeqReqStatus"/> is removed from
  63. /// <see cref="Statuses"/>.
  64. /// </summary>
  65. /// <param name="terminator">The terminating string.</param>
  66. public static void Remove (string terminator)
  67. {
  68. ArgumentException.ThrowIfNullOrEmpty (terminator);
  69. lock (Statuses)
  70. {
  71. EscSeqReqStatus? found = Statuses.Find (x => x.Terminator == terminator);
  72. if (found is null)
  73. {
  74. return;
  75. }
  76. found.NumOutstanding--;
  77. if (found.NumOutstanding == 0)
  78. {
  79. Statuses.Remove (found);
  80. }
  81. }
  82. }
  83. }