Browse Source

Remove unneeded abstract ConsoleDriver objects.

BDisp 9 months ago
parent
commit
c1063a00fe

+ 3 - 17
Terminal.Gui/ConsoleDrivers/AnsiEscapeSequence/AnsiEscapeSequenceRequest.cs

@@ -61,23 +61,14 @@ public class AnsiEscapeSequenceRequest
     public static bool TryExecuteAnsiRequest (AnsiEscapeSequenceRequest ansiRequest, out AnsiEscapeSequenceResponse result)
     {
         var error = new StringBuilder ();
-        var savedIsReportingMouseMoves = false;
-        ConsoleDriver? driver = null;
         var values = new string? [] { null };
 
         try
         {
-            driver = Application.Driver;
-
-            savedIsReportingMouseMoves = driver!.IsReportingMouseMoves;
-
-            if (savedIsReportingMouseMoves)
-            {
-                driver.StopReportingMouseMoves ();
-            }
+            ConsoleDriver? driver = Application.Driver;
 
             // Send the ANSI escape sequence
-            ansiRequest.Response = driver.WriteAnsiRequest (ansiRequest);
+            ansiRequest.Response = driver?.WriteAnsiRequest (ansiRequest)!;
 
             if (!string.IsNullOrEmpty (ansiRequest.Response) && !ansiRequest.Response.StartsWith (EscSeqUtils.KeyEsc))
             {
@@ -104,12 +95,7 @@ public class AnsiEscapeSequenceRequest
         {
             if (string.IsNullOrEmpty (error.ToString ()))
             {
-                (string? c1Control, string? code, values, string? terminator) = EscSeqUtils.GetEscapeResult (ansiRequest.Response.ToCharArray ());
-            }
-
-            if (savedIsReportingMouseMoves)
-            {
-                driver?.StartReportingMouseMoves ();
+                (string? _, string? _, values, string? _) = EscSeqUtils.GetEscapeResult (ansiRequest.Response.ToCharArray ());
             }
         }
 

+ 14 - 22
Terminal.Gui/ConsoleDrivers/ConsoleDriver.cs

@@ -144,7 +144,7 @@ public abstract class ConsoleDriver
                     // are correctly combined with the base char, but are ALSO treated as 1 column
                     // width codepoints E.g. `echo "[e`u{0301}`u{0301}]"` will output `[é  ]`.
                     // 
-                    // Until this is addressed (see Issue #), we do our best by 
+                    // Until this is addressed (see Issue #), we do our best by
                     // a) Attempting to normalize any CM with the base char to it's left
                     // b) Ignoring any CMs that don't normalize
                     if (Col > 0)
@@ -167,7 +167,7 @@ public abstract class ConsoleDriver
                             if (normalized.Length == 1)
                             {
                                 // It normalized! We can just set the Cell to the left with the
-                                // normalized codepoint 
+                                // normalized codepoint
                                 Contents [Row, Col - 1].Rune = (Rune)normalized [0];
 
                                 // Ignore. Don't move to next column because we're already there
@@ -377,7 +377,7 @@ public abstract class ConsoleDriver
                 {
                     Contents [r, c] = new Cell
                     {
-                        Rune = (rune != default ? rune : (Rune)' '),
+                        Rune = rune != default ? rune : (Rune)' ',
                         Attribute = CurrentAttribute, IsDirty = true
                     };
                     _dirtyLines! [r] = true;
@@ -562,11 +562,6 @@ public abstract class ConsoleDriver
 
     #region Mouse and Keyboard
 
-    /// <summary>
-    /// Gets whether the mouse is reporting move events.
-    /// </summary>
-    public abstract bool IsReportingMouseMoves { get; internal set; }
-
     /// <summary>Event fired when a key is pressed down. This is a precursor to <see cref="KeyUp"/>.</summary>
     public event EventHandler<Key>? KeyDown;
 
@@ -613,16 +608,6 @@ public abstract class ConsoleDriver
     /// <param name="ctrl">If <see langword="true"/> simulates the Ctrl key being pressed.</param>
     public abstract void SendKeys (char keyChar, ConsoleKey key, bool shift, bool alt, bool ctrl);
 
-    /// <summary>
-    ///     Provide handling for the terminal start reporting mouse events.
-    /// </summary>
-    public abstract void StartReportingMouseMoves ();
-
-    /// <summary>
-    ///     Provide handling for the terminal stop reporting mouse events.
-    /// </summary>
-    public abstract void StopReportingMouseMoves ();
-
     /// <summary>
     ///     Provide handling for the terminal write ANSI escape sequence request.
     /// </summary>
@@ -630,22 +615,29 @@ public abstract class ConsoleDriver
     /// <returns>The request response.</returns>
     public abstract string WriteAnsiRequest (AnsiEscapeSequenceRequest ansiRequest);
 
+    /// <summary>
+    ///     Provide proper writing to send escape sequence recognized by the <see cref="ConsoleDriver"/>.
+    /// </summary>
+    /// <param name="ansi"></param>
+    public abstract void WriteRaw (string ansi);
+
     internal string ReadAnsiResponseDefault (AnsiEscapeSequenceRequest ansiRequest)
     {
         var response = new StringBuilder ();
-        int index = 0;
+        var index = 0;
 
         while (Console.KeyAvailable)
         {
             // Peek the next key
             ConsoleKeyInfo keyInfo = Console.ReadKey (true); // true to not display on the console
 
-            if ((index == 0 && keyInfo.KeyChar == EscSeqUtils.KeyEsc) || (index > 0 && keyInfo.KeyChar != EscSeqUtils.KeyEsc))
+            if (index == 0 && keyInfo.KeyChar != EscSeqUtils.KeyEsc)
             {
-                // Append the current key to the response
-                response.Append (keyInfo.KeyChar);
+                break;
             }
 
+            response.Append (keyInfo.KeyChar);
+
             // 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])

+ 2 - 16
Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs

@@ -177,33 +177,19 @@ internal class CursesDriver : ConsoleDriver
         return true;
     }
 
-    public override bool IsReportingMouseMoves { get; internal set; }
-
-    public override void StartReportingMouseMoves ()
+    public void StartReportingMouseMoves ()
     {
         if (!RunningUnitTests)
         {
             Console.Out.Write (EscSeqUtils.CSI_EnableMouseEvents);
-
-            IsReportingMouseMoves = true;
         }
     }
 
-    public override void StopReportingMouseMoves ()
+    public void StopReportingMouseMoves ()
     {
         if (!RunningUnitTests)
         {
             Console.Out.Write (EscSeqUtils.CSI_DisableMouseEvents);
-
-            IsReportingMouseMoves = false;
-
-            Thread.Sleep (100); // Allow time for mouse stopping and to flush the input buffer
-
-            // Flush the input buffer to avoid reading stale input
-            while (Console.KeyAvailable)
-            {
-                Console.ReadKey (true);
-            }
         }
     }
 

+ 2 - 8
Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs

@@ -40,9 +40,6 @@ public class FakeDriver : ConsoleDriver
     public static Behaviors FakeBehaviors = new ();
     public override bool SupportsTrueColor => false;
 
-    /// <inheritdoc />
-    public override bool IsReportingMouseMoves { get; internal set; }
-
     public FakeDriver ()
     {
         Cols = FakeConsole.WindowWidth = FakeConsole.BufferWidth = FakeConsole.WIDTH;
@@ -396,13 +393,10 @@ public class FakeDriver : ConsoleDriver
     }
 
     /// <inheritdoc />
-    public override void StartReportingMouseMoves () { throw new NotImplementedException (); }
-
-    /// <inheritdoc />
-    public override void StopReportingMouseMoves () { throw new NotImplementedException (); }
+    public override string WriteAnsiRequest (AnsiEscapeSequenceRequest ansiRequest) { throw new NotImplementedException (); }
 
     /// <inheritdoc />
-    public override string WriteAnsiRequest (AnsiEscapeSequenceRequest ansiRequest) { throw new NotImplementedException (); }
+    public override void WriteRaw (string ansi) { throw new NotImplementedException (); }
 
     public void SetBufferSize (int width, int height)
     {

+ 2 - 21
Terminal.Gui/ConsoleDrivers/NetDriver.cs

@@ -1414,38 +1414,19 @@ internal class NetDriver : ConsoleDriver
 
     #region Mouse Handling
 
-    public override bool IsReportingMouseMoves { get; internal set; }
-
-    public override void StartReportingMouseMoves ()
+    public void StartReportingMouseMoves ()
     {
         if (!RunningUnitTests)
         {
             Console.Out.Write (EscSeqUtils.CSI_EnableMouseEvents);
-
-            IsReportingMouseMoves = true;
         }
     }
 
-    public override void StopReportingMouseMoves ()
+    public void StopReportingMouseMoves ()
     {
         if (!RunningUnitTests)
         {
             Console.Out.Write (EscSeqUtils.CSI_DisableMouseEvents);
-
-            IsReportingMouseMoves = false;
-
-            while (_mainLoopDriver is { _netEvents: { } } && Console.KeyAvailable)
-            {
-                _mainLoopDriver._netEvents._waitForStart.Set ();
-                _mainLoopDriver._netEvents._waitForStart.Reset ();
-
-                _mainLoopDriver._netEvents._forceRead = true;
-            }
-
-            if (_mainLoopDriver is { _netEvents: { } })
-            {
-                _mainLoopDriver._netEvents._forceRead = false;
-            }
         }
     }