Skip to content
Merged
1 change: 1 addition & 0 deletions .vsts-ci/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ variables:
POWERSHELL_TELEMETRY_OPTOUT: 1
Comment thread
SteveL-MSFT marked this conversation as resolved.
Resolved
# Avoid expensive initialization of dotnet cli, see: https://donovanbrown.com/post/Stop-wasting-time-during-NET-Core-builds
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
__SuppressAnsiEscapeSequences: 1

resources:
- repo: self
Expand Down
1 change: 1 addition & 0 deletions .vsts-ci/mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ variables:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
# Turn off Homebrew analytics
HOMEBREW_NO_ANALYTICS: 1
__SuppressAnsiEscapeSequences: 1

resources:
- repo: self
Expand Down
1 change: 1 addition & 0 deletions .vsts-ci/windows-daily.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ variables:
POWERSHELL_TELEMETRY_OPTOUT: 1
# Avoid expensive initialization of dotnet cli, see: https://donovanbrown.com/post/Stop-wasting-time-during-NET-Core-builds
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
__SuppressAnsiEscapeSequences: 1

resources:
- repo: self
Expand Down
1 change: 1 addition & 0 deletions .vsts-ci/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ variables:
POWERSHELL_TELEMETRY_OPTOUT: 1
# Avoid expensive initialization of dotnet cli, see: https://donovanbrown.com/post/Stop-wasting-time-during-NET-Core-builds
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
__SuppressAnsiEscapeSequences: 1

resources:
- repo: self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public string ToString(string directory)
}
Comment thread
SteveL-MSFT marked this conversation as resolved.
Resolved
Comment thread
SteveL-MSFT marked this conversation as resolved.
Resolved

/// <summary>
/// Returns the string representation of the match object with the matched line passed
/// Returns the string representation of the match object with the matched line passed
/// in as <paramref name="line"/> and trims the path to be relative to
/// the<paramref name="directory"/> argument.
/// </summary>
Expand Down Expand Up @@ -315,10 +315,10 @@ public string ToEmphasizedString(string directory)
/// <returns>The matched line with matched text inverted.</returns>
private string EmphasizeLine()
{
const string InvertColorsVT100 = "\u001b[7m";
const string ResetVT100 = "\u001b[0m";
string invertColorsVT100 = VTUtility.GetEscapeSequence(VTUtility.VT.Inverse);
string resetVT100 = VTUtility.GetEscapeSequence(VTUtility.VT.Reset);

char[] chars = new char[(_matchIndexes.Count * (InvertColorsVT100.Length + ResetVT100.Length)) + Line.Length];
char[] chars = new char[(_matchIndexes.Count * (invertColorsVT100.Length + resetVT100.Length)) + Line.Length];
int lineIndex = 0;
int charsIndex = 0;
for (int i = 0; i < _matchIndexes.Count; i++)
Expand All @@ -329,17 +329,17 @@ private string EmphasizeLine()
lineIndex = _matchIndexes[i];

// Adds opening vt sequence
InvertColorsVT100.CopyTo(0, chars, charsIndex, InvertColorsVT100.Length);
charsIndex += InvertColorsVT100.Length;
invertColorsVT100.CopyTo(0, chars, charsIndex, invertColorsVT100.Length);
charsIndex += invertColorsVT100.Length;

// Adds characters being emphasized
Line.CopyTo(lineIndex, chars, charsIndex, _matchLengths[i]);
lineIndex += _matchLengths[i];
charsIndex += _matchLengths[i];

// Adds closing vt sequence
ResetVT100.CopyTo(0, chars, charsIndex, ResetVT100.Length);
charsIndex += ResetVT100.Length;
resetVT100.CopyTo(0, chars, charsIndex, resetVT100.Length);
charsIndex += resetVT100.Length;
}

// Adds remaining characters in line
Expand Down Expand Up @@ -1294,8 +1294,8 @@ public string[] Exclude
private int _postContext = 0;

// When we are in Raw mode or pre- and postcontext are zero, use the _noContextTracker, since we will not be needing trackedLines.
private IContextTracker GetContextTracker() => (Raw || (_preContext == 0 && _postContext == 0))
? _noContextTracker
private IContextTracker GetContextTracker() => (Raw || (_preContext == 0 && _postContext == 0))
? _noContextTracker
: new ContextTracker(_preContext, _postContext);

