From 4e3407c0daa6da5ab3075bbb64230962f1ac8c13 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 5 Feb 2020 11:43:10 -0800 Subject: [PATCH] Fix ConciseView to handle case where there isn't a console to obtain the width --- .../PowerShellCore_format_ps1xml.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 67d6da5d551..3fdff3e13c4 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -1151,15 +1151,23 @@ function Get-ConciseViewPositionMessage { # replace newlines in message so it lines up correct $message = $message.Replace($newline, ' ').Replace(""`t"", ' ') - if ([Console]::WindowWidth -gt 0 -and ($message.Length - $prefixVTLength) -gt [Console]::WindowWidth) { + try { + $windowWidth = [Console]::WindowWidth + } + catch { + # fails if there is no console + $windowWidth = 120 + } + + if ($windowWidth -gt 0 -and ($message.Length - $prefixVTLength) -gt $windowWidth) { $sb = [Text.StringBuilder]::new() - $substring = Get-TruncatedString -string $message -length ([Console]::WindowWidth + $prefixVTLength) + $substring = Get-TruncatedString -string $message -length ($windowWidth + $prefixVTLength) $null = $sb.Append($substring) $remainingMessage = $message.Substring($substring.Length).Trim() $null = $sb.Append($newline) - while (($remainingMessage.Length + $prefixLength) -gt [Console]::WindowWidth) { + while (($remainingMessage.Length + $prefixLength) -gt $windowWidth) { $subMessage = $prefix + $remainingMessage - $substring = Get-TruncatedString -string $subMessage -length ([Console]::WindowWidth + $prefixVtLength) + $substring = Get-TruncatedString -string $subMessage -length ($windowWidth + $prefixVtLength) $null = $sb.Append($substring) $null = $sb.Append($newline) $remainingMessage = $remainingMessage.Substring($substring.Length - $prefix.Length).Trim()