Write out OSC indicator only if the stdout is not redirected (#17419)

This commit is contained in:
Dongbo Wang
2022-05-25 14:29:47 -07:00
committed by GitHub
parent 80a207160b
commit 0882737efb
2 changed files with 12 additions and 3 deletions
@@ -3,6 +3,7 @@
using System;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Threading;
using Dbg = System.Management.Automation.Diagnostics;
@@ -10,7 +11,7 @@ using Dbg = System.Management.Automation.Diagnostics;
namespace Microsoft.PowerShell
{
internal partial
class ConsoleHostUserInterface : System.Management.Automation.Host.PSHostUserInterface
class ConsoleHostUserInterface : PSHostUserInterface
{
/// <summary>
/// Called at the end of a prompt loop to take down any progress display that might have appeared and purge any
@@ -48,7 +49,7 @@ namespace Microsoft.PowerShell
_pendingProgress = null;
if (SupportsVirtualTerminal && PSStyle.Instance.Progress.UseOSCIndicator)
if (SupportsVirtualTerminal && !PSHost.IsStdOutputRedirected && PSStyle.Instance.Progress.UseOSCIndicator)
{
// OSC sequence to turn off progress indicator
// https://github.com/microsoft/terminal/issues/6700
@@ -99,7 +100,7 @@ namespace Microsoft.PowerShell
{
// 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 (SupportsVirtualTerminal && PSStyle.Instance.Progress.UseOSCIndicator)
if (SupportsVirtualTerminal && !PSHost.IsStdOutputRedirected && PSStyle.Instance.Progress.UseOSCIndicator)
{
int percentComplete = record.PercentComplete;
if (percentComplete < 0)
@@ -237,6 +237,14 @@ Describe 'Tests for $PSStyle automatic variable' {
$PSStyle.Progress.MaxWidth = $maxWidth
}
}
It 'Do not use OSC indicator when the stdout is redirected' {
$pwsh = Join-Path $PSHOME 'pwsh'
## In the case that the stdout is redirected, pwsh should not write the OSC indicator Ansi sequence.
$result = & $pwsh -noprofile -Command { $PSStyle.Progress.UseOSCIndicator = $true; 'hello'} | Format-List
$result | Out-String -Stream | Should -BeExactly 'hello'
}
}
Describe 'Handle strings with escape sequences in formatting' {