@@ -20,31 +20,42 @@ filter WrapString {
20
20
[Parameter (Position = 0 )]
21
21
[int ]$Width = ($Host.UI.RawUI.BufferSize.Width ),
22
22
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.
25
25
[string ]$IndentPadding = ([string ]::Empty),
26
26
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
+
27
34
# If set, colors to use for alternating lines
28
35
[string []]$Colors = @ (' ' ),
29
36
30
37
# If set, will output empty lines for each original new line
31
38
[switch ]$EmphasizeOriginalNewlines
32
39
)
33
40
begin {
41
+ $FirstLine = $true
34
42
$color = 0 ;
35
43
Write-Debug " Colors: $ ( $Colors -replace " `e (.+)" , " `e`$ 1`` e`$ 1" ) "
36
44
# $wrappableChars = [char[]]" ,.?!:;-`n`r`t"
37
45
# $maxLength = $width - $IndentPadding.Length -1
38
46
$wrapper = [Regex ]::new(" ((?:$AnsiPattern )*[^-=,.?!:;\s\r\n\t\\\/\|]+(?:$AnsiPattern )*)" , " Compiled" )
39
47
$output = [System.Text.StringBuilder ]::new()
40
- $buffer = [System.Text.StringBuilder ]::new($Colors [ $color ] )
48
+ $buffer = [System.Text.StringBuilder ]::new()
41
49
$lineLength = 0
42
50
if ($Width -lt $IndentPadding.Length ) {
43
51
Write-Warning " Width $Width is less than IndentPadding length $ ( $IndentPadding.Length ) . Setting Width to BufferWidth ($ ( $Host.UI.RawUI.BufferSize.Width ) )"
44
52
}
45
53
}
46
54
process {
47
55
foreach ($line in $InputObject -split " (\r?\n)" ) {
56
+ if ($FirstLine -and $PSBoundParameters.ContainsKey (' FirstLineIndent' )) {
57
+ $IndentPadding , $FirstLineIndent = $FirstLineIndent , $IndentPadding
58
+ }
48
59
# Don't bother trying to split empty lines
49
60
if ([String ]::IsNullOrWhiteSpace($AnsiRegex.Replace ($line , ' ' ))) {
50
61
Write-Debug " Empty String ($ ( $line.Length ) )"
@@ -64,15 +75,24 @@ filter WrapString {
64
75
} else {
65
76
Write-Verbose " Output $ ( $lineLength - $slice.Length ) "
66
77
Write-Verbose " + $ ( $slice.Length ) = $ ( $slice.Length ) "
67
- $color = ($color + 1 ) % $Colors.Length
68
78
# $null = $output.Append($buffer.ToString())
69
- $null = $buffer.Append ($newline ).Append($slice.Text )
79
+ $null = $buffer.Append ($newline ).Append($WrappedIndent ).Append( $ slice.Text )
70
80
$lineLength = $IndentPadding.Length + $slice.Length
71
81
}
72
82
}
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)
75
93
$lineLength = $IndentPadding.Length
94
+ $FirstLine = $false
95
+ $IndentPadding = $FirstLineIndent
76
96
}
77
97
$output.ToString ()
78
98
}
0 commit comments