Add AnsiEscapeSequenceRequests scenario.

This commit is contained in:
BDisp
2024-10-01 17:34:41 +01:00
parent 21c31557c0
commit 7142a0489f

View File

@@ -0,0 +1,118 @@
using System.Collections.Generic;
using Terminal.Gui;
namespace UICatalog.Scenarios;
[ScenarioMetadata ("AnsiEscapeSequenceRequest", "Ansi Escape Sequence Request")]
[ScenarioCategory ("Controls")]
public sealed class AnsiEscapeSequenceRequests : Scenario
{
public override void Main ()
{
// Init
Application.Init ();
// Setup - Create a top-level application window and configure it.
Window appWindow = new ()
{
Title = GetQuitKeyAndName (),
};
appWindow.Padding.Thickness = new (1);
var scrRequests = new List<string>
{
"CSI_SendDeviceAttributes",
"CSI_ReportTerminalSizeInChars",
"CSI_RequestCursorPositionReport",
"CSI_SendDeviceAttributes2"
};
var cbRequests = new ComboBox () { Width = 40, Height = 5, ReadOnly = true, Source = new ListWrapper<string> (new (scrRequests)) };
appWindow.Add (cbRequests);
var label = new Label { Y = Pos.Bottom (cbRequests) + 1, Text = "Request:"};
var tfRequest = new TextField { X = Pos.Right (label) + 1, Y = Pos.Top (label), Width = 20 };
appWindow.Add (label, tfRequest);
label = new Label { X = Pos.Right (tfRequest) + 1, Y = Pos.Top (tfRequest), Text = "Value:" };
var tfValue = new TextField { X = Pos.Right (label) + 1, Y = Pos.Top (label), Width = 6 };
appWindow.Add (label, tfValue);
label = new Label { X = Pos.Right (tfValue) + 1, Y = Pos.Top (tfValue), Text = "Terminator:" };
var tfTerminator = new TextField { X = Pos.Right (label) + 1, Y = Pos.Top (label), Width = 4 };
appWindow.Add (label, tfTerminator);
cbRequests.SelectedItemChanged += (s, e) =>
{
var selAnsiEscapeSequenceRequestName = scrRequests [cbRequests.SelectedItem];
AnsiEscapeSequenceRequest selAnsiEscapeSequenceRequest = null;
switch (selAnsiEscapeSequenceRequestName)
{
case "CSI_SendDeviceAttributes":
selAnsiEscapeSequenceRequest = EscSeqUtils.CSI_SendDeviceAttributes;
break;
case "CSI_ReportTerminalSizeInChars":
selAnsiEscapeSequenceRequest = EscSeqUtils.CSI_ReportTerminalSizeInChars;
break;
case "CSI_RequestCursorPositionReport":
selAnsiEscapeSequenceRequest = EscSeqUtils.CSI_RequestCursorPositionReport;
break;
case "CSI_SendDeviceAttributes2":
selAnsiEscapeSequenceRequest = EscSeqUtils.CSI_SendDeviceAttributes2;
break;
}
tfRequest.Text = selAnsiEscapeSequenceRequest is { } ? selAnsiEscapeSequenceRequest.Request : "";
tfValue.Text = selAnsiEscapeSequenceRequest is { } ? selAnsiEscapeSequenceRequest.Value ?? "" : "";
tfTerminator.Text = selAnsiEscapeSequenceRequest is { } ? selAnsiEscapeSequenceRequest.Terminator : "";
};
// Forces raise cbRequests.SelectedItemChanged to update TextFields
cbRequests.SelectedItem = 0;
label = new Label { Y = Pos.Bottom (tfRequest) + 2, Text = "Response:" };
var tvResponse = new TextView { X = Pos.Right (label) + 1, Y = Pos.Top (label), Width = 30, Height = 4, ReadOnly = true };
appWindow.Add (label, tvResponse);
label = new Label { X = Pos.Right (tvResponse) + 1, Y = Pos.Top (tvResponse), Text = "Error:" };
var tvError = new TextView { X = Pos.Right (label) + 1, Y = Pos.Top (label), Width = 20, Height = 4, ReadOnly = true };
appWindow.Add (label, tvError);
label = new Label { X = Pos.Right (tvError) + 1, Y = Pos.Top (tvError), Text = "Value:" };
var tvValue = new TextView { X = Pos.Right (label) + 1, Y = Pos.Top (label), Width = 6, Height = 4, ReadOnly = true };
appWindow.Add (label, tvValue);
label = new Label { X = Pos.Right (tvValue) + 1, Y = Pos.Top (tvValue), Text = "Terminator:" };
var tvTerminator = new TextView { X = Pos.Right (label) + 1, Y = Pos.Top (label), Width = 4, Height = 4, ReadOnly = true };
appWindow.Add (label, tvTerminator);
var btnResponse = new Button { X = Pos.Center (), Y = Pos.Bottom (tvResponse) + 2, Text = "Send Request" };
btnResponse.Accept += (s, e) =>
{
var ansiEscapeSequenceRequest = new AnsiEscapeSequenceRequest
{
Request = tfRequest.Text,
Terminator = tfTerminator.Text,
Value = string.IsNullOrEmpty (tfValue.Text) ? null : tfValue.Text
};
var ansiEscapeSequenceResponse = AnsiEscapeSequenceRequest.ExecuteAnsiRequest (
ansiEscapeSequenceRequest
);
tvResponse.Text =ansiEscapeSequenceResponse.Response;
tvError.Text = ansiEscapeSequenceResponse.Error;
tvValue.Text = ansiEscapeSequenceResponse.Value ?? "";
tvTerminator.Text = ansiEscapeSequenceResponse.Terminator;
};
appWindow.Add (btnResponse);
appWindow.Add (new Label { Y = Pos.Bottom (btnResponse) + 2, Text = "You can send other requests by editing the TextFields." });
// Run - Start the application.
Application.Run (appWindow);
appWindow.Dispose ();
// Shutdown - Calling Application.Shutdown is required.
Application.Shutdown ();
}
}