/
ConvertTo-WrappedLines.ps1
102 lines (90 loc) · 3.55 KB
/
ConvertTo-WrappedLines.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
function ConvertTo-WrappedLines {
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string]$text
,
[Parameter(Mandatory = $false)]
[int]$width= 80
,
[Parameter(Mandatory = $false)]
[switch]$asString
,
[Parameter(Mandatory = $false)]
[switch]$useCrLf
)
begin {
Write-Verbose -Message ((Get-ResStr 'STARTING_FUNCTION') -f $myInvocation.Mycommand)
New-Variable -Name 'column' -Scope 'Private' -Value (0)
New-Variable -Name 'line' -Scope 'Private' -Value ('')
New-Variable -Name 'paragraph' -Scope 'Private' -Value ('')
New-Variable -Name 'paragraphs' -Scope 'Private' -Value ($null)
New-Variable -Name 'rawWords' -Scope 'Private' -Value ($null)
New-Variable -Name 'word' -Scope 'Private' -Value ('')
New-Variable -Name 'words' -Scope 'Private' -Value ($null)
New-Variable -Name 'result' -Scope 'Private' -Value ($null)
$initialVariables = Get-CurrentVariables -Debug:$DebugPreference
}
process {
# standard arrays dont allow 'add' command
$result = [System.Collections.ArrayList]@()
# Normalize string delimiter to unix style
$text = $text.replace("`r`n", "`n")
# Delete all unnecessary empty lines and characters at the end of the last paragraph
$text = $text.TrimEnd()
# Each line break is a hard return, i.e. a paragraph
[string[]]$paragraphs = $text -split "\n+"
foreach ($paragraph in $paragraphs ) {
[string[]]$rawWords = $paragraph -split "\s+"
# Create a word list with words and make sure that no word is longer than 'width'
$words = [System.Collections.ArrayList]@()
foreach ($word in $rawWords ) {
if ($word.Length -le $width) {
$words.Add($word) | Out-Null
} else {
# if one word is longer then the width, split it
[string[]]$wordparts = $word -split "(.{$width})" -ne ''
foreach ($word in $wordparts) {
$words.Add($word) | Out-Null
}
}
}
# Put as many words as possible in one line, but never longer than 'width'
[int]$column = 0
[string]$line = ""
foreach ($word in $words ) {
$column += $word.Length + 1
if ($column -gt $width ) {
$result.add($line.trim()) | Out-Null
$column = $word.Length + 1
$line = ""
}
$line = "$line$($word) "
}
$result.add($line.trim()) | Out-Null
}
if ($asString) {
if ($useCrLf) {
[string]$result = $result.trim() -join "`r`n"
} else {
[string]$result = $result.trim() -join "`n"
}
} else {
$result = ,$result
try {
if ($result[0].Count -gt 1) {
if ($result[0][0] -eq "") {
$result[0].RemoveAt(0)
}
}
}
catch {
}
}
}
end {
Get-CurrentVariables -InitialVariables $initialVariables -Debug:$DebugPreference
Return $result
}
# Test: ConvertTo-WrappedLines -text 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed porttitor lacus sed augue commodo dapibus. Suspendisse potenti.' -width 40
}