From 8ccba379a6c1e058f8892aa533300215aa59ba95 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 4 Mar 2021 06:49:56 -0800 Subject: [PATCH 1/3] Emit DECRST 1 sequence to restore keyboard mode on Unix systems on pwsh exit --- src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index 9de877ba46c..3c9337c271c 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -254,6 +254,11 @@ internal static int Start(string bannerText, string helpText) { #if LEGACYTELEMETRY TelemetryAPI.ReportExitTelemetry(s_theConsoleHost); +#endif +#if UNIX + // https://github.com/dotnet/runtime/issues/27626 leaves terminal in application mode + // for now, we explicitly emit DECRST 1 sequence + s_theConsoleHost.UI.Write("\x1b[?1l"); #endif s_theConsoleHost.Dispose(); } From 26df8a0179039cf85b12d15305b0675d3c0c0ae6 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 4 Mar 2021 15:22:18 -0800 Subject: [PATCH 2/3] emit DECCKM before prompt and disable before executing commandline --- .../host/msh/ConsoleHost.cs | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index 3c9337c271c..9c359c4517e 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -256,9 +256,12 @@ internal static int Start(string bannerText, string helpText) TelemetryAPI.ReportExitTelemetry(s_theConsoleHost); #endif #if UNIX - // https://github.com/dotnet/runtime/issues/27626 leaves terminal in application mode - // for now, we explicitly emit DECRST 1 sequence - s_theConsoleHost.UI.Write("\x1b[?1l"); + if (s_theConsoleHost.IsInteractive && s_theConsoleHost.UI.SupportsVirtualTerminal) + { + // https://github.com/dotnet/runtime/issues/27626 leaves terminal in application mode + // for now, we explicitly emit DECRST 1 sequence + s_theConsoleHost.UI.Write("\x1b[?1l"); + } #endif s_theConsoleHost.Dispose(); } @@ -2478,6 +2481,14 @@ internal void Run(bool inputLoopIsNested) ui.Write(prompt); } +#if UNIX + if (c.SupportsVirtualTerminal) + { + // enable DECCKM as .NET requires cursor keys to emit VT for Console class + c.Write("\x1b[?1h"); + } +#endif + previousResponseWasEmpty = false; // There could be a profile. So there could be a user defined custom readline command line = ui.ReadLineWithTabCompletion(_exec); @@ -2581,6 +2592,14 @@ e is RemoteException || } else { +#if UNIX + if (c.SupportsVirtualTerminal) + { + // disable DECCKM to standard mode as applications may not expect VT for cursor keys + c.Write("\x1b[?1l"); + } +#endif + if (_parent.IsRunningAsync && !_parent.IsNested) { _exec.ExecuteCommandAsync(line, out e, Executor.ExecutionOptions.AddOutputter | Executor.ExecutionOptions.AddToHistory); From 791231c7090421e282a93c4fe355107456ae476e Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 4 Mar 2021 15:27:40 -0800 Subject: [PATCH 3/3] make escape sequences const strings --- .../host/msh/ConsoleHost.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index 9c359c4517e..e1ad56a6dc6 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -54,6 +54,10 @@ internal sealed partial class ConsoleHost internal const int ExitCodeInitFailure = 70; // Internal Software Error internal const int ExitCodeBadCommandLineParameter = 64; // Command Line Usage Error private const uint SPI_GETSCREENREADER = 0x0046; +#if UNIX + internal const string DECCKM_ON = "\x1b[?1h"; + internal const string DECCKM_OFF = "\x1b[?1l"; +#endif [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] @@ -260,7 +264,7 @@ internal static int Start(string bannerText, string helpText) { // https://github.com/dotnet/runtime/issues/27626 leaves terminal in application mode // for now, we explicitly emit DECRST 1 sequence - s_theConsoleHost.UI.Write("\x1b[?1l"); + s_theConsoleHost.UI.Write(DECCKM_OFF); } #endif s_theConsoleHost.Dispose(); @@ -2485,7 +2489,7 @@ internal void Run(bool inputLoopIsNested) if (c.SupportsVirtualTerminal) { // enable DECCKM as .NET requires cursor keys to emit VT for Console class - c.Write("\x1b[?1h"); + c.Write(DECCKM_ON); } #endif @@ -2596,7 +2600,7 @@ e is RemoteException || if (c.SupportsVirtualTerminal) { // disable DECCKM to standard mode as applications may not expect VT for cursor keys - c.Write("\x1b[?1l"); + c.Write(DECCKM_OFF); } #endif