Skip to content

Ensure error message resets the color#17904

Closed
SteveL-MSFT wants to merge 2 commits intoPowerShell:masterfrom
SteveL-MSFT:color-exit
Closed

Ensure error message resets the color#17904
SteveL-MSFT wants to merge 2 commits intoPowerShell:masterfrom
SteveL-MSFT:color-exit

Conversation

@SteveL-MSFT
Copy link
Copy Markdown
Member

@SteveL-MSFT SteveL-MSFT commented Aug 15, 2022

PR Summary

Prior to this change, after writing the error message if your prompt doesn't set any colors, the error color (default red) would continue to write to output.

Looked at changing it so that the color resets at writeline() time but that didn't work correctly across multiple lines as they should all be red, but because lines expect color to be retained, they would show incorrectly.

Ultimately, decided that the error message itself should reset the color (which also means that all output needs to reset at the end rather than expecting PS or ConsoleHost to fix it).

Manually tested with ConciseView and NormalView using:

write-error 1;"hello"

Where the error should be in red even if multiple lines, but hello should be default foreground/background color.

PR Context

Fix #17885

PR Checklist

@ghost ghost assigned daxian-dbw Aug 15, 2022
@SteveL-MSFT SteveL-MSFT added this to the 7.3-Consider milestone Aug 15, 2022
@ghost ghost removed this from the 7.3-Consider milestone Aug 15, 2022
@ghost
Copy link
Copy Markdown

ghost commented Aug 15, 2022

Open PRs should not be assigned to milestone, so they are not assigned to the wrong milestone after they are merged. For backport consideration, use a backport label.

@pull-request-quantifier-deprecated
Copy link
Copy Markdown

This PR has 6 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Small
Size       : +4 -2
Percentile : 2.4%

Total files changed: 1

Change summary by file extension:
.cs : +4 -2

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@daxian-dbw
Copy link
Copy Markdown
Member

I cherry-picked the changes to 7.2.6 branch trying to make sure the changes work on 7.2.next. It turns out this works fine on Windows but not so well on Linux:

screenshot

As shown in the GIF, the first prompt after write-error is in default color, but the following ones are in red color. So, it looks like writing out Reset VT sequence doesn't correctly reset the color change done by Console.ForegroundColor = xxx.

I also tried the 7.3 private build, and it works fine on both Windows and Linux. There are big changes in .NET 7 in the Console area, so I guess that was fixed in .NET 7. However, we need it to work well on 7.2.6 (.NET 6) if we want to backport this change.

@daxian-dbw
Copy link
Copy Markdown
Member

daxian-dbw commented Aug 18, 2022

Given what I found above, I think we should not rely on setting Console.ForegroundColor/BackgroundColor for NormalView/CategoryView. Instead, as long as VT is supported, we can translate ConsoleColor to VT sequences using [System.Management.Automation.VTUtility]::GetEscapeSequence and explicitly use VT sequences to set the color attribute, so that Reset at the end will reliably work on 7.2.6.

One caveat is that [VTUtility]::GetEscapeSequence seems to only convert foreground color, not background color, but that can be added pretty easily, I think.

@daxian-dbw
Copy link
Copy Markdown
Member

Actually, I have question: why do we have to use colors defined in $Host.PrivateData for the NormalView error rendering?
In my opinion, $ErrorView just defines how to render ErrorRecord, but not necessarily coupled with where the color should come from.

After introducing $PSStyle, we say this in the doc:

It replaces $Host.PrivateData as the way to manage colors for formatting rendering.

So, it feels to me we should just use $PSStyle.Formatting.Error regardless of $ErrorView as long as VT is supported.

@SteveL-MSFT SteveL-MSFT added the WG-Interactive-Console the console experience label Aug 18, 2022
@SteveL-MSFT
Copy link
Copy Markdown
Member Author

SteveL-MSFT commented Aug 18, 2022

@daxian-dbw the feedback was that it broke existing users of NormalView who set the color in their $profile. Perhaps we should have a WG review.

There may have been some other changes that would need to be cherry-picked to 7.2 to make it work completely.

@daxian-dbw
Copy link
Copy Markdown
Member

daxian-dbw commented Aug 18, 2022

the feedback was that it broke existing users of NormalView who set the color in their $profile. Perhaps we should have a WG review.

It's already broken for other formatting color settings defined in $host.PrivateData, unless we want to make them all working.

@SteveL-MSFT
Copy link
Copy Markdown
Member Author

Waiting on the WG-Interactive-Console group to decide what to do with PrivateData.

@daxian-dbw
Copy link
Copy Markdown
Member

The Interactive WG discussed $Host.PrivateData vs. $PSStyle.Formatting, and agreed that all formatting color settings should be in the same location, and ErrorRecord formatting should use $PSStyle instead of $host. See #17886 (comment) for details.

@daxian-dbw
Copy link
Copy Markdown
Member

This PR is superseded by #17987

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

$ErrorView = 'NormalView' leaves the console in Error color after rendering an ErrorRecord

3 participants