AnsiRequestsScenario.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. #nullable enable
  2. using System.Text;
  3. namespace UICatalog.Scenarios;
  4. [ScenarioMetadata ("AnsiEscapeSequenceRequest", "Ansi Escape Sequence Request")]
  5. [ScenarioCategory ("Tests")]
  6. public sealed class AnsiEscapeSequenceRequests : Scenario
  7. {
  8. private GraphView? _graphView;
  9. private ScatterSeries? _sentSeries;
  10. private ScatterSeries? _answeredSeries;
  11. private readonly List<DateTime> _sends = new ();
  12. private readonly object _lockAnswers = new object ();
  13. private readonly Dictionary<DateTime, string> _answers = new ();
  14. private Label? _lblSummary;
  15. private object? _updateTimeoutToken;
  16. private object? _sendDarTimeoutToken;
  17. public override void Main ()
  18. {
  19. // Init
  20. Application.Init ();
  21. TabView tv = new TabView
  22. {
  23. Width = Dim.Fill (),
  24. Height = Dim.Fill (),
  25. CanFocus = true
  26. };
  27. Tab single = new ();
  28. single.DisplayText = "Single";
  29. single.View = BuildSingleTab ();
  30. Tab bulk = new ();
  31. bulk.DisplayText = "Multi";
  32. bulk.View = BuildBulkTab ();
  33. tv.AddTab (single, true);
  34. tv.AddTab (bulk, false);
  35. // Setup - Create a top-level application window and configure it.
  36. Window appWindow = new ()
  37. {
  38. Title = GetQuitKeyAndName (),
  39. };
  40. appWindow.Add (tv);
  41. // Run - Start the application.
  42. Application.Run (appWindow);
  43. bulk.View.Dispose ();
  44. single.View.Dispose ();
  45. appWindow.Dispose ();
  46. Application.RemoveTimeout (_updateTimeoutToken!);
  47. Application.RemoveTimeout (_sendDarTimeoutToken!);
  48. // Shutdown - Calling Application.Shutdown is required.
  49. Application.Shutdown ();
  50. }
  51. private View BuildSingleTab ()
  52. {
  53. View w = new View ()
  54. {
  55. Width = Dim.Fill (),
  56. Height = Dim.Fill (),
  57. CanFocus = true
  58. };
  59. w!.Padding!.Thickness = new (1);
  60. var scrRequests = new List<string>
  61. {
  62. "CSI_SendDeviceAttributes",
  63. "CSI_ReportTerminalSizeInChars",
  64. "CSI_RequestCursorPositionReport",
  65. "CSI_SendDeviceAttributes2"
  66. };
  67. // TODO: This UI would be cleaner/less rigid if Pos.Align were used
  68. var cbRequests = new ComboBox () { Width = 40, Height = 5, ReadOnly = true, Source = new ListWrapper<string> (new (scrRequests)) };
  69. w.Add (cbRequests);
  70. var label = new Label { Y = Pos.Bottom (cbRequests) + 1, Text = "Request:" };
  71. var tfRequest = new TextField { X = Pos.Left (label), Y = Pos.Bottom (label), Width = 20 };
  72. w.Add (label, tfRequest);
  73. label = new Label { X = Pos.Right (tfRequest) + 1, Y = Pos.Top (tfRequest) - 1, Text = "Value:" };
  74. var tfValue = new TextField { X = Pos.Left (label), Y = Pos.Bottom (label), Width = 6 };
  75. w.Add (label, tfValue);
  76. label = new Label { X = Pos.Right (tfValue) + 1, Y = Pos.Top (tfValue) - 1, Text = "Terminator:" };
  77. var tfTerminator = new TextField { X = Pos.Left (label), Y = Pos.Bottom (label), Width = 4 };
  78. w.Add (label, tfTerminator);
  79. cbRequests.SelectedItemChanged += (s, e) =>
  80. {
  81. if (cbRequests.SelectedItem == -1)
  82. {
  83. return;
  84. }
  85. var selAnsiEscapeSequenceRequestName = scrRequests [cbRequests.SelectedItem];
  86. AnsiEscapeSequence? selAnsiEscapeSequenceRequest = null;
  87. switch (selAnsiEscapeSequenceRequestName)
  88. {
  89. case "CSI_SendDeviceAttributes":
  90. selAnsiEscapeSequenceRequest = EscSeqUtils.CSI_SendDeviceAttributes;
  91. break;
  92. case "CSI_ReportTerminalSizeInChars":
  93. selAnsiEscapeSequenceRequest = EscSeqUtils.CSI_ReportWindowSizeInChars;
  94. break;
  95. case "CSI_RequestCursorPositionReport":
  96. selAnsiEscapeSequenceRequest = EscSeqUtils.CSI_RequestCursorPositionReport;
  97. break;
  98. case "CSI_SendDeviceAttributes2":
  99. selAnsiEscapeSequenceRequest = EscSeqUtils.CSI_SendDeviceAttributes2;
  100. break;
  101. }
  102. tfRequest.Text = selAnsiEscapeSequenceRequest is { } ? selAnsiEscapeSequenceRequest.Request : "";
  103. tfValue.Text = selAnsiEscapeSequenceRequest is { } ? selAnsiEscapeSequenceRequest.Value ?? "" : "";
  104. tfTerminator.Text = selAnsiEscapeSequenceRequest is { } ? selAnsiEscapeSequenceRequest.Terminator : "";
  105. };
  106. // Forces raise cbRequests.SelectedItemChanged to update TextFields
  107. cbRequests.SelectedItem = 0;
  108. label = new Label { Y = Pos.Bottom (tfRequest) + 2, Text = "Response:" };
  109. var tvResponse = new TextView { X = Pos.Left (label), Y = Pos.Bottom (label), Width = 40, Height = 4, ReadOnly = true };
  110. w.Add (label, tvResponse);
  111. label = new Label { X = Pos.Right (tvResponse) + 1, Y = Pos.Top (tvResponse) - 1, Text = "Error:" };
  112. var tvError = new TextView { X = Pos.Left (label), Y = Pos.Bottom (label), Width = 40, Height = 4, ReadOnly = true };
  113. w.Add (label, tvError);
  114. label = new Label { X = Pos.Right (tvError) + 1, Y = Pos.Top (tvError) - 1, Text = "Value:" };
  115. var tvValue = new TextView { X = Pos.Left (label), Y = Pos.Bottom (label), Width = 6, Height = 4, ReadOnly = true };
  116. w.Add (label, tvValue);
  117. label = new Label { X = Pos.Right (tvValue) + 1, Y = Pos.Top (tvValue) - 1, Text = "Terminator:" };
  118. var tvTerminator = new TextView { X = Pos.Left (label), Y = Pos.Bottom (label), Width = 4, Height = 4, ReadOnly = true };
  119. w.Add (label, tvTerminator);
  120. var btnResponse = new Button { X = Pos.Center (), Y = Pos.Bottom (tvResponse) + 2, Text = "Send Request", IsDefault = true };
  121. var lblSuccess = new Label { X = Pos.Center (), Y = Pos.Bottom (btnResponse) + 1 };
  122. w.Add (lblSuccess);
  123. btnResponse.Accepting += (s, e) =>
  124. {
  125. var ansiEscapeSequenceRequest = new AnsiEscapeSequence
  126. {
  127. Request = tfRequest.Text,
  128. Terminator = tfTerminator.Text,
  129. Value = string.IsNullOrEmpty (tfValue.Text) ? null : tfValue.Text
  130. };
  131. Application.Driver?.QueueAnsiRequest (
  132. new ()
  133. {
  134. Request = ansiEscapeSequenceRequest.Request,
  135. Terminator = ansiEscapeSequenceRequest.Terminator,
  136. ResponseReceived = (s) => OnSuccess (s!, tvResponse, tvError, tvValue, tvTerminator, lblSuccess),
  137. Abandoned = () => OnFail (tvResponse, tvError, tvValue, tvTerminator, lblSuccess)
  138. });
  139. };
  140. w.Add (btnResponse);
  141. w.Add (new Label { Y = Pos.Bottom (lblSuccess) + 2, Text = "You can send other requests by editing the TextFields." });
  142. return w;
  143. }
  144. private void OnSuccess (string response, TextView tvResponse, TextView tvError, TextView tvValue, TextView tvTerminator, Label lblSuccess)
  145. {
  146. tvResponse.Text = response;
  147. tvError.Text = string.Empty;
  148. tvValue.Text = string.Empty;
  149. tvTerminator.Text = string.Empty;
  150. lblSuccess.SchemeName = SchemeManager.SchemesToSchemeName (Schemes.Base);
  151. lblSuccess.Text = "Successful";
  152. }
  153. private void OnFail (TextView tvResponse, TextView tvError, TextView tvValue, TextView tvTerminator, Label lblSuccess)
  154. {
  155. tvResponse.Text = string.Empty;
  156. tvError.Text = "No Response";
  157. tvValue.Text = string.Empty;
  158. tvTerminator.Text = string.Empty;
  159. lblSuccess.SchemeName = SchemeManager.SchemesToSchemeName (Schemes.Error);
  160. lblSuccess.Text = "Error";
  161. }
  162. private View BuildBulkTab ()
  163. {
  164. View w = new View ()
  165. {
  166. Width = Dim.Fill (),
  167. Height = Dim.Fill (),
  168. CanFocus = true
  169. };
  170. var lbl = new Label ()
  171. {
  172. Text = "This scenario tests Ansi request/response processing. Use the TextView to ensure regular user interaction continues as normal during sends",
  173. Height = 2,
  174. Width = Dim.Fill ()
  175. };
  176. _updateTimeoutToken = Application.AddTimeout (
  177. TimeSpan.FromMilliseconds (1000),
  178. () =>
  179. {
  180. lock (_lockAnswers)
  181. {
  182. UpdateGraph ();
  183. UpdateResponses ();
  184. }
  185. return true;
  186. });
  187. var tv = new TextView ()
  188. {
  189. Y = Pos.Bottom (lbl),
  190. Width = Dim.Percent (50),
  191. Height = Dim.Fill ()
  192. };
  193. var lblDar = new Label ()
  194. {
  195. Y = Pos.Bottom (lbl),
  196. X = Pos.Right (tv) + 1,
  197. Text = "DAR per second",
  198. };
  199. var cbDar = new NumericUpDown ()
  200. {
  201. X = Pos.Right (lblDar),
  202. Y = Pos.Bottom (lbl),
  203. Value = 0,
  204. };
  205. cbDar.ValueChanging += (s, e) =>
  206. {
  207. if (e.NewValue < 0 || e.NewValue > 20)
  208. {
  209. e.Cancel = true;
  210. }
  211. };
  212. w.Add (cbDar);
  213. int lastSendTime = Environment.TickCount;
  214. object lockObj = new object ();
  215. _sendDarTimeoutToken = Application.AddTimeout (
  216. TimeSpan.FromMilliseconds (50),
  217. () =>
  218. {
  219. lock (lockObj)
  220. {
  221. if (cbDar.Value > 0)
  222. {
  223. int interval = 1000 / cbDar.Value; // Calculate the desired interval in milliseconds
  224. int currentTime = Environment.TickCount; // Current system time in milliseconds
  225. // Check if the time elapsed since the last send is greater than the interval
  226. if (currentTime - lastSendTime >= interval)
  227. {
  228. SendDar (); // Send the request
  229. lastSendTime = currentTime; // Update the last send time
  230. }
  231. }
  232. }
  233. return true;
  234. });
  235. _graphView = new GraphView ()
  236. {
  237. Y = Pos.Bottom (cbDar),
  238. X = Pos.Right (tv),
  239. Width = Dim.Fill (),
  240. Height = Dim.Fill (1)
  241. };
  242. _lblSummary = new Label ()
  243. {
  244. Y = Pos.Bottom (_graphView),
  245. X = Pos.Right (tv),
  246. Width = Dim.Fill ()
  247. };
  248. SetupGraph ();
  249. w.Add (lbl);
  250. w.Add (lblDar);
  251. w.Add (cbDar);
  252. w.Add (tv);
  253. w.Add (_graphView);
  254. w.Add (_lblSummary);
  255. return w;
  256. }
  257. private void UpdateResponses ()
  258. {
  259. _lblSummary!.Text = GetSummary ();
  260. _lblSummary.SetNeedsDraw ();
  261. }
  262. private string GetSummary ()
  263. {
  264. if (_answers.Count == 0)
  265. {
  266. return "No requests sent yet";
  267. }
  268. var last = _answers.Last ().Value;
  269. var unique = _answers.Values.Distinct ().Count ();
  270. var total = _answers.Count;
  271. return $"Last:{last} U:{unique} T:{total}";
  272. }
  273. private void SetupGraph ()
  274. {
  275. _graphView!.Series.Add (_sentSeries = new ScatterSeries ());
  276. _graphView!.Series.Add (_answeredSeries = new ScatterSeries ());
  277. _sentSeries.Fill = new GraphCellToRender (new Rune ('.'), new Attribute (ColorName16.BrightGreen, ColorName16.Black));
  278. _answeredSeries.Fill = new GraphCellToRender (new Rune ('.'), new Attribute (ColorName16.BrightRed, ColorName16.Black));
  279. // Todo:
  280. // _graphView.Annotations.Add (_sentSeries new PathAnnotation {});
  281. _graphView.CellSize = new PointF (1, 1);
  282. _graphView.MarginBottom = 2;
  283. _graphView.AxisX.Increment = 1;
  284. _graphView.AxisX.Text = "Seconds";
  285. _graphView.GraphColor = new Attribute (Color.Green, Color.Black);
  286. }
  287. private void UpdateGraph ()
  288. {
  289. _sentSeries!.Points = _sends
  290. .GroupBy (ToSeconds)
  291. .Select (g => new PointF (g.Key, g.Count ()))
  292. .ToList ();
  293. _answeredSeries!.Points = _answers.Keys
  294. .GroupBy (ToSeconds)
  295. .Select (g => new PointF (g.Key, g.Count ()))
  296. .ToList ();
  297. // _graphView.ScrollOffset = new PointF(,0);
  298. _graphView!.SetNeedsDraw ();
  299. }
  300. private int ToSeconds (DateTime t)
  301. {
  302. return (int)(DateTime.Now - t).TotalSeconds;
  303. }
  304. private void SendDar ()
  305. {
  306. Application.Driver?.QueueAnsiRequest (
  307. new ()
  308. {
  309. Request = EscSeqUtils.CSI_SendDeviceAttributes.Request,
  310. Terminator = EscSeqUtils.CSI_SendDeviceAttributes.Terminator,
  311. ResponseReceived = HandleResponse!
  312. });
  313. _sends.Add (DateTime.Now);
  314. }
  315. private void HandleResponse (string response)
  316. {
  317. lock (_lockAnswers)
  318. {
  319. _answers.Add (DateTime.Now, response);
  320. }
  321. }
  322. }