FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix ConciseView where error message is wider than window width and do… · PowerShell/PowerShell@34f9b43 · GitHub

Commit 34f9b43

Browse files
authored
Fix ConciseView where error message is wider than window width and doesn't have whitespace (#11880)
1 parent 8e68397 commit 34f9b43

2 files changed

Lines changed: 27 additions & 9 deletions

File tree

‎src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,12 +1151,10 @@ function Get-ConciseViewPositionMessage {
11511151
11521152
# replace newlines in message so it lines up correct
11531153
$message = $message.Replace($newline, ' ').Replace(""`t"", ' ')
1154-
try {
1155-
$windowWidth = [Console]::WindowWidth
1156-
}
1157-
catch {
1158-
# fails if there is no console
1159-
$windowWidth = 120
1154+
1155+
$windowWidth = 120
1156+
if ($Host.UI.RawUI -ne $null) {
1157+
$windowWidth = $Host.UI.RawUI.WindowSize.Width
11601158
}
11611159
11621160
if ($windowWidth -gt 0 -and ($message.Length - $prefixVTLength) -gt $windowWidth) {
@@ -1168,9 +1166,17 @@ function Get-ConciseViewPositionMessage {
11681166
while (($remainingMessage.Length + $prefixLength) -gt $windowWidth) {
11691167
$subMessage = $prefix + $remainingMessage
11701168
$substring = Get-TruncatedString -string $subMessage -length ($windowWidth + $prefixVtLength)
1171-
$null = $sb.Append($substring)
1172-
$null = $sb.Append($newline)
1173-
$remainingMessage = $remainingMessage.Substring($substring.Length - $prefix.Length).Trim()
1169+
1170+
if ($substring.Length - $prefix.Length -gt 0)
1171+
{
1172+
$null = $sb.Append($substring)
1173+
$null = $sb.Append($newline)
1174+
$remainingMessage = $remainingMessage.Substring($substring.Length - $prefix.Length).Trim()
1175+
}
1176+
else
1177+
{
1178+
break
1179+
}
11741180
}
11751181
$null = $sb.Append($prefix + $remainingMessage.Trim())
11761182
$message = $sb.ToString()

‎test/powershell/engine/Formatting/ErrorView.Tests.ps1‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ Describe 'Tests for $ErrorView' -Tag CI {
8787
# validate line number is shown
8888
$e | Should -BeLike '* 2 *'
8989
}
90+
91+
It "Long exception message gets rendered" {
92+
93+
$msg = "1234567890"
94+
while ($msg.Length -le $Host.UI.RawUI.WindowSize.Width)
95+
{
96+
$msg += $msg
97+
}
98+
99+
$e = { throw "$msg" } | Should -Throw $msg -PassThru | Out-String
100+
$e | Should -BeLike "*$msg*"
101+
}
90102
}
91103

92104
Context 'NormalView tests' {

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL