Browse Source

WIP: Trying to get NetDriver to work properly

tznind 9 months ago
parent
commit
512af810d5

+ 6 - 0
Terminal.Gui/ConsoleDrivers/ConsoleDriver.cs

@@ -15,6 +15,12 @@ namespace Terminal.Gui;
 /// </remarks>
 public abstract class ConsoleDriver
 {
+
+    /// <summary>
+    /// How long after Esc has been pressed before we give up on getting an Ansi escape sequence
+    /// </summary>
+    internal TimeSpan EscTimeout = TimeSpan.FromMilliseconds (50);
+
     // As performance is a concern, we keep track of the dirty lines and only refresh those.
     // This is in addition to the dirty flag on each cell.
     internal bool []? _dirtyLines;

+ 26 - 4
Terminal.Gui/ConsoleDrivers/NetDriver.cs

@@ -160,8 +160,7 @@ internal class NetEvents : IDisposable
 
         Task.Run (CheckWindowSizeChange, _inputReadyCancellationTokenSource.Token);
 
-        Parser.ExpectResponseT ("m",ProcessRequestResponse,true);
-        Parser.ExpectResponseT ("M", ProcessRequestResponse, true);
+        Parser.UnexpectedResponseHandler = ProcessRequestResponse;
     }
 
 
@@ -204,7 +203,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)
@@ -217,6 +216,12 @@ internal class NetEvents : IDisposable
         {
             Task.Delay (100, cancellationToken).Wait (cancellationToken);
 
+            foreach (var k in ShouldRelease ())
+            {
+                ProcessMapConsoleKeyInfo (k);
+                _inputReady.Set ();
+            }
+
             if (Console.KeyAvailable)
             {
                 return Console.ReadKey (intercept);
@@ -228,6 +233,17 @@ internal class NetEvents : IDisposable
         return default (ConsoleKeyInfo);
     }
 
+    public IEnumerable<ConsoleKeyInfo> ShouldRelease ()
+    {
+        if (Parser.State == AnsiResponseParserState.ExpectingBracket &&
+            DateTime.Now - Parser.StateChangedAt > _consoleDriver.EscTimeout)
+        {
+            return Parser.Release ().Select (o => o.Item2);
+        }
+
+        return [];
+    }
+
     private void ProcessInputQueue ()
     {
         while (_inputReadyCancellationTokenSource is { IsCancellationRequested: false })
@@ -417,7 +433,7 @@ internal class NetEvents : IDisposable
         return true;
     }
 
-    private void ProcessRequestResponse (IEnumerable<Tuple<char, ConsoleKeyInfo>> obj)
+    private bool ProcessRequestResponse (IEnumerable<Tuple<char, ConsoleKeyInfo>> obj)
     {
         // Added for signature compatibility with existing method, not sure what they are even for.
         ConsoleKeyInfo newConsoleKeyInfo = default;
@@ -425,6 +441,12 @@ internal class NetEvents : IDisposable
         ConsoleModifiers mod = default;
 
         ProcessRequestResponse (ref newConsoleKeyInfo, ref key, obj.Select (v=>v.Item2).ToArray (),ref mod);
+
+        // Probably
+        _inputReady.Set ();
+
+        // Handled
+        return true;
     }
 
     // Process a CSI sequence received by the driver (key pressed, mouse event, or request/response event)

+ 1 - 6
Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

@@ -1454,10 +1454,6 @@ internal class WindowsDriver : ConsoleDriver
         return new MainLoop (_mainLoopDriver);
     }
 
-    /// <summary>
-    /// How long after Esc has been pressed before we give up on getting an Ansi escape sequence
-    /// </summary>
-    private TimeSpan _escTimeout = TimeSpan.FromMilliseconds (50);
     private AnsiResponseParser<WindowsConsole.InputRecord> _parser = new ();
 
     internal void ProcessInput (WindowsConsole.InputRecord inputEvent)
@@ -1565,9 +1561,8 @@ internal class WindowsDriver : ConsoleDriver
 
     public IEnumerable<WindowsConsole.InputRecord> ShouldRelease ()
     {
-
         if (_parser.State == AnsiResponseParserState.ExpectingBracket &&
-            DateTime.Now - _parser.StateChangedAt > _escTimeout)
+            DateTime.Now - _parser.StateChangedAt > EscTimeout)
         {
             return _parser.Release ().Select (o => o.Item2);
         }