-
Notifications
You must be signed in to change notification settings - Fork 8.1k
don't insert linebreaks to output (except for tables) #5193
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
don't insert linebreaks to output (except for tables) #5193
Conversation
d27bd62 to
1b9c754
Compare
don't use current console window width to add linebreaks to output (including debug, error, and verbose) explicitly change errorrecord formatter to not add linebreaks set width to int.maxvalue if not specified, in some special cases if width is int.maxvalue, set to console.windowwidth so that you don't get int.maxvalue padding (like tables)
1b9c754 to
57c8c6f
Compare
| pwsh -c "& { [Console]::Error.WriteLine('$longtext') }" 2>&1 > $testdrive\error.txt | ||
| $e = Get-Content -Path $testdrive\error.txt | ||
| $e.Count | Should BeExactly 1 | ||
| $e | Should Match $longtext |
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.
Maybe use "BeExactly"? It seems better for testing a formatting.
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.
The problem currently is that redirection writes with a BOM, so this would fail
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.
We read with Get-Content -Path $testdrive\error.txt - I believe there is not BOM if we don't use -Byte.
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.
Good point about Get-Content, I'll change it
| $origDebugPref = $DebugPreference | ||
| $DebugPreference = "Continue" | ||
| try { | ||
| $out = Write-Debug $text *>&1 |
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.
Maybe use exactly "2>&1"
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.
Debug isn’t stream number 2 and it seems you can only redirect debug if you redirect all streams
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.
It seems works:
PS> $a=Write-Verbose "ext " -Verbose 4>&1
PS> $a
VERBOSE: ext
PS> $b=Write-Debug "qwerty " -Debug 5>&1
Confirm
Continue with this operation?
[Y] Yes [A] Yes to All [H] Halt Command [S] Suspend [?] Help (default is "Y"): y
PS C:\Users\sie> $b
DEBUG: qwertyThere 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.
Maybe I did something wrong when I tried it. Will update.
| $DebugPreference = "Continue" | ||
| try { | ||
| $out = Write-Debug $text *>&1 | ||
| $out | Should Match $text |
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.
Maybe use "$out | Should BeExactly $longtext"?
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.
Same problem as above in that redirection currently adds a BOM
| $DebugPreference = $origDebugPref | ||
| } | ||
| } | ||
| } No newline at end of file |
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.
Please add new line at EOF.
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.
Will do
| pwsh -c Write-Error -Message $longtext 2>&1 > $testdrive\error.txt | ||
| $e = Get-Content -Path $testdrive\error.txt | ||
| $e.Count | Should BeExactly 4 | ||
| $e[0] | Should Match $longtext |
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.
Can we use more strong match pattern? I'm again worried about false positives.
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.
Write-Error adds extra info prefixed to the text. The important bits for this test is the long text not getting choppedby a linebreak
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.
I see - no false positives can be.
Closed.
| $origVerbosePref = $VerbosePreference | ||
| $VerbosePreference = "continue" | ||
| try { | ||
| $out = Write-Verbose $text *>&1 |
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.
The same "2>&1".
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.
Verbose isn’t stream 2, I believe it’s 3, but redirection only worked when using all streams
| $VerbosePreference = "continue" | ||
| try { | ||
| $out = Write-Verbose $text *>&1 | ||
| $out | Should Match $text |
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.
The same about "BeExactly".
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.
Same as above, BOM gets added
| } | ||
|
|
||
| int columnsOnTheScreen = this.InnerCommand._lo.ColumnNumber; | ||
| if (columnsOnTheScreen == int.MaxValue) |
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.
Could you please add comments about the special cases?
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.
Yes, will add
| } | ||
| else | ||
| { | ||
| // NTRAID#Windows OS Bugs-1061752-2004/12/15-sburns should read a skin setting here... |
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.
Is the comment still actual?
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.
I believe the comment is saying that if we decide to support color themes (aka skins), this is one place to put the code for Write-Debug. Did a quick search for "read a skin" and found four places for Debug, Verbose, Warning, and ProgressPane.
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.
Thanks for clarify.
Closed.
| } | ||
|
|
||
| int columnsOnTheScreen = this.InnerCommand._lo.ColumnNumber; | ||
| // for tables, we want to make sure we don't end up adding padding to int.MaxValue and default to 120 columns instead |
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.
Can we rephrase it for easy reading?
And why 120? Can we use current windows width? Have we tests for this?
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.
Let me try to add a bit more detail to make it easier to understand. The code tries to use window width, but if it can't be read (like implicit remoting where there's no console window), we have to default to something. 120 was the original default without my changes.
lzybkr
left a comment
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.
I tried out these changes - it looks like a big improvement for Format-List and no change for Format-Table or Format-Wide and error records aren't wrapped, so that's great.
Maybe in some future work we can detect when the output is to a file and be smarter about Format-Table and Format-Wide.
|
Given that @iSazonov's comments have been addressed, I will merge this PR. |
don't use current console window width to add linebreaks to output (including debug, error, and verbose)
explicitly change errorrecord formatter to not add linebreaks
set width to int.maxvalue if not specified, in some special cases if width is int.maxvalue, set to console.windowwidth
so that you don't get int.maxvalue padding (like tables)
Related: #3813