EscSeqRequests.cs 3.1 KB

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