EscSeqReq.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Terminal.Gui {
  4. /// <summary>
  5. /// Represents the state of an ANSI escape sequence request.
  6. /// </summary>
  7. /// <remarks>
  8. /// This is needed because there are some escape sequence requests responses that are equal
  9. /// with some normal escape sequences and thus, will be only considered the responses to the
  10. /// requests that were registered with this object.
  11. /// </remarks>
  12. public class EscSeqReqStatus {
  13. /// <summary>
  14. /// Gets the terminating.
  15. /// </summary>
  16. public string Terminating { get; }
  17. /// <summary>
  18. /// Gets the number of requests.
  19. /// </summary>
  20. public int NumRequests { get; }
  21. /// <summary>
  22. /// Gets information about unfinished requests.
  23. /// </summary>
  24. public int NumOutstanding { get; set; }
  25. /// <summary>
  26. /// Creates a new state of escape sequence request.
  27. /// </summary>
  28. /// <param name="terminating">The terminating.</param>
  29. /// <param name="numOfReq">The number of requests.</param>
  30. public EscSeqReqStatus (string terminating, int numOfReq)
  31. {
  32. Terminating = terminating;
  33. NumRequests = NumOutstanding = numOfReq;
  34. }
  35. }
  36. /// <summary>
  37. /// Manages a list of <see cref="EscSeqReqStatus"/>.
  38. /// </summary>
  39. public class EscSeqReqProc {
  40. /// <summary>
  41. /// Gets the <see cref="EscSeqReqStatus"/> list.
  42. /// </summary>
  43. public List<EscSeqReqStatus> EscSeqReqStats { get; } = new List<EscSeqReqStatus> ();
  44. /// <summary>
  45. /// Adds a new <see cref="EscSeqReqStatus"/> instance to the <see cref="EscSeqReqStats"/> list.
  46. /// </summary>
  47. /// <param name="terminating">The terminating.</param>
  48. /// <param name="numOfReq">The number of requests.</param>
  49. public void Add (string terminating, int numOfReq = 1)
  50. {
  51. lock (EscSeqReqStats) {
  52. var found = EscSeqReqStats.Find (x => x.Terminating == terminating);
  53. if (found == null) {
  54. EscSeqReqStats.Add (new EscSeqReqStatus (terminating, numOfReq));
  55. } else if (found != null && found.NumOutstanding < found.NumRequests) {
  56. found.NumOutstanding = Math.Min (found.NumOutstanding + numOfReq, found.NumRequests);
  57. }
  58. }
  59. }
  60. /// <summary>
  61. /// Removes a <see cref="EscSeqReqStatus"/> instance from the <see cref="EscSeqReqStats"/> list.
  62. /// </summary>
  63. /// <param name="terminating">The terminating string.</param>
  64. public void Remove (string terminating)
  65. {
  66. lock (EscSeqReqStats) {
  67. var found = EscSeqReqStats.Find (x => x.Terminating == terminating);
  68. if (found == null) {
  69. return;
  70. }
  71. if (found != null && found.NumOutstanding == 0) {
  72. EscSeqReqStats.Remove (found);
  73. } else if (found != null && found.NumOutstanding > 0) {
  74. found.NumOutstanding--;
  75. if (found.NumOutstanding == 0) {
  76. EscSeqReqStats.Remove (found);
  77. }
  78. }
  79. }
  80. }
  81. /// <summary>
  82. /// Indicates if a <see cref="EscSeqReqStatus"/> with the <paramref name="terminating"/> exist
  83. /// in the <see cref="EscSeqReqStats"/> list.
  84. /// </summary>
  85. /// <param name="terminating"></param>
  86. /// <returns><see langword="true"/> if exist, <see langword="false"/> otherwise.</returns>
  87. public bool Requested (string terminating)
  88. {
  89. lock (EscSeqReqStats) {
  90. var found = EscSeqReqStats.Find (x => x.Terminating == terminating);
  91. if (found == null) {
  92. return false;
  93. }
  94. if (found != null && found.NumOutstanding > 0) {
  95. return true;
  96. } else {
  97. EscSeqReqStats.Remove (found);
  98. }
  99. return false;
  100. }
  101. }
  102. }
  103. }