From 2f3f39906f6f58e1028672ae3aeffb83934c5153 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 12 Apr 2021 20:11:50 -0700 Subject: [PATCH] resolve merge conflict (#14969) --- .../host/msh/ConsoleHostUserInterface.cs | 32 +++++++++++++++- test/powershell/Host/ConsoleHost.Tests.ps1 | 37 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index 14425da82c..6d0c0ff250 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -60,8 +60,38 @@ namespace Microsoft.PowerShell _parent = parent; _rawui = new ConsoleHostRawUserInterface(this); - SupportsVirtualTerminal = TryTurnOnVtMode(); + SupportsVirtualTerminal = true; _isInteractiveTestToolListening = false; + + if (ExperimentalFeature.IsEnabled("PSAnsiRendering")) + { + // check if TERM env var is set + // `dumb` means explicitly don't use VT + // `xterm-mono` and `xtermm` means support VT, but emit plaintext + switch (Environment.GetEnvironmentVariable("TERM")) + { + case "dumb": + SupportsVirtualTerminal = false; + break; + case "xterm-mono": + case "xtermm": + PSStyle.Instance.OutputRendering = OutputRendering.PlainText; + break; + default: + break; + } + + // widely supported by CLI tools via https://no-color.org/ + if (Environment.GetEnvironmentVariable("NO_COLOR") != null) + { + PSStyle.Instance.OutputRendering = OutputRendering.PlainText; + } + } + + if (SupportsVirtualTerminal) + { + SupportsVirtualTerminal = TryTurnOnVtMode(); + } } internal bool TryTurnOnVtMode() diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1 index e57b6357c7..667ef77824 100644 --- a/test/powershell/Host/ConsoleHost.Tests.ps1 +++ b/test/powershell/Host/ConsoleHost.Tests.ps1 @@ -1008,3 +1008,40 @@ Describe 'Console host name' -Tag CI { (Get-Process -Id $PID).Name | Should -BeExactly 'pwsh' } } + +Describe 'TERM env var' -Tag CI { + BeforeAll { + $oldTERM = $env:TERM + $PSDefaultParameterValues.Add('It:Skip', (-not $EnabledExperimentalFeatures.Contains('PSAnsiRendering'))) + } + + AfterAll { + $env:TERM = $oldTERM + $PSDefaultParameterValues.Remove('It:Skip') + } + + It 'TERM = "dumb"' { + $env:TERM = 'dumb' + pwsh -noprofile -command '$Host.UI.SupportsVirtualTerminal' | Should -BeExactly 'False' + } + + It 'TERM = ""' -TestCases @( + @{ term = "xterm-mono" } + @{ term = "xtermm" } + ) { + param ($term) + + $env:TERM = $term + pwsh -noprofile -command '$PSStyle.OutputRendering' | Should -BeExactly 'PlainText' + } + + It 'NO_COLOR' { + try { + $env:NO_COLOR = 1 + pwsh -noprofile -command '$PSStyle.OutputRendering' | Should -BeExactly 'PlainText' + } + finally { + $env:NO_COLOR = $null + } + } +}