Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -211,32 +211,13 @@ private LineOutput InstantiateLineOutputInterface()
return null;

// compute the # of columns available
int computedWidth = 120;
int computedWidth = int.MaxValue;

if (_width != null)
{
// use the value from the command line
computedWidth = _width.Value;
}
else
{
// use the value we get from the console
try
{
// NOTE: we subtract 1 because we want to properly handle
// the following scenario:
// MSH>get-foo|out-file foo.txt
// MSH>get-content foo.txt
// in this case, if the computed width is (say) 80, get-content
// would cause a wrapping of the 80 column long raw strings.
// Hence we set the width to 79.
computedWidth = this.Host.UI.RawUI.BufferSize.Width - 1;
}
catch (HostException)
{
// non interactive host
}
}

// use the stream writer to create and initialize the Line Output writer
TextWriterLineOutput twlo = new TextWriterLineOutput(_sw, computedWidth, _suppressNewline);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,31 +94,13 @@ private LineOutput InstantiateLineOutputInterface()
_writer = new StreamingTextWriter(callback, Host.CurrentCulture);

// compute the # of columns available
int computedWidth = 120;
int computedWidth = int.MaxValue;

if (_width != null)
{
// use the value from the command line
computedWidth = _width.Value;
}
else
{
// use the value we get from the console
try
{
// NOTE: we subtract 1 because we want to properly handle
// the following scenario:
// MSH>get-foo|format-table|out-string
// in this case, if the computed width is (say) 80, get-content
// would cause a wrapping of the 80 column long raw strings.
// Hence we set the width to 79.
computedWidth = this.Host.UI.RawUI.BufferSize.Width - 1;
}
catch (HostException)
{
// non interactive host
}
}

// use it to create and initialize the Line Output writer
TextWriterLineOutput twlo = new TextWriterLineOutput(_writer, computedWidth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2222,7 +2222,7 @@ private void ExitDebugMode(DebuggerResumeAction resumeAction)
/// </summary>
private void WriteDebuggerMessage(string line)
{
this.ui.WriteWrappedLine(this.ui.DebugForegroundColor, this.ui.DebugBackgroundColor, line);
this.ui.WriteLine(this.ui.DebugForegroundColor, this.ui.DebugBackgroundColor, line);
}

#endregion debugger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1148,44 +1148,6 @@ internal void AddWord(string text, int startIndex, int endIndex,
}
}

/// <summary>
///
/// Writes text, wrapping a "word" boundaries when needed.
///
/// </summary>
/// <param name="fg">
///
/// Foreground color to output the text.
///
/// </param>
/// <param name="bg">
///
/// Background color to output the text.
///
/// </param>
/// <param name="text">
///
/// Text to be emitted.
///
/// </param>

internal void WriteWrappedLine(ConsoleColor fg, ConsoleColor bg, string text)
{
WriteLine(fg, bg, WrapToCurrentWindowWidth(text));
}



// unused for now.
//internal
//void
//WriteWrappedLine(string text)
//{
// WriteWrappedLine(RawUI.ForegroundColor, RawUI.BackgroundColor, text);
//}



internal string WrapToCurrentWindowWidth(string text)
{
StringBuilder sb = new StringBuilder();
Expand All @@ -1207,11 +1169,7 @@ internal string WrapToCurrentWindowWidth(string text)
return sb.ToString();
}



#endregion Word Wrapping


#endregion Word Wrapping

/// <summary>
///
Expand Down Expand Up @@ -1248,7 +1206,7 @@ public override void WriteDebugLine(string message)
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.

