Repository navigation
Add $env:__SuppressAnsiEscapeSequences to control whether to have VT escape sequence in output
#10814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Travis Plunk (TravisEz13)
merged 11 commits into
PowerShell:master
from
SteveL-MSFT:outputpreference
Oct 24, 2019
Merged
Add $env:__SuppressAnsiEscapeSequences to control whether to have VT escape sequence in output
#10814
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
753531d
add $OutputPreference to control whether to have VT escape seq in output
SteveL-MSFT 696a009
address soem CodeFactor issues, but rest are for consistency with exi…
SteveL-MSFT a981c59
address Paul's feedback
SteveL-MSFT 4a67fe2
change to use $env:__SuppressAnsiEscapeSequences instead of automatic…
SteveL-MSFT 4a10b30
fix tests
SteveL-MSFT a8c396e
address CodeFactor
SteveL-MSFT 847b288
Update src/System.Management.Automation/utils/VTUtils.cs
SteveL-MSFT 3e980bb
address Travis' feedback
SteveL-MSFT 630a9f3
Merge branch 'outputpreference' of github.com:SteveL-MSFT/PowerShell …
SteveL-MSFT 6371cee
fix codacy issues
SteveL-MSFT 2c5b4a9
fix select-string tests
SteveL-MSFT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } |
89 changes: 89 additions & 0 deletions
89
test/powershell/Language/Scripting/SuppressAnsiEscapeSequence.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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*" | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.