AnsiRequestsScenario.cs 15 KB

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