Browse Source

Fixes #4246. v2net doesn't work with Force16Colors (#4247)

* Fixes #4246. v2net doesn't work with Force16Colors

* Fixes #4248. v2net sometimes output mouse inputs after exit the app

* Fix unit test

* Fix cursor visibility and cursor styles

* Fix Ctrl being ignored in the range \u0001-\u001a

* Helper method to map char to control keys

* Add unit test for the MapChar method

* Add more sequences related with macOS

* Fix issue on Windows where sending AltGr+some key fail assertion

* Only Ctrl+I is considered as Tab key and not with more

* Avoid sometimes error when running gnome-terminal

* Captures Ctrl+Shift+Alt+D7

* Exclude Oem1 from assertion

* Captures Ctrl+D4, Ctrl+D5, Ctrl+D6 and Ctrl+D7

* Definitively fixes mouse sequence escape from outputting on exit

* Add unit test for Ctrl+Shift+Alt+7

* Fix regex pattern

* Replace with hexadecimal values

---------

Co-authored-by: Tig <[email protected]>
BDisp 2 months ago
parent
commit
00975d025b

+ 1 - 1
Terminal.Gui/Drivers/EscSeqUtils/EscSeqUtils.cs

@@ -1461,7 +1461,7 @@ public static class EscSeqUtils
         // Handle control keys whose VK codes match the related ASCII value (those below ASCII 33) like ESC
         if (keyInfo.Key != ConsoleKey.None && Enum.IsDefined (typeof (KeyCode), (uint)keyInfo.Key))
         {
-            if (keyInfo.Modifiers.HasFlag (ConsoleModifiers.Control) && keyInfo.Key == ConsoleKey.I)
+            if (keyInfo is { Modifiers: ConsoleModifiers.Control, Key: ConsoleKey.I })
             {
                 return KeyCode.Tab;
             }

+ 16 - 0
Terminal.Gui/Drivers/V2/NetInput.cs

@@ -70,10 +70,23 @@ public class NetInput : ConsoleInput<ConsoleKeyInfo>, INetInput
         }
     }
 
+    private void FlushConsoleInput ()
+    {
+        if (!ConsoleDriver.RunningUnitTests)
+        {
+            while (Console.KeyAvailable)
+            {
+                Console.ReadKey (intercept: true);
+            }
+        }
+    }
+
     /// <inheritdoc/>
     public override void Dispose ()
     {
         base.Dispose ();
+
+        // Disable mouse events first
         Console.Out.Write (EscSeqUtils.CSI_DisableMouseEvents);
 
         //Disable alternative screen buffer.
@@ -83,5 +96,8 @@ public class NetInput : ConsoleInput<ConsoleKeyInfo>, INetInput
         Console.Out.Write (EscSeqUtils.CSI_ShowCursor);
 
         _adjustConsole?.Cleanup ();
+
+        // Flush any pending input so no stray events appear
+        FlushConsoleInput ();
     }
 }

+ 38 - 15
Terminal.Gui/Drivers/V2/NetOutput.cs

@@ -54,24 +54,31 @@ public class NetOutput : OutputBase, IConsoleOutput
     /// <inheritdoc/>
     protected override void AppendOrWriteAttribute (StringBuilder output, Attribute attr, TextStyle redrawTextStyle)
     {
-        EscSeqUtils.CSI_AppendForegroundColorRGB (
-                                                  output,
-                                                  attr.Foreground.R,
-                                                  attr.Foreground.G,
-                                                  attr.Foreground.B
-                                                 );
-
-        EscSeqUtils.CSI_AppendBackgroundColorRGB (
-                                                  output,
-                                                  attr.Background.R,
-                                                  attr.Background.G,
-                                                  attr.Background.B
-                                                 );
+        if (Application.Force16Colors)
+        {
+            output.Append (EscSeqUtils.CSI_SetForegroundColor (attr.Foreground.GetAnsiColorCode ()));
+            output.Append (EscSeqUtils.CSI_SetBackgroundColor (attr.Background.GetAnsiColorCode ()));
+        }
+        else
+        {
+            EscSeqUtils.CSI_AppendForegroundColorRGB (
+                                                      output,
+                                                      attr.Foreground.R,
+                                                      attr.Foreground.G,
+                                                      attr.Foreground.B
+                                                     );
+
+            EscSeqUtils.CSI_AppendBackgroundColorRGB (
+                                                      output,
+                                                      attr.Background.R,
+                                                      attr.Background.G,
+                                                      attr.Background.B
+                                                     );
+        }
 
         EscSeqUtils.CSI_AppendTextStyleChange (output, redrawTextStyle, attr.Style);
     }
 
-
     /// <inheritdoc />
     protected override void Write (StringBuilder output)
     {
@@ -116,9 +123,25 @@ public class NetOutput : OutputBase, IConsoleOutput
     }
 
 
+    private EscSeqUtils.DECSCUSR_Style? _currentDecscusrStyle;
+
     /// <inheritdoc cref="IConsoleOutput.SetCursorVisibility"/>
     public override void SetCursorVisibility (CursorVisibility visibility)
     {
-        Console.Out.Write (visibility == CursorVisibility.Default ? EscSeqUtils.CSI_ShowCursor : EscSeqUtils.CSI_HideCursor);
+        if (visibility != CursorVisibility.Invisible)
+        {
+            if (_currentDecscusrStyle is null || _currentDecscusrStyle != (EscSeqUtils.DECSCUSR_Style)(((int)visibility >> 24) & 0xFF))
+            {
+                _currentDecscusrStyle = (EscSeqUtils.DECSCUSR_Style)(((int)visibility >> 24) & 0xFF);
+
+                Write (EscSeqUtils.CSI_SetCursorStyle ((EscSeqUtils.DECSCUSR_Style)_currentDecscusrStyle));
+            }
+
+            Write (EscSeqUtils.CSI_ShowCursor);
+        }
+        else
+        {
+            Write (EscSeqUtils.CSI_HideCursor);
+        }
     }
 }