Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,38 @@ internal ConsoleHostUserInterface(ConsoleHost parent)

_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()
Expand Down
37 changes: 37 additions & 0 deletions test/powershell/Host/ConsoleHost.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1003,3 +1003,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 = "<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
}
}
}