GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Enable simpler ANSI formatting via `$PSStyle` and support suppressing ANSI output by SteveL-MSFT · Pull Request #13758 · PowerShell/PowerShell

Conversation

@SteveL-MSFT

@SteveL-MSFT Steve Lee (SteveL-MSFT) commented Oct 7, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

PR Summary

See #13071 for details.

  • Expose $PSStyle automatic variable to get or set ANSI escape sequences associated with text decoration
  • Formatters for $PSStyle to help users see what the output would look like using one of the colors/attributes. Note that current design choice is to automatically show nested types in $PSStyle as I expect users dumping that variable are interested in all the choices.
  • Update formatting engine to support stripping ANSI escape sequences if not supported or explicitly disabled
  • To preserve existing behavior, redirected output (from pwsh) is always plaintext

After this is merged:

  • Remove __SuppressAnsiExscapeSequences code as it's no longer needed
  • Update existing ErrorRecord and Get-Error formatting to use $PSStyle

PR Context

Fix #13071

PR Checklist

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Make sure all .h, .cpp, .cs, .ps1 and .psm1 files have the correct copyright header
  • This PR is ready to merge and is not Work in Progress.
    • If the PR is work in progress, please add the prefix WIP: or [ WIP ] to the beginning of the title (the WIP bot will keep its status check at Pending while the prefix is present) and remove the prefix when the PR is ready.
  • Breaking changes
    • None
    • OR
    • Experimental feature(s) needed
      • Experimental feature name(s): PSAnsiRendering
  • User-facing changes
    • Not Applicable
    • OR
    • Documentation needed
      • Issue filed: ExperimentalFeature: PSAnsiRendering MicrosoftDocs/PowerShell-Docs#7009
  • Testing - New and feature
    • N/A or can only be tested interactively
    • OR
    • Make sure you've added a new test if existing tests do not effectively test the code changed
  • Tooling
    • I have considered the user experience from a tooling perspective and don't believe tooling will be impacted.
    • OR
    • I have considered the user experience from a tooling perspective and enumerated concerns in the summary. This may include:
      • Impact on PowerShell Editor Services which is used in the PowerShell extension for VSCode (which runs in a different PS Host).
      • Impact on Completions (both in the console and in editors) - one of PowerShell's most powerful features.
      • Impact on PSScriptAnalyzer (which provides linting & formatting in the editor extensions).
      • Impact on EditorSyntax (which provides syntax highlighting with in VSCode, GitHub, and many other editors).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ghost ghost assigned Aditya Patwardhan (adityapatwardhan) Oct 7, 2020
@ghost ghost added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Oct 11, 2020
@ghost ghost removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Oct 24, 2020
@SteveL-MSFT
Steve Lee (SteveL-MSFT) force-pushed the rendering branch 6 times, most recently from 57589ea to da999d9 Compare October 25, 2020 23:16
@SteveL-MSFT

Steve Lee (SteveL-MSFT) commented Oct 26, 2020

Copy link
Copy Markdown
Member Author

Still need to add tests

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ghost ghost added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept labels Oct 29, 2020
@ghost ghost added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Nov 8, 2020
@ghost ghost added the Stale label Nov 23, 2020
@ghost

Deleted user (ghost) commented Nov 23, 2020

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 15 days. It will be closed if no further activity occurs within 10 days of this comment.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

powercode
Staffan Gustafsson (powercode) reviewed Nov 23, 2020
View reviewed changes
Comment thread src/System.Management.Automation/FormatAndOutput/common/StringDecorated.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/Utils.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ghost ghost removed Stale Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept labels Nov 23, 2020
@SteveL-MSFT

Steve Lee (SteveL-MSFT) commented Nov 25, 2020

Copy link
Copy Markdown
Member Author

Still need to have WriteVerbose, etc... use the $PSStyle settings before taking this out of draft

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ghost ghost added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept labels Nov 30, 2020
powercode
Staffan Gustafsson (powercode) reviewed Dec 1, 2020
View reviewed changes
Comment thread src/System.Management.Automation/FormatAndOutput/common/StringDecorated.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

powercode
Staffan Gustafsson (powercode) approved these changes Dec 1, 2020
View reviewed changes
Comment thread src/System.Management.Automation/FormatAndOutput/common/StringDecorated.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/FormatAndOutput/common/StringDecorated.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/FormatAndOutput/common/StringDecorated.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/FormatAndOutput/common/StringDecorated.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/Utils.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@powercode

Staffan Gustafsson (powercode) commented Dec 1, 2020

Copy link
Copy Markdown
Collaborator

