Skip to content

Conversation

@SteveL-MSFT
Copy link
Member

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

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)
wrap calls to get WindowWidth and default to 120 columns if it fails
@SteveL-MSFT SteveL-MSFT added the Breaking-Change breaking change that may affect users label Oct 22, 2017
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
Copy link
Collaborator

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.

Copy link
Member Author

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

Copy link
Collaborator

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.

Copy link
Member Author

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
Copy link
Collaborator

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"

Copy link
Member Author

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

Copy link
Collaborator

@iSazonov iSazonov Oct 23, 2017

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: qwerty

Copy link
Member Author

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
Copy link
Collaborator

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"?

Copy link
Member Author

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
Copy link
Collaborator

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.

Copy link
Member Author

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
Copy link
Collaborator

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.

Copy link
Member Author

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

Copy link
Collaborator

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
Copy link
Collaborator

Choose a reason for hiding this comment

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

The same "2>&1".

Copy link
Member Author

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
Copy link
Collaborator

Choose a reason for hiding this comment

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

The same about "BeExactly".

Copy link
Member Author

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)
Copy link
Collaborator

@iSazonov iSazonov Oct 23, 2017

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, will add

address iSazonov's comments
}
else
{
// NTRAID#Windows OS Bugs-1061752-2004/12/15-sburns should read a skin setting here...
Copy link
Collaborator

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?

Copy link
Member Author

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.

Copy link
Collaborator

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
Copy link
Collaborator

@iSazonov iSazonov Oct 24, 2017

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?

Copy link
Member Author

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.

expanded on comment for computing width for tables
Copy link
Contributor

@lzybkr lzybkr left a 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.

@daxian-dbw
Copy link
Member

Given that @iSazonov's comments have been addressed, I will merge this PR.

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

Labels

Breaking-Change breaking change that may affect users

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants