Fix exception throwing if no terminator is specified.

This commit is contained in:
BDisp
2024-10-07 12:52:29 +01:00
parent 922f586904
commit 2c76ed20ac
2 changed files with 15 additions and 10 deletions

View File

@@ -57,6 +57,7 @@ public class AnsiEscapeSequenceRequest
var error = new StringBuilder ();
var savedIsReportingMouseMoves = false;
NetDriver? netDriver = null;
var values = new string? [] { null };
try
{
@@ -124,14 +125,20 @@ public class AnsiEscapeSequenceRequest
// Append the current key to the response
response.Append (keyInfo.KeyChar);
if (keyInfo.KeyChar == ansiRequest.Terminator [^1]) // Check if the key is terminator (ANSI escape sequence ends)
// Read until no key is available if no terminator was specified or
// check if the key is terminator (ANSI escape sequence ends)
if (!string.IsNullOrEmpty (ansiRequest.Terminator) && keyInfo.KeyChar == ansiRequest.Terminator [^1])
{
// Break out of the loop when terminator is found
break;
}
}
if (!response.ToString ().EndsWith (ansiRequest.Terminator [^1]))
if (string.IsNullOrEmpty (ansiRequest.Terminator))
{
error.AppendLine ("Terminator request is empty.");
}
else if (!response.ToString ().EndsWith (ansiRequest.Terminator [^1]))
{
throw new InvalidOperationException ($"Terminator doesn't ends with: '{ansiRequest.Terminator [^1]}'");
}
@@ -142,6 +149,11 @@ public class AnsiEscapeSequenceRequest
}
finally
{
if (string.IsNullOrEmpty (error.ToString ()))
{
(string? c1Control, string? code, values, string? terminator) = EscSeqUtils.GetEscapeResult (response.ToString ().ToCharArray ());
}
if (savedIsReportingMouseMoves)
{
switch (Application.Driver)
@@ -159,13 +171,6 @@ public class AnsiEscapeSequenceRequest
}
}
var values = new string? [] { null };
if (string.IsNullOrEmpty (error.ToString ()))
{
(string? c1Control, string? code, values, string? terminator) = EscSeqUtils.GetEscapeResult (response.ToString ().ToCharArray ());
}
AnsiEscapeSequenceResponse ansiResponse = new ()
{
Response = response.ToString (), Error = error.ToString (),

View File

@@ -85,7 +85,7 @@ public sealed class AnsiEscapeSequenceRequests : Scenario
var tvTerminator = new TextView { X = Pos.Left (label), Y = Pos.Bottom (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" };
var btnResponse = new Button { X = Pos.Center (), Y = Pos.Bottom (tvResponse) + 2, Text = "Send Request", IsDefault = true };
var lblSuccess = new Label { X = Pos.Center (), Y = Pos.Bottom (btnResponse) + 1 };
appWindow.Add (lblSuccess);