I really like this PR! It will be super useful to me!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@SteveL-MSFT
Steve Lee (SteveL-MSFT) marked this pull request as ready for review December 1, 2020 14:14
@SteveL-MSFT
Steve Lee (SteveL-MSFT) requested review from Andrew (anmenaga) and Dongbo Wang (daxian-dbw) as code owners December 1, 2020 14:14
powercode
Staffan Gustafsson (powercode) reviewed Dec 1, 2020
View reviewed changes
Comment thread src/System.Management.Automation/FormatAndOutput/common/StringDecorated.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

daxian-dbw
Dongbo Wang (daxian-dbw) reviewed Dec 1, 2020
View reviewed changes

@daxian-dbw Dongbo Wang (daxian-dbw) left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Publish my comments so far, will continue to review.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/System.Management.Automation/engine/CommandBase.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/CommandBase.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/CommandBase.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/InitialSessionState.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/CommandBase.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/Utils.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

daxian-dbw
Dongbo Wang (daxian-dbw) reviewed Dec 2, 2020
View reviewed changes
Comment thread src/System.Management.Automation/FormatAndOutput/common/ILineOutput.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/FormatAndOutput/common/StringDecorated.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/FormatAndOutput/common/StringDecorated.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/FormatAndOutput/common/StringDecorated.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ghost ghost removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Dec 11, 2020
Steve Lee (SteveL-MSFT) added 2 commits December 10, 2020 22:14
@SteveL-MSFT
Fix formatting of streams and added tests
56ff2c4
@SteveL-MSFT
fix codefactor issue
e4dc572
@iSazonov

Ilya (iSazonov) commented Dec 11, 2020

Copy link
Copy Markdown
Collaborator

So

