From 90d50998a61f7fde323dc09b444d0cbb42ff2146 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Sat, 17 Apr 2021 20:40:43 -0700 Subject: [PATCH 1/4] Fix `Minimal` ProgressView to handle Activity that is longer than console width --- .../host/msh/ProgressNode.cs | 6 ++++++ .../Microsoft.PowerShell.Utility/Write-Progress.Tests.ps1 | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs index ca2694bcabc..23c8ae9ba6a 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs @@ -387,6 +387,12 @@ internal static bool IsMinimalProgressRenderingEnabled() maxWidth = PSStyle.Instance.Progress.MaxWidth; } + // if the activity is really long, only use up to half the width + if (Activity.Length > maxWidth / 2) + { + Activity = Activity.Substring(0, maxWidth / 2) + PSObjectHelper.Ellipsis; + } + // 4 is for the extra space and square brackets below and one extra space int barWidth = maxWidth - Activity.Length - indentation - 4; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Progress.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Progress.Tests.ps1 index f346b61f079..6e6c9ee57c4 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Progress.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Progress.Tests.ps1 @@ -22,4 +22,8 @@ Describe "Write-Progress DRT Unit Tests" -Tags "CI" { It "all params works" -Pending { { Write-Progress -Activity 'myactivity' -Status 'mystatus' -Id 1 -ParentId 2 -Completed:$false -current 'current' -sec 1 -percent 1 } | Should -Not -Throw } + + It 'Activity longer than console width works' { + { Write-Progress -Activity ('a' * ([console]::WindowWidth + 1)) -Status ('b' * ([console]::WindowWidth + 1)) -Id 1 } | Should -Not -Throw + } } From 47d420fb0f84ffebae9b1bad453ac36eac4adacc Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 19 Apr 2021 20:24:06 -0700 Subject: [PATCH 2/4] handle case where console window width is extremely narrow --- .../host/msh/ProgressNode.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs index 23c8ae9ba6a..801c113f8db 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs @@ -400,7 +400,8 @@ internal static bool IsMinimalProgressRenderingEnabled() int padding = maxWidth + PSStyle.Instance.Progress.Style.Length + PSStyle.Instance.Reverse.Length + PSStyle.Instance.ReverseOff.Length; sb.Append(PSStyle.Instance.Reverse); - if (StatusDescription.Length > barWidth - secRemainLength) + int maxStatusLength = barWidth - secRemainLength - 1; + if (maxStatusLength > 0 && StatusDescription.Length > barWidth - secRemainLength) { sb.Append(StatusDescription.Substring(0, barWidth - secRemainLength - 1)); sb.Append(PSObjectHelper.Ellipsis); @@ -410,10 +411,14 @@ internal static bool IsMinimalProgressRenderingEnabled() sb.Append(StatusDescription); } - sb.Append(string.Empty.PadRight(barWidth + PSStyle.Instance.Reverse.Length - sb.Length - secRemainLength)); + int emptyPadLength = barWidth + PSStyle.Instance.Reverse.Length - sb.Length - secRemainLength; + if (emptyPadLength > 0) + { + sb.Append(string.Empty.PadRight(emptyPadLength)); + } sb.Append(secRemain); - if (PercentComplete > 0 && PercentComplete < 100) + if (PercentComplete > 0 && PercentComplete < 100 && barWidth > 0) { int barLength = PercentComplete * barWidth / 100; if (barLength >= barWidth) @@ -421,7 +426,10 @@ internal static bool IsMinimalProgressRenderingEnabled() barLength = barWidth - 1; } - sb.Insert(barLength + PSStyle.Instance.Reverse.Length, PSStyle.Instance.ReverseOff); + if (barLength < sb.Length) + { + sb.Insert(barLength + PSStyle.Instance.Reverse.Length, PSStyle.Instance.ReverseOff); + } } else { From 640064814b0f5c920a44d96e15196fbcfad28ee2 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 19 Apr 2021 20:25:40 -0700 Subject: [PATCH 3/4] address codefactor --- src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs index 801c113f8db..3692cdf15e8 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs @@ -416,6 +416,7 @@ internal static bool IsMinimalProgressRenderingEnabled() { sb.Append(string.Empty.PadRight(emptyPadLength)); } + sb.Append(secRemain); if (PercentComplete > 0 && PercentComplete < 100 && barWidth > 0) From 5dc7d8eb63541fb59dba78e4d1bb5d889ddf6a84 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 20 Apr 2021 09:54:39 -0700 Subject: [PATCH 4/4] address Ilya's feedback to not modify original string --- .../host/msh/ProgressNode.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs index 3692cdf15e8..84f2f0355f2 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs @@ -388,13 +388,18 @@ internal static bool IsMinimalProgressRenderingEnabled() } // if the activity is really long, only use up to half the width + string activity; if (Activity.Length > maxWidth / 2) { - Activity = Activity.Substring(0, maxWidth / 2) + PSObjectHelper.Ellipsis; + activity = Activity.Substring(0, maxWidth / 2) + PSObjectHelper.Ellipsis; + } + else + { + activity = Activity; } // 4 is for the extra space and square brackets below and one extra space - int barWidth = maxWidth - Activity.Length - indentation - 4; + int barWidth = maxWidth - activity.Length - indentation - 4; var sb = new StringBuilder(); int padding = maxWidth + PSStyle.Instance.Progress.Style.Length + PSStyle.Instance.Reverse.Length + PSStyle.Instance.ReverseOff.Length; @@ -442,7 +447,7 @@ internal static bool IsMinimalProgressRenderingEnabled() "{0}{1}{2} [{3}]{4}", indent, PSStyle.Instance.Progress.Style, - Activity, + activity, sb.ToString(), PSStyle.Instance.Reset) .PadRight(padding));