Fix NetDriver and WindowsDriver.

This commit is contained in:
BDisp
2024-10-31 20:30:22 +00:00
parent ba607f1380
commit ee8d040407
2 changed files with 61 additions and 31 deletions

View File

@@ -198,7 +198,7 @@ internal class NetEvents : IDisposable
return null;
}
private static ConsoleKeyInfo ReadConsoleKeyInfo (CancellationToken cancellationToken, bool intercept = true)
private ConsoleKeyInfo ReadConsoleKeyInfo (CancellationToken cancellationToken, bool intercept = true)
{
// if there is a key available, return it without waiting
// (or dispatching work to the thread queue)
@@ -215,6 +215,30 @@ internal class NetEvents : IDisposable
{
return Console.ReadKey (intercept);
}
if (EscSeqUtils.IncompleteCkInfos is null && EscSeqRequests is { Statuses.Count: > 0 })
{
if (_retries > 1)
{
EscSeqRequests.Statuses.TryDequeue (out EscSeqReqStatus seqReqStatus);
lock (seqReqStatus.AnsiRequest._responseLock)
{
seqReqStatus.AnsiRequest.Response = string.Empty;
seqReqStatus.AnsiRequest.RaiseResponseFromInput (seqReqStatus.AnsiRequest, string.Empty);
}
_retries = 0;
}
else
{
_retries++;
}
}
else
{
_retries = 0;
}
}
cancellationToken.ThrowIfCancellationRequested ();
@@ -223,6 +247,7 @@ internal class NetEvents : IDisposable
}
internal bool _forceRead;
private int _retries;
private void ProcessInputQueue ()
{
@@ -230,7 +255,10 @@ internal class NetEvents : IDisposable
{
try
{
_waitForStart.Wait (_inputReadyCancellationTokenSource.Token);
if (!_forceRead)
{
_waitForStart.Wait (_inputReadyCancellationTokenSource.Token);
}
}
catch (OperationCanceledException)
{
@@ -258,6 +286,11 @@ internal class NetEvents : IDisposable
return;
}
if (EscSeqUtils.IncompleteCkInfos is { })
{
EscSeqUtils.InsertArray (EscSeqUtils.IncompleteCkInfos, _cki);
}
if ((consoleKeyInfo.KeyChar == (char)KeyCode.Esc && !_isEscSeq)
|| (consoleKeyInfo.KeyChar != (char)KeyCode.Esc && _isEscSeq))
{
@@ -277,7 +310,7 @@ internal class NetEvents : IDisposable
_isEscSeq = true;
if (consoleKeyInfo.KeyChar != Key.Esc && consoleKeyInfo.KeyChar <= Key.Space)
if (_cki is { } && _cki [^1].KeyChar != Key.Esc && consoleKeyInfo.KeyChar != Key.Esc && consoleKeyInfo.KeyChar <= Key.Space)
{
ProcessRequestResponse (ref newConsoleKeyInfo, ref key, _cki, ref mod);
_cki = null;
@@ -322,6 +355,11 @@ internal class NetEvents : IDisposable
ProcessMapConsoleKeyInfo (consoleKeyInfo);
if (_retries > 0)
{
_retries = 0;
}
break;
}
}
@@ -460,17 +498,13 @@ internal class NetEvents : IDisposable
if (seqReqStatus is { })
{
//HandleRequestResponseEvent (c1Control, code, values, terminating);
StringBuilder sb = new ();
foreach (ConsoleKeyInfo keyChar in cki)
{
sb.Append (keyChar.KeyChar);
}
var ckiString = EscSeqUtils.ToString (cki);
lock (seqReqStatus.AnsiRequest._responseLock)
{
seqReqStatus.AnsiRequest.Response = sb.ToString ();
seqReqStatus.AnsiRequest.RaiseResponseFromInput (seqReqStatus.AnsiRequest, sb.ToString ());
seqReqStatus.AnsiRequest.Response = ckiString;
seqReqStatus.AnsiRequest.RaiseResponseFromInput (seqReqStatus.AnsiRequest, ckiString);
}
return;
@@ -1165,7 +1199,7 @@ internal class NetDriver : ConsoleDriver
return new MainLoop (_mainLoopDriver);
}
private void ProcessInput (InputResult inputEvent)
{
switch (inputEvent.EventType)
@@ -1457,12 +1491,12 @@ internal class NetDriver : ConsoleDriver
};
_mainLoopDriver._netEvents.EscSeqRequests.Add (ansiRequest);
_mainLoopDriver._netEvents._forceRead = true;
}
if (!_ansiResponseTokenSource.IsCancellationRequested && Console.KeyAvailable)
if (!_ansiResponseTokenSource.IsCancellationRequested)
{
_mainLoopDriver._netEvents._forceRead = true;
_mainLoopDriver._netEvents._waitForStart.Set ();
if (!_mainLoopDriver._waitForProbe.IsSet)
@@ -1497,6 +1531,9 @@ internal class NetDriver : ConsoleDriver
return response;
}
/// <inheritdoc />
public override void WriteRaw (string ansi) { throw new NotImplementedException (); }
private MouseEventArgs ToDriverMouse (NetEvents.MouseEvent me)
{
//System.Diagnostics.Debug.WriteLine ($"X: {me.Position.X}; Y: {me.Position.Y}; ButtonState: {me.ButtonState}");

View File

@@ -18,9 +18,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using static Terminal.Gui.ConsoleDrivers.ConsoleKeyMapping;
using static Terminal.Gui.SpinnerStyle;
namespace Terminal.Gui;
@@ -1066,9 +1064,6 @@ internal class WindowsDriver : ConsoleDriver
public override bool SupportsTrueColor => RunningUnitTests || (Environment.OSVersion.Version.Build >= 14931 && _isWindowsTerminal);
/// <inheritdoc />
public override bool IsReportingMouseMoves { get; internal set; }
public WindowsConsole WinConsole { get; private set; }
public WindowsConsole.KeyEventRecord FromVKPacketToKeyEventRecord (WindowsConsole.KeyEventRecord keyEvent)
@@ -1196,7 +1191,7 @@ internal class WindowsDriver : ConsoleDriver
}
}
/// <inheritdoc />
/// <inheritdoc/>
public override string WriteAnsiRequest (AnsiEscapeSequenceRequest ansiRequest)
{
if (_mainLoopDriver is null)
@@ -1217,12 +1212,11 @@ internal class WindowsDriver : ConsoleDriver
try
{
if (WinConsole?.WriteANSI (ansiRequest.Request) == true)
{
Thread.Sleep (100); // Allow time for the terminal to respond
WriteRaw (ansiRequest.Request);
return ReadAnsiResponseDefault (ansiRequest);
}
Thread.Sleep (100); // Allow time for the terminal to respond
return ReadAnsiResponseDefault (ansiRequest);
}
catch (Exception)
{
@@ -1232,18 +1226,17 @@ internal class WindowsDriver : ConsoleDriver
{
_mainLoopDriver._suspendRead = false;
}
return string.Empty;
}
#region Not Implemented
public override void StartReportingMouseMoves () { throw new NotImplementedException (); }
public override void StopReportingMouseMoves () { throw new NotImplementedException (); }
public override void Suspend () { throw new NotImplementedException (); }
public override void WriteRaw (string ansi)
{
WinConsole?.WriteANSI (ansi);
}
#endregion
public WindowsConsole.ConsoleKeyInfoEx ToConsoleKeyInfoEx (WindowsConsole.KeyEventRecord keyEvent)