PS>"$($PSStyle.Formatting.Error)custom error message"`

will output red colored string.

In time of evaluating the string we already know values of all constants - SupportsVirtualTerminal, isHost, Console.IsOutputRedirected. If so why do we need the filtering at all if we can return $null at the evaluating time if we do not need the ANSI value?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

daxian-dbw
Dongbo Wang (daxian-dbw) approved these changes Dec 11, 2020
View reviewed changes
@SteveL-MSFT

Steve Lee (SteveL-MSFT) commented Dec 11, 2020

Copy link
Copy Markdown
Member Author

Ilya (@iSazonov) Error is currently a special case due to the custom formatting that happens. After this gets merged, I'll update the error formatting to use $PSStyle.Formatting.Error.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

daxian-dbw
Dongbo Wang (daxian-dbw) reviewed Dec 11, 2020
View reviewed changes
Comment thread src/System.Management.Automation/engine/Utils.cs
if (!InternalTestHooks.BypassOutputRedirectionCheck &&
((PSStyle.Instance.OutputRendering == OutputRendering.PlainText) ||
(formatStyle == FormatStyle.Error && Console.IsErrorRedirected) ||
(formatStyle != FormatStyle.Error && Console.IsOutputRedirected)))

@daxian-dbw Dongbo Wang (daxian-dbw) Dec 11, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Steve Lee (@SteveL-MSFT), this method currently directly check on Console.IsErrorRedirected and Console.IsOutputRedirected. I think we shouldn't directly check on those in PowerShell engine, but should instead have separate parameters for the caller to provide that information. The engine should not assume there is a console available, and it's up to the host implementation to pass in whether there is error/output redirection when calling this method.

I've put this comment in the tracking issue #14387, so you can fix it in a separate PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

SteveL-MSFT reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@SteveL-MSFT Steve Lee (SteveL-MSFT) Dec 11, 2020

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#14397

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@iSazonov

Ilya (iSazonov) commented Dec 11, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

Steve Lee (@SteveL-MSFT) I say that $PSStyle could not be a const and be evaluated - as result we have no need to filter out escapes because $PSStyle would return $null if user turns off coloring or terminal does not support escapes and so on.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@daxian-dbw

Dongbo Wang (daxian-dbw) commented Dec 11, 2020

Copy link
Copy Markdown
Member

In time of evaluating the string we already know values of all constants - SupportsVirtualTerminal, isHost, Console.IsOutputRedirected.

Ilya (@iSazonov) The PowerShell engine doesn't know if the output/error is being reidrected. It cannot depedn on Console.IsOutputRedirected because it may be hosted in a Windows GUI applicatioin which doesn't have a console at all.
The information about whether the output/error is redirected or whether ANSI sequences should be stripped off because the process's output/error is redirected should be decided only by the host implementation.

Take the host implementation of the PowerShell sub kernel (jupyter kernel) in .NET Interactive as an instance, it's a console application, and the process's output/error streams are always redirected to send to the frontend -- jupyter notebook, which is capable of handling ANSI sequences. So, in this case, even thought Console.IsOutputRedirected and Console.IsErrorRedirected return true, the ANSI sequences shouldn't be stripped off.

To summarize, the host implementation has the final call on whether ANSI sequences should be stripped off or not, and that's why I opened #14387.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iSazonov

Ilya (iSazonov) commented Dec 11, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

To summarize, the host implementation has the final call on whether ANSI sequences should be stripped off or not

Why can't PowerShell engine get this information from the host implementation?

because it may be hosted in a Windows GUI applicatioin which doesn't have a console at all.

So PowerShell engine can get get this information from the host implementation?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@daxian-dbw

Dongbo Wang (daxian-dbw) commented Dec 11, 2020

Copy link
Copy Markdown
Member

I don't know how that would work and how well that would work if it works.

  • From a design point of view, I like to decouple engine and host instead of more coupling.
  • From an implementation point of view, a host may dynamically change how it want the ANSI sequences to be handled.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@daxian-dbw

Dongbo Wang (daxian-dbw) commented Dec 11, 2020

Copy link
Copy Markdown
Member

Ilya (@iSazonov) I will merge this PR to make the cut of 7.2.0-preview.2 release build. Feel free to comment in #14387 about how you think this feature can be improved.

iSazonov reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@daxian-dbw
Dongbo Wang (daxian-dbw) merged commit a99ea2a into PowerShell:master Dec 11, 2020
@daxian-dbw Dongbo Wang (daxian-dbw) added the CL-Engine Indicates that a PR should be marked as an engine change in the Change Log label Dec 11, 2020
@daxian-dbw Dongbo Wang (daxian-dbw) added this to the 7.2.0-preview.2 milestone Dec 11, 2020
@SteveL-MSFT Steve Lee (SteveL-MSFT) mentioned this pull request Dec 11, 2020
Add isOutputRedirected parameter to GetFormatStyleString() method #14397
Merged
8 of 14 tasks
@SteveL-MSFT
Steve Lee (SteveL-MSFT) deleted the rendering branch December 11, 2020 19:20
This was referenced Dec 14, 2020
Attributing Text Effects #11890
Closed
The output of $Error[1] | Out-String contains ANSI escape sequences #13416
Closed
@rjmholt Rob Holt (rjmholt) added CL-Experimental Indicates that a PR should be marked as an Experimental Feature in the Change Log and removed CL-Engine Indicates that a PR should be marked as an engine change in the Change Log labels Dec 15, 2020
@ghost

Deleted user (ghost) commented Dec 15, 2020

Copy link
Copy Markdown

🎉v7.2.0-preview.2 has been released which incorporates this pull request.:tada:

Handy links:

  • Release Notes
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ghost ghost mentioned this pull request Dec 15, 2020
ANSI escape handling and PowerShell #13071
Closed
@rjmholt

Rob Holt (rjmholt) commented Dec 16, 2020

Copy link
Copy Markdown
Collaborator

These changes don't seem to work in the Integrated Console: #14435

iSazonov reacted with confused emoji
All reactions
  • 😕 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Jan 27, 2021
Enhance Get-Member/Reflection Output with Colorization #14665
Closed
Write-error gives weird symbols when using >> to redirect it to a file. #14655
Closed
@iSazonov Ilya (iSazonov) mentioned this pull request Oct 28, 2021
Does PSStyle early initialization less expensive #16320
Merged
22 tasks
@iSazonov Ilya (iSazonov) mentioned this pull request Jan 31, 2025
Occasional NullReferenceException in Microsoft.PowerShell.ProgresPane.Hide() when updating multiple progress bars on PS 7.4.0 #21021
Closed
5 tasks done
@kborowinski Kris Borowinski (kborowinski) mentioned this pull request May 4, 2025
Fix the NREs when writing to console from multiple threads #25440
Merged
6 of 21 tasks
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
@SteveL-MSFT
Enable ANSI formatting via $PSStyle and support suppressing ANSI ou…
99e9a48
…tput (PowerShell#13758)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@daxian-dbw Dongbo Wang (daxian-dbw) daxian-dbw approved these changes
@iSazonov Ilya (iSazonov) iSazonov left review comments
@anmenaga Andrew (anmenaga) Awaiting requested review from anmenaga
+1 more reviewer
@powercode Staffan Gustafsson (powercode) powercode approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@daxian-dbw Dongbo Wang (daxian-dbw)

Labels

CL-Experimental Indicates that a PR should be marked as an Experimental Feature in the Change Log

Projects

None yet

Milestone

7.2.0-preview.2

Development

Successfully merging this pull request may close these issues.

ANSI escape handling and PowerShell

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@SteveL-MSFT @powercode @daxian-dbw @iSazonov @rjmholt @adityapatwardhan
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.