From 00975d025b27bc7e53ec325446221016cce6c590 Mon Sep 17 00:00:00 2001 From: BDisp Date: Thu, 2 Oct 2025 15:59:59 +0100 Subject: [PATCH] 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 --- .../Drivers/EscSeqUtils/EscSeqUtils.cs | 2 +- Terminal.Gui/Drivers/V2/NetInput.cs | 16 ++++++ Terminal.Gui/Drivers/V2/NetOutput.cs | 51 ++++++++++++++----- 3 files changed, 54 insertions(+), 15 deletions(-) diff --git a/Terminal.Gui/Drivers/EscSeqUtils/EscSeqUtils.cs b/Terminal.Gui/Drivers/EscSeqUtils/EscSeqUtils.cs index ddeb34372..f9c1d109a 100644 --- a/Terminal.Gui/Drivers/EscSeqUtils/EscSeqUtils.cs +++ b/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; } diff --git a/Terminal.Gui/Drivers/V2/NetInput.cs b/Terminal.Gui/Drivers/V2/NetInput.cs index 10b5e1cf6..bdc7c6fc1 100644 --- a/Terminal.Gui/Drivers/V2/NetInput.cs +++ b/Terminal.Gui/Drivers/V2/NetInput.cs @@ -70,10 +70,23 @@ public class NetInput : ConsoleInput, INetInput } } + private void FlushConsoleInput () + { + if (!ConsoleDriver.RunningUnitTests) + { + while (Console.KeyAvailable) + { + Console.ReadKey (intercept: true); + } + } + } + /// 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, INetInput Console.Out.Write (EscSeqUtils.CSI_ShowCursor); _adjustConsole?.Cleanup (); + + // Flush any pending input so no stray events appear + FlushConsoleInput (); } } diff --git a/Terminal.Gui/Drivers/V2/NetOutput.cs b/Terminal.Gui/Drivers/V2/NetOutput.cs index eea6b3edf..d1c70526d 100644 --- a/Terminal.Gui/Drivers/V2/NetOutput.cs +++ b/Terminal.Gui/Drivers/V2/NetOutput.cs @@ -54,24 +54,31 @@ public class NetOutput : OutputBase, IConsoleOutput /// protected override void AppendOrWriteAttribute (StringBuilder output, Attribute attr, TextStyle redrawTextStyle) { - EscSeqUtils.CSI_AppendForegroundColorRGB ( - output, - attr.Foreground.R, - attr.Foreground.G, - attr.Foreground.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_AppendBackgroundColorRGB ( + output, + attr.Background.R, + attr.Background.G, + attr.Background.B + ); + } EscSeqUtils.CSI_AppendTextStyleChange (output, redrawTextStyle, attr.Style); } - /// protected override void Write (StringBuilder output) { @@ -116,9 +123,25 @@ public class NetOutput : OutputBase, IConsoleOutput } + private EscSeqUtils.DECSCUSR_Style? _currentDecscusrStyle; + /// 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); + } } }