// This context tracker is only used for strings which are piped
Expand All @@ -1322,6 +1322,12 @@ private IContextTracker GetContextTracker() => (Raw || (_preContext == 0 && _pos
/// </summary>
protected override void BeginProcessing()
{
string suppressVt = Environment.GetEnvironmentVariable("__SuppressAnsiEscapeSequences");
if (!string.IsNullOrEmpty(suppressVt))
{
NoEmphasis = true;
}

if (!SimpleMatch)
{
RegexOptions regexOptions = CaseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -739,35 +739,18 @@ private static IEnumerable<FormatViewDefinition> ViewsOf_System_Management_Autom
$maxDepth = 10
$ellipsis = ""`u{2026}""
$resetColor = ''
if ($Host.UI.SupportsVirtualTerminal) {
$resetColor = ""`e[0m""
if ($Host.UI.SupportsVirtualTerminal -and !(Test-Path env:__SuppressAnsiEscapeSequences)) {
$resetColor = [System.Management.Automation.VTUtility]::GetEscapeSequence(
[System.Management.Automation.VTUtility+VT]::Reset
)
}

function Get-VT100Color([string] $color) {
if (! $Host.UI.SupportsVirtualTerminal) {
function Get-VT100Color([ConsoleColor] $color) {
if (!$Host.UI.SupportsVirtualTerminal -or (Test-Path env:__SuppressAnsiEscapeSequences)) {
Comment thread
SteveL-MSFT marked this conversation as resolved.
Resolved
return ''
}

$colors = @{
'Black' = ""`e[2;30m""
'DarkRed' = ""`e[2;31m""
'DarkGreen' = ""`e[2;32m""
'DarkYellow' = ""`e[2;33m""
'DarkBlue' = ""`e[2;34m""
'DarkMagenta' = ""`e[2;35m""
'DarkCyan' = ""`e[2;36m""
'Gray' = ""`e[2;37m""
'DarkGray' = ""`e[1;30m""
'Red' = ""`e[1;31m""
'Green' = ""`e[1;32m""
'Yellow' = ""`e[1;33m""
'Blue' = ""`e[1;34m""
'Magenta' = ""`e[1;35m""
'Cyan' = ""`e[1;36m""
'White' = ""`e[1;37m""
}

return $colors[$color]
return [System.Management.Automation.VTUtility]::GetEscapeSequence($color)
}

function Show-ErrorRecord($obj, [int]$indent = 0, [int]$depth = 1) {
Expand Down Expand Up @@ -979,35 +962,18 @@ function Get-ConciseViewPositionMessage {
Set-StrictMode -Off

$resetColor = ''
if ($Host.UI.SupportsVirtualTerminal) {
$resetColor = ""`e[0m""
if ($Host.UI.SupportsVirtualTerminal -and !(Test-Path env:__SuppressAnsiEscapeSequences)) {
$resetColor = [System.Management.Automation.VTUtility]::GetEscapeSequence(
[System.Management.Automation.VTUtility+VT]::Reset
)
}

function Get-VT100Color([string] $color) {
if (! $Host.UI.SupportsVirtualTerminal) {
function Get-VT100Color([ConsoleColor] $color) {
if (!$Host.UI.SupportsVirtualTerminal -or (Test-Path env:__SuppressAnsiEscapeSequences)) {
return ''
}

$colors = @{
'Black' = ""`e[2;30m""
'DarkRed' = ""`e[2;31m""
'DarkGreen' = ""`e[2;32m""
'DarkYellow' = ""`e[2;33m""
'DarkBlue' = ""`e[2;34m""
'DarkMagenta' = ""`e[2;35m""
'DarkCyan' = ""`e[2;36m""
'Gray' = ""`e[2;37m""
'DarkGray' = ""`e[1;30m""
'Red' = ""`e[1;31m""
'Green' = ""`e[1;32m""
'Yellow' = ""`e[1;33m""
'Blue' = ""`e[1;34m""
'Magenta' = ""`e[1;35m""
'Cyan' = ""`e[1;36m""
'White' = ""`e[1;37m""
}

return $colors[$color]
return [System.Management.Automation.VTUtility]::GetEscapeSequence($color)
}

# return length of string sans VT100 codes
Expand Down
83 changes: 83 additions & 0 deletions src/System.Management.Automation/utils/VTUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;

namespace System.Management.Automation
{
/// <summary>
/// Class to help with VT escape sequences.
/// </summary>
public sealed class VTUtility
{
/// <summary>
/// Available VT escape codes other than colors.
/// </summary>
public enum VT
{
/// <summary>Reset the text style.</summary>
Reset,

/// <summary>Invert the foreground and background colors.</summary>
Inverse
}

private static readonly Dictionary<ConsoleColor, string> ConsoleColors = new Dictionary<ConsoleColor, string>
{
{ ConsoleColor.Black, "\x1b[2;30m" },
{ ConsoleColor.Gray, "\x1b[2;37m" },
{ ConsoleColor.Red, "\x1b[1;31m" },
{ ConsoleColor.Green, "\x1b[1;32m" },
{ ConsoleColor.Yellow, "\x1b[1;33m" },
{ ConsoleColor.Blue, "\x1b[1;34m" },
{ ConsoleColor.Magenta, "\x1b[1;35m" },
{ ConsoleColor.Cyan, "\x1b[1;36m" },
{ ConsoleColor.White, "\x1b[1;37m" },
{ ConsoleColor.DarkRed, "\x1b[2;31m" },
{ ConsoleColor.DarkGreen, "\x1b[2;32m" },
{ ConsoleColor.DarkYellow, "\x1b[2;33m" },
{ ConsoleColor.DarkBlue, "\x1b[2;34m" },
{ ConsoleColor.DarkMagenta, "\x1b[2;35m" },
{ ConsoleColor.DarkCyan, "\x1b[2;36m" },
{ ConsoleColor.DarkGray, "\x1b[1;30m" },
};

private static readonly Dictionary<VT, string> VTCodes = new Dictionary<VT, string>
{
{ VT.Reset, "\x1b[0m" },
{ VT.Inverse, "\x1b[7m" }
};

/// <summary>
/// Return the VT escape sequence for a ConsoleColor.
/// </summary>
/// <param name="color">
/// The ConsoleColor to return the equivalent VT escape sequence.
/// </param>
/// <returns>
/// The requested VT escape sequence.
/// </returns>
public static string GetEscapeSequence(ConsoleColor color)
{
string value = string.Empty;
ConsoleColors.TryGetValue(color, out value);
return value;
}

/// <summary>
/// Return the VT escape sequence for a supported VT enum value.
/// </summary>
/// <param name="vt">
/// The VT code to return the VT escape sequence.
/// </param>
/// <returns>
/// The requested VT escape sequence.
/// </returns>
public static string GetEscapeSequence(VT vt)
{
string value = string.Empty;
VTCodes.TryGetValue(vt, out value);
return value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

Describe '$env:__SuppressAnsiEscapeSequences tests' -Tag CI {
BeforeAll {
$originalSuppressPref = $env:__SuppressAnsiEscapeSequences
}

AfterAll {
$env:__SuppressAnsiEscapeSequences = $originalSuppressPref
}


Context 'Allow Escape Sequences' {
BeforeAll {
Remove-Item env:__SuppressAnsiEscapeSequences -ErrorAction Ignore
}

It 'Select-String emits VT' {
"select this string" | Select-String 'this' | Out-String |Should -BeLikeExactly "*`e*"
}

It 'ConciseView emits VT' {
$oldErrorView = $ErrorView

try {
$ErrorView = 'ConciseView'
Invoke-Expression '1/d'
}
catch {
$e = $_
}
finally {
$ErrorView = $oldErrorView
}

$e | Out-String | Should -BeLikeExactly "*`e*"
}

It 'Get-Error emits VT' {
try {
1/0
}
catch {
# ignore
}

Get-Error | Out-String | Should -BeLikeExactly "*`e*"
}
}

Context 'No Escape Sequences' {
BeforeAll {
$env:__SuppressAnsiEscapeSequences = 1
}

It 'Select-String does not emit VT' {
"select this string" | Select-String 'this' | Out-String | Should -Not -BeLikeExactly "*`e*"
}

It 'ConciseView does not emit VT' {
$oldErrorView = $ErrorView

try {
$ErrorView = 'ConciseView'
Invoke-Expression '1/d'
}
catch {
$e = $_
}
finally {
$ErrorView = $oldErrorView
}

$e | Out-String | Should -Not -BeLikeExactly "*`e*"
}

It 'Get-Error does not emit VT' {
try {
1/0
}
catch {
# ignore
}

Get-Error | Out-String | Should -Not -BeLikeExactly "*`e*"
}
}
}
Loading