@@ -20,31 +20,42 @@ filter WrapString {
2020 [Parameter (Position = 0 )]
2121 [int ]$Width = ($Host.UI.RawUI.BufferSize.Width ),
2222
23- # The padding to add to the front of each line to cause indenting. Defaults to empty string.
24- [ Parameter ( Position = 1 )]
23+ # The padding for each line defaults to an empty string.
24+ # If set, whitespace on the front of each line is replaced with this string.
2525 [string ]$IndentPadding = ([string ]::Empty),
2626
27+ # If set, this will be used only for the first line (defaults to IndentPadding)
28+ [string ]$FirstLineIndent = $IndentPadding ,
29+
30+ # If set, wrapped lines use this instead of IndentPadding to create a hanging indent
31+ [string ]$WrappedIndent = $IndentPadding ,
32+
33+
2734 # If set, colors to use for alternating lines
2835 [string []]$Colors = @ (' ' ),
2936
3037 # If set, will output empty lines for each original new line
3138 [switch ]$EmphasizeOriginalNewlines
3239 )
3340 begin {
41+ $FirstLine = $true
3442 $color = 0 ;
3543 Write-Debug " Colors: $ ( $Colors -replace " `e (.+)" , " `e`$ 1`` e`$ 1" ) "
3644 # $wrappableChars = [char[]]" ,.?!:;-`n`r`t"
3745 # $maxLength = $width - $IndentPadding.Length -1
3846 $wrapper = [Regex ]::new(" ((?:$AnsiPattern )*[^-=,.?!:;\s\r\n\t\\\/\|]+(?:$AnsiPattern )*)" , " Compiled" )
3947 $output = [System.Text.StringBuilder ]::new()
40- $buffer = [System.Text.StringBuilder ]::new($Colors [ $color ] )
48+ $buffer = [System.Text.StringBuilder ]::new()
4149 $lineLength = 0
4250 if ($Width -lt $IndentPadding.Length ) {
4351 Write-Warning " Width $Width is less than IndentPadding length $ ( $IndentPadding.Length ) . Setting Width to BufferWidth ($ ( $Host.UI.RawUI.BufferSize.Width ) )"
4452 }
4553 }
4654 process {
4755 foreach ($line in $InputObject -split " (\r?\n)" ) {
56+ if ($FirstLine -and $PSBoundParameters.ContainsKey (' FirstLineIndent' )) {
57+ $IndentPadding , $FirstLineIndent = $FirstLineIndent , $IndentPadding
58+ }
4859 # Don't bother trying to split empty lines
4960 if ([String ]::IsNullOrWhiteSpace($AnsiRegex.Replace ($line , ' ' ))) {
5061 Write-Debug " Empty String ($ ( $line.Length ) )"
@@ -64,15 +75,24 @@ filter WrapString {
6475 } else {
6576 Write-Verbose " Output $ ( $lineLength - $slice.Length ) "
6677 Write-Verbose " + $ ( $slice.Length ) = $ ( $slice.Length ) "
67- $color = ($color + 1 ) % $Colors.Length
6878 # $null = $output.Append($buffer.ToString())
69- $null = $buffer.Append ($newline ).Append($slice.Text )
79+ $null = $buffer.Append ($newline ).Append($WrappedIndent ).Append( $ slice.Text )
7080 $lineLength = $IndentPadding.Length + $slice.Length
7181 }
7282 }
73- $null = $output.Append ($buffer.ToString ())
74- $null = $buffer.Clear ().Append($newline ).Append($Colors [$color ]).Append($IndentPadding )
83+ if (! $FirstLine ) {
84+ $null = $output.Append ($newline )
85+ }
86+ if ($PSBoundParameters.ContainsKey (" IndentPadding" )) {
87+ $null = $output.Append ($Colors [$color ] + $IndentPadding + $buffer.ToString ().TrimStart())
88+ } else {
89+ $null = $output.Append ($Colors [$color ] + $buffer.ToString ())
90+ }
91+ $color = ($color + 1 ) % $Colors.Length
92+ $null = $buffer.Clear () # .Append($Colors[$color]).Append($IndentPadding)
7593 $lineLength = $IndentPadding.Length
94+ $FirstLine = $false
95+ $IndentPadding = $FirstLineIndent
7696 }
7797 $output.ToString ()
7898 }
0 commit comments