From 84cc3b3c8c824b4e4d336c0d5a4bc2caeca6742e Mon Sep 17 00:00:00 2001 From: BDisp Date: Wed, 25 Oct 2023 13:49:00 +0100 Subject: [PATCH] Fixes #2930. Console.CursorLeft-- throw an exception with NetDriver on Linux (v2). (#2931) * Fixes #2930. Console.CursorLeft-- throw an exception with NetDriver on Linux (v2). * Fix inverted parameters. * Fixes #2666. With #2659 Cursor flickers on NetDriver * Fix typo. * Using the _cachedCursorVisibility field. --- Terminal.Gui/Application.cs | 5 +-- .../ConsoleDrivers/EscSeqUtils/EscSeqUtils.cs | 2 +- Terminal.Gui/ConsoleDrivers/NetDriver.cs | 34 +++++++++---------- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/Terminal.Gui/Application.cs b/Terminal.Gui/Application.cs index b1bcc63fd..1521c86f8 100644 --- a/Terminal.Gui/Application.cs +++ b/Terminal.Gui/Application.cs @@ -480,7 +480,7 @@ namespace Terminal.Gui { #endif resume = false; var runState = Begin (view); - // If ExitRunLoopAfterFirstIteration is true then the user must dispose of the runToken + // If EndAfterFirstIteration is true then the user must dispose of the runToken // by using NotifyStopRunState event. RunLoop (runState); if (!EndAfterFirstIteration) { @@ -668,9 +668,6 @@ namespace Terminal.Gui { } else if (Current.SuperView == null && Current?.Modal == true) { Refresh (); } - if (Driver.EnsureCursorVisibility ()) { - state.Toplevel.SetNeedsDisplay (); - } } firstIteration = false; diff --git a/Terminal.Gui/ConsoleDrivers/EscSeqUtils/EscSeqUtils.cs b/Terminal.Gui/ConsoleDrivers/EscSeqUtils/EscSeqUtils.cs index 08c2d686b..b90a7a05f 100644 --- a/Terminal.Gui/ConsoleDrivers/EscSeqUtils/EscSeqUtils.cs +++ b/Terminal.Gui/ConsoleDrivers/EscSeqUtils/EscSeqUtils.cs @@ -173,7 +173,7 @@ public static class EscSeqUtils { /// /// /// - public static string CSI_SetCursorPosition (int x, int y) => $"{CSI}{y};{x}H"; + public static string CSI_SetCursorPosition (int y, int x) => $"{CSI}{y};{x}H"; //ESC [ ; f - HVP Horizontal Vertical Position* Cursor moves to; coordinate within the viewport, where is the column of the line diff --git a/Terminal.Gui/ConsoleDrivers/NetDriver.cs b/Terminal.Gui/ConsoleDrivers/NetDriver.cs index b442f83fa..ee451e45a 100644 --- a/Terminal.Gui/ConsoleDrivers/NetDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/NetDriver.cs @@ -759,8 +759,8 @@ internal class NetDriver : ConsoleDriver { Attribute redrawAttr = new Attribute (); var lastCol = -1; - //GetCursorVisibility (out CursorVisibility savedVisibitity); - //SetCursorVisibility (CursorVisibility.Invisible); + var savedVisibitity = _cachedCursorVisibility; + SetCursorVisibility (CursorVisibility.Invisible); for (var row = top; row < rows; row++) { if (Console.WindowHeight < 1) { @@ -812,7 +812,7 @@ internal class NetDriver : ConsoleDriver { output.Append (rune.ToString ()); if (rune.IsSurrogatePair () && rune.GetColumns () < 2) { WriteToConsole (output, ref lastCol, row, ref outputWidth); - Console.CursorLeft--; + SetCursorPosition (col - 1, row); } Contents [row, col].IsDirty = false; } @@ -824,7 +824,7 @@ internal class NetDriver : ConsoleDriver { } SetCursorPosition (0, 0); - //SetCursorVisibility (savedVisibitity); + _cachedCursorVisibility = savedVisibitity; void WriteToConsole (StringBuilder output, ref int lastCol, int row, ref int outputWidth) { @@ -888,20 +888,20 @@ internal class NetDriver : ConsoleDriver { #region Cursor Handling bool SetCursorPosition (int col, int row) { - //if (IsWinPlatform) { - // Could happens that the windows is still resizing and the col is bigger than Console.WindowWidth. - try { - Console.SetCursorPosition (col, row); + if (IsWinPlatform) { + // Could happens that the windows is still resizing and the col is bigger than Console.WindowWidth. + try { + Console.SetCursorPosition (col, row); + return true; + } catch (Exception) { + return false; + } + } else { + // + 1 is needed because non-Windows is based on 1 instead of 0 and + // Console.CursorTop/CursorLeft isn't reliable. + Console.Out.Write (EscSeqUtils.CSI_SetCursorPosition (row + 1, col + 1)); return true; - } catch (Exception) { - return false; } - // BUGBUG: This breaks -usc on WSL; not sure why. But commenting out fixes. - //} else { - // // TODO: Explain why + 1 is needed (and why we do this for non-Windows). - // Console.Out.Write (EscSeqUtils.CSI_SetCursorPosition (row + 1, col + 1)); - // return true; - //} } CursorVisibility? _cachedCursorVisibility; @@ -926,7 +926,7 @@ internal class NetDriver : ConsoleDriver { { _cachedCursorVisibility = visibility; var isVisible = RunningUnitTests ? visibility == CursorVisibility.Default : Console.CursorVisible = visibility == CursorVisibility.Default; - //Console.Out.Write (isVisible ? EscSeqUtils.CSI_ShowCursor : EscSeqUtils.CSI_HideCursor); + Console.Out.Write (isVisible ? EscSeqUtils.CSI_ShowCursor : EscSeqUtils.CSI_HideCursor); return isVisible; }