EscSeqReq.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// Represents the status of an ANSI escape sequence request made to the terminal using
  4. /// <see cref="EscSeqRequests"/>.
  5. /// </summary>
  6. /// <remarks></remarks>
  7. public class EscSeqReqStatus
  8. {
  9. /// <summary>Creates a new state of escape sequence request.</summary>
  10. /// <param name="terminator">The terminator.</param>
  11. /// <param name="numReq">The number of requests.</param>
  12. public EscSeqReqStatus (string terminator, int numReq)
  13. {
  14. Terminator = terminator;
  15. NumRequests = NumOutstanding = numReq;
  16. }
  17. /// <summary>Gets the number of unfinished requests.</summary>
  18. public int NumOutstanding { get; set; }
  19. /// <summary>Gets the number of requests.</summary>
  20. public int NumRequests { get; }
  21. /// <summary>Gets the Escape Sequence Termintor (e.g. ESC[8t ... t is the terminator).</summary>
  22. public string Terminator { get; }
  23. }
  24. // TODO: This class is a singleton. It should use the singleton pattern.
  25. /// <summary>
  26. /// Manages ANSI Escape Sequence requests and responses. The list of <see cref="EscSeqReqStatus"/> contains the
  27. /// status of the request. Each request is identified by the terminator (e.g. ESC[8t ... t is the terminator).
  28. /// </summary>
  29. public class EscSeqRequests
  30. {
  31. /// <summary>Gets the <see cref="EscSeqReqStatus"/> list.</summary>
  32. public List<EscSeqReqStatus> Statuses { get; } = new ();
  33. /// <summary>
  34. /// Adds a new request for the ANSI Escape Sequence defined by <paramref name="terminator"/>. Adds a
  35. /// <see cref="EscSeqReqStatus"/> instance to <see cref="Statuses"/> list.
  36. /// </summary>
  37. /// <param name="terminator">The terminator.</param>
  38. /// <param name="numReq">The number of requests.</param>
  39. public void Add (string terminator, int numReq = 1)
  40. {
  41. lock (Statuses)
  42. {
  43. EscSeqReqStatus found = Statuses.Find (x => x.Terminator == terminator);
  44. if (found == null)
  45. {
  46. Statuses.Add (new EscSeqReqStatus (terminator, numReq));
  47. }
  48. else if (found != null && found.NumOutstanding < found.NumRequests)
  49. {
  50. found.NumOutstanding = Math.Min (found.NumOutstanding + numReq, found.NumRequests);
  51. }
  52. }
  53. }
  54. /// <summary>
  55. /// Indicates if a <see cref="EscSeqReqStatus"/> with the <paramref name="terminator"/> exists in the
  56. /// <see cref="Statuses"/> list.
  57. /// </summary>
  58. /// <param name="terminator"></param>
  59. /// <returns><see langword="true"/> if exist, <see langword="false"/> otherwise.</returns>
  60. public bool HasResponse (string terminator)
  61. {
  62. lock (Statuses)
  63. {
  64. EscSeqReqStatus found = Statuses.Find (x => x.Terminator == terminator);
  65. if (found == null)
  66. {
  67. return false;
  68. }
  69. if (found != null && found.NumOutstanding > 0)
  70. {
  71. return true;
  72. }
  73. // BUGBUG: Why does an API that returns a bool remove the entry from the list?
  74. // NetDriver and Unit tests never exercise this line of code. Maybe Curses does?
  75. Statuses.Remove (found);
  76. return false;
  77. }
  78. }
  79. /// <summary>
  80. /// Removes a request defined by <paramref name="terminator"/>. If a matching <see cref="EscSeqReqStatus"/> is
  81. /// found and the number of outstanding requests is greater than 0, the number of outstanding requests is decremented.
  82. /// If the number of outstanding requests is 0, the <see cref="EscSeqReqStatus"/> is removed from
  83. /// <see cref="Statuses"/>.
  84. /// </summary>
  85. /// <param name="terminator">The terminating string.</param>
  86. public void Remove (string terminator)
  87. {
  88. lock (Statuses)
  89. {
  90. EscSeqReqStatus found = Statuses.Find (x => x.Terminator == terminator);
  91. if (found == null)
  92. {
  93. return;
  94. }
  95. if (found != null && found.NumOutstanding == 0)
  96. {
  97. Statuses.Remove (found);
  98. }
  99. else if (found != null && found.NumOutstanding > 0)
  100. {
  101. found.NumOutstanding--;
  102. if (found.NumOutstanding == 0)
  103. {
  104. Statuses.Remove (found);
  105. }
  106. }
  107. }
  108. }
  109. }