From 004a0ea25f90bda9856bb914a888c18eb874eb6e Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Mon, 1 Mar 2021 06:59:25 -0800 Subject: [PATCH 1/4] Add `$PSStyle.Progress.UseOSCIndicator` to enable progress indicator in terminal --- .../msh/ConsoleHostUserInterfaceProgress.cs | 22 +++++++++++++++++++ .../PowerShellCore_format_ps1xml.cs | 6 +++-- .../FormatAndOutput/common/PSStyle.cs | 6 +++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs index 50dedf410a7..74c036a7748 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs @@ -47,6 +47,13 @@ class ConsoleHostUserInterface : System.Management.Automation.Host.PSHostUserInt } _pendingProgress = null; + + if (ExperimentalFeature.IsEnabled(ExperimentalFeature.PSAnsiProgressFeatureName) && PSStyle.Instance.Progress.UseOSCIndicator) + { + // OSC sequence to turn off progress indicator + // https://github.com/microsoft/terminal/issues/6700 + Console.Write("\x1b]9;4;0\x1b\\"); + } } } @@ -92,6 +99,21 @@ class ConsoleHostUserInterface : System.Management.Automation.Host.PSHostUserInt { // Update the progress pane only when the timer set up the update flag or WriteProgress is completed. // As a result, we do not block WriteProgress and whole script and eliminate unnecessary console locks and updates. + if (ExperimentalFeature.IsEnabled(ExperimentalFeature.PSAnsiProgressFeatureName) && PSStyle.Instance.Progress.UseOSCIndicator) + { + // OSC sequence to turn off progress indicator + // https://github.com/microsoft/terminal/issues/6700 + int percentComplete = record.PercentComplete; + if (percentComplete < 0) + { + // Write-Progress allows for negative percent complete, but not greater than 100 + // but OSC sequence is limited from 0 to 100. + percentComplete = 0; + } + + Console.Write($"\x1b]9;4;1;{percentComplete}\x1b\\"); + } + _progPane.Show(_pendingProgress); } } diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index d9763164679..c0a30061524 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -1687,8 +1687,8 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co } private const string PreReleaseStringScriptBlock = @" - if ($_.PrivateData -and - $_.PrivateData.ContainsKey('PSData') -and + if ($_.PrivateData -and + $_.PrivateData.ContainsKey('PSData') -and $_.PrivateData.PSData.ContainsKey('PreRelease')) { $_.PrivateData.PSData.PreRelease @@ -2055,6 +2055,7 @@ private static IEnumerable ViewsOf_System_Management_Autom .AddItemScriptBlock(@"""$($_.Progress.Style)$($_.Progress.Style.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Progress.Style") .AddItemScriptBlock(@"""$($_.Progress.MaxWidth)""", label: "Progress.MaxWidth") .AddItemScriptBlock(@"""$($_.Progress.View)""", label: "Progress.View") + .AddItemScriptBlock(@"""$($_.Progress.UseOSCIndicator)""", label: "Progress.UseOSCIndicator") .AddItemScriptBlock(@"""$($_.Foreground.Black)$($_.Foreground.Black.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Foreground.Black") .AddItemScriptBlock(@"""$($_.Foreground.White)$($_.Foreground.White.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Foreground.White") .AddItemScriptBlock(@"""$($_.Foreground.DarkGray)$($_.Foreground.DarkGray.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Foreground.DarkGray") @@ -2114,6 +2115,7 @@ private static IEnumerable ViewsOf_System_Management_Autom .AddItemScriptBlock(@"""$($_.Style)$($_.Style.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Style") .AddItemProperty(@"MaxWidth") .AddItemProperty(@"View") + .AddItemProperty(@"UseOSCIndicator") .EndEntry() .EndList()); } diff --git a/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs b/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs index aa00bdf9e18..3a252030f04 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs @@ -290,6 +290,12 @@ public sealed class ProgressConfiguration /// Gets or sets the view for progress bar. /// public ProgressView View { get; set; } = ProgressView.Minimal; + + /// + /// Enable use of OSC 9 4 to show indicator in terminal. + /// Ensure terminal supports this sequence before enabling. + /// + public bool UseOSCIndicator { get; set; } = false; } /// From 63635c14db4e566e7e83d4fe3f0312061ef15bf3 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Mon, 1 Mar 2021 07:09:42 -0800 Subject: [PATCH 2/4] fix codefactor issue --- .../FormatAndOutput/common/PSStyle.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs b/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs index 3a252030f04..0d912aca3e7 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs @@ -292,8 +292,7 @@ public sealed class ProgressConfiguration public ProgressView View { get; set; } = ProgressView.Minimal; /// - /// Enable use of OSC 9 4 to show indicator in terminal. - /// Ensure terminal supports this sequence before enabling. + /// Gets or sets a value indicating whether to use OSC 9 4 to show indicator in terminal. /// public bool UseOSCIndicator { get; set; } = false; } From 18cc947f3c348b1d3df6989e919ddc1f8fb4430d Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Mon, 1 Mar 2021 08:14:09 -0800 Subject: [PATCH 3/4] fix and move comment --- .../host/msh/ConsoleHostUserInterfaceProgress.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs index 74c036a7748..406c36c8a00 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs @@ -101,8 +101,6 @@ class ConsoleHostUserInterface : System.Management.Automation.Host.PSHostUserInt // As a result, we do not block WriteProgress and whole script and eliminate unnecessary console locks and updates. if (ExperimentalFeature.IsEnabled(ExperimentalFeature.PSAnsiProgressFeatureName) && PSStyle.Instance.Progress.UseOSCIndicator) { - // OSC sequence to turn off progress indicator - // https://github.com/microsoft/terminal/issues/6700 int percentComplete = record.PercentComplete; if (percentComplete < 0) { @@ -111,6 +109,8 @@ class ConsoleHostUserInterface : System.Management.Automation.Host.PSHostUserInt percentComplete = 0; } + // OSC sequence to turn on progress indicator + // https://github.com/microsoft/terminal/issues/6700 Console.Write($"\x1b]9;4;1;{percentComplete}\x1b\\"); } From 5d9bd703cb0279ee04ebb1116ea27f2071c17f88 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 10 Mar 2021 22:26:45 -0800 Subject: [PATCH 4/4] Update src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs Co-authored-by: Dongbo Wang --- .../FormatAndOutput/common/PSStyle.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs b/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs index 0d912aca3e7..b8e1890b37f 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs @@ -292,7 +292,7 @@ public sealed class ProgressConfiguration public ProgressView View { get; set; } = ProgressView.Minimal; /// - /// Gets or sets a value indicating whether to use OSC 9 4 to show indicator in terminal. + /// Gets or sets a value indicating whether to use Operating System Command (OSC) control sequences 'ESC ]9;4;' to show indicator in terminal. /// public bool UseOSCIndicator { get; set; } = false; }