Private/Split-LongString.ps1
|
Function Split-LongString { <# .SYNOPSIS Splits a long string into multiple lines based on the specified width and padding. .EXAMPLE Split-LongString -Text $LongString -Width 80 -Padding 4 #> [CmdletBinding()] param( [Parameter(Mandatory=$true, ValueFromPipeline=$True, Position = 0, HelpMessage = 'The long string to split into multiple lines.')] [string]$Text, [Parameter(Mandatory=$False, Position = 1, HelpMessage = 'The maximum width of each line.')] [int]$Width = [Console]::WindowWidth, [Parameter(Mandatory=$False, Position = 2, HelpMessage = 'The number of padding spaces to subtract from the width.')] [int]$Padding = 4 ) # helper to strip ANSI/escape sequences so length comparisons use printable characters function Remove-AnsiCodes { param( [string]$t ) If(-not $t) { return '' } $regex = "{0}\[[0-9;]*m" -f [ConsoleFX.Ansi]::Escape return $t -replace $regex, '' } function Get-PrintableLength { param( [string]$t ) return (Remove-AnsiCodes -t $t).Length } $Width = $Width - $Padding $Lines = @() # Split by newlines first to preserve line structure $InputLines = $Text -split "`n" ForEach($InputLine in $InputLines) { # If line is short enough, keep it as-is If((Get-PrintableLength $InputLine) -le $Width) { $Lines += $InputLine } Else { # Only wrap lines that are too long $Words = $InputLine -split '\s+' $Line = '' $LineLen = 0 ForEach($Word in $Words) { $wordLen = Get-PrintableLength $Word $spaceLen = if ($LineLen -gt 0) {1} else {0} if(($LineLen + $wordLen + $spaceLen) -le $Width) { if($LineLen -gt 0) { $Line += ' ' $LineLen += 1 } $Line += $Word $LineLen += $wordLen } Else { $Lines += $Line $Line = $Word $LineLen = $wordLen } } If($Line) { $Lines += $Line } } } Return $Lines } |