WriteWrappedLine(
WriteLine(
DebugForegroundColor,
DebugBackgroundColor,
StringUtil.Format(ConsoleHostUserInterfaceStrings.DebugFormatString, message));
Expand Down Expand Up @@ -1309,7 +1267,7 @@ public override void WriteVerboseLine(string message)
}
else
{
WriteWrappedLine(
WriteLine(
VerboseForegroundColor,
VerboseBackgroundColor,
StringUtil.Format(ConsoleHostUserInterfaceStrings.VerboseFormatString, message));
Expand Down Expand Up @@ -1352,7 +1310,7 @@ public override void WriteWarningLine(string message)
}
else
{
WriteWrappedLine(
WriteLine(
WarningForegroundColor,
WarningBackgroundColor,
StringUtil.Format(ConsoleHostUserInterfaceStrings.WarningFormatString, message));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ private void ProcessOutOfBandPayload(FormatEntryData fed)
{
ComplexWriter complexWriter = new ComplexWriter();

complexWriter.Initialize(_lo, _lo.ColumnNumber);
complexWriter.Initialize(_lo, int.MaxValue);
complexWriter.WriteObject(cve.formatValueList);

return;
Expand Down Expand Up @@ -921,6 +921,22 @@ internal override void Initialize()
}

int columnsOnTheScreen = this.InnerCommand._lo.ColumnNumber;
// Tables need to use spaces for padding to maintain table look even if console window is resized.
// For all other output, we use int.MaxValue if the user didn't explicitly specify a width.
// If we detect that int.MaxValue is used, first we try to get the current console window width.
// However, if we can't read that (for example, implicit remoting has no console window), we default
// to something reasonable: 120 columns.
if (columnsOnTheScreen == int.MaxValue)
{
try
{
columnsOnTheScreen = Console.WindowWidth;
}
catch
{
columnsOnTheScreen = 120;
}
}

int columns = this.CurrentTableHeaderInfo.tableColumnInfoList.Count;
if (columns == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,6 @@ private static IEnumerable<FormatViewDefinition> ViewsOf_System_Management_Autom
}

$indent = 4
$width = $host.UI.RawUI.BufferSize.Width - $indent - 2

$errorCategoryMsg = & { Set-StrictMode -Version 1; $_.ErrorCategory_Message }
if ($null -ne $errorCategoryMsg)
Expand All @@ -800,19 +799,16 @@ private static IEnumerable<FormatViewDefinition> ViewsOf_System_Management_Autom
{
$indentString = ""+ CategoryInfo : "" + $_.CategoryInfo
}
$posmsg += ""`n""
foreach($line in @($indentString -split ""(.{$width})"")) { if($line) { $posmsg += ("" "" * $indent + $line) } }
$posmsg += ""`n"" + $indentString

$indentString = ""+ FullyQualifiedErrorId : "" + $_.FullyQualifiedErrorId
$posmsg += ""`n""
foreach($line in @($indentString -split ""(.{$width})"")) { if($line) { $posmsg += ("" "" * $indent + $line) } }
$posmsg += ""`n"" + $indentString

$originInfo = & { Set-StrictMode -Version 1; $_.OriginInfo }
if (($null -ne $originInfo) -and ($null -ne $originInfo.PSComputerName))
{
$indentString = ""+ PSComputerName : "" + $originInfo.PSComputerName
$posmsg += ""`n""
foreach($line in @($indentString -split ""(.{$width})"")) { if($line) { $posmsg += ("" "" * $indent + $line) } }
$posmsg += ""`n"" + $indentString
}

if ($ErrorView -eq ""CategoryView"") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ internal void Initialize(int leftMarginIndent, int screenColumns, int[] columnWi
//Console.WriteLine(" 1 2 3 4 5 6 7");
//Console.WriteLine("01234567890123456789012345678901234567890123456789012345678901234567890123456789");

if (screenColumns == int.MaxValue)
{
try
{
screenColumns = System.Console.WindowWidth;
}
catch
{
screenColumns = 120;
}
}

if (leftMarginIndent < 0)
{
leftMarginIndent = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,25 @@ Describe "Native streams behavior with PowerShell" -Tags 'CI' {
It 'preserves error stream as is with Out-String' {
($out | Out-String).Replace("`r", '') | Should Be "foo`n`nbar`n`nbazmiddlefoo`n`nbar`n`nbaz`n"
}

It 'does not get truncated or split when redirected' {
$longtext = "0123456789"
while ($longtext.Length -lt [console]::WindowWidth) {
$longtext += $longtext
}
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 BeExactly $longtext
}
}
}

Describe 'piping powershell objects to finished native executable' -Tags 'CI' {
It 'doesn''t throw any exceptions, when we are piping to the closed executable' {
1..3 | ForEach-Object {
Start-Sleep -Milliseconds 100
# yeild some multi-line formatted object
# yield some multi-line formatted object
@{'a' = 'b'}
} | testexe -echoargs | Should Be $null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Describe "Write-Debug tests" -Tags "CI" {
It "Should not have added line breaks" {
$text = "0123456789"
while ($text.Length -lt [Console]::WindowWidth) {
$text += $text
}
$origDebugPref = $DebugPreference
$DebugPreference = "Continue"
try {
$out = Write-Debug $text 5>&1
$out | Should BeExactly $text
}
finally {
$DebugPreference = $origDebugPref
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Describe "Write-Error DRT Unit Tests" -Tags "CI" {
Describe "Write-Error Tests" -Tags "CI" {
It "Should be works with command: write-error myerrortext" {
$e = Write-Error myerrortext 2>&1
$e | Should BeOfType 'System.Management.Automation.ErrorRecord'
Expand Down Expand Up @@ -74,40 +74,49 @@ Describe "Write-Error DRT Unit Tests" -Tags "CI" {
$e.CategoryInfo.TargetType | Should Be 'fooTargetType'
$e.CategoryInfo.GetMessage() | Should Be 'NotSpecified: (fooTargetName:fooTargetType) [fooAct], fooReason'
}
}

Describe "Write-Error" -Tags "CI" {
It "Should be able to throw" {
Write-Error "test throw" -ErrorAction SilentlyContinue | Should Throw
Write-Error "test throw" -ErrorAction SilentlyContinue | Should Throw
}

It "Should throw a non-terminating error" {
Write-Error "test throw" -ErrorAction SilentlyContinue
Write-Error "test throw" -ErrorAction SilentlyContinue

1 + 1 | Should Be 2
1 + 1 | Should Be 2
}

It "Should trip an exception using the exception switch" {
$var = 0
try
{
Write-Error -Exception -Message "test throw"
}
catch [System.Exception]
{

$var++
}
finally
{
$var | Should Be 1
}
$var = 0
try
{
Write-Error -Exception -Message "test throw"
}
catch [System.Exception]
{

$var++
}
finally
{
$var | Should Be 1
}
}

It "Should output the error message to the `$error automatic variable" {
$theError = "Error: Too many input values."
write-error -message $theError -category InvalidArgument -ErrorAction SilentlyContinue
$theError = "Error: Too many input values."
write-error -message $theError -category InvalidArgument -ErrorAction SilentlyContinue

$error[0]| Should Be $theError
}

$error[0]| Should Be $theError
It "ErrorRecord should not be truncated" {
$longtext = "0123456789"
while ($longtext.Length -lt [console]::WindowWidth) {
$longtext += $longtext
}
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.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,20 @@ Describe "Write-Verbose" -Tags "CI" {

$(Write-Verbose -Message "test" -Verbose:$true) 4>&1 | Should Be "test"
}

It "Should not have added line breaks" {
$text = "0123456789"
while ($text.Length -lt [Console]::WindowWidth) {
$text += $text
}
$origVerbosePref = $VerbosePreference
$VerbosePreference = "continue"
try {
$out = Write-Verbose $text 4>&1
$out | Should BeExactly $text
}
finally {
$VerbosePreference = $origVerbosePref
}
}
}