forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd-Border.ps1
More file actions
169 lines (146 loc) · 7.31 KB
/
Add-Border.ps1
File metadata and controls
169 lines (146 loc) · 7.31 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#add a border around a string of text
<#
originally published at
https://gist.github.com/jdhitsolutions/0bbd6b64c107d7da23e65359c4d0e25c
#>
Function Add-Border {
[CmdletBinding(DefaultParameterSetName = "single")]
Param(
# The string of text to process
[Parameter(Position = 0, Mandatory, ValueFromPipeline, ParameterSetName = 'single')]
[ValidateNotNullOrEmpty()]
[string]$Text,
[Parameter(Position = 0, Mandatory, ParameterSetName = 'block')]
[ValidateNotNullOrEmpty()]
[Alias("tb")]
[string[]]$TextBlock,
# The character to use for the border. It must be a single character.
[ValidateNotNullOrEmpty()]
[alias("border")]
[string]$Character = "*",
# add blank lines before and after text
[Switch]$InsertBlanks,
# insert X number of tabs
[int]$Tab = 0,
[Parameter(HelpMessage = "Enter an ANSI escape sequence to color the border characters." )]
[string]$ANSIBorder,
[Parameter(HelpMessage = "Enter an ANSI escape sequence to color the text." )]
[string]$ANSIText
)
Begin {
Write-Detail "Starting $($myinvocation.mycommand)" -Prefix begin | Write-Verbose
$tabs = "`t" * $tab
Write-Detail "Using a tab of $tab" -Prefix BEGIN | Write-Verbose
Write-Detail "Using border character $Character" -Prefix begin | Write-Verbose
$ansiClear = "$([char]0x1b)[0m"
if ($PSBoundParameters.ContainsKey("ANSIBorder")) {
Write-Detail "Using an ANSI border Color" -Prefix Begin | Write-Verbose
$Character = "{0}{1}{2}" -f $PSBoundParameters.ANSIBorder, $Character, $ansiClear
}
#define regex expressions to detect ANSI escapes. Need to subtract their
#length from the string if used. Issue #79
[regex]$ansiopen = "$([char]0x1b)\[\d+[\d;]+m"
[regex]$ansiend = "$([char]0x1b)\[0m"
} #begin
Process {
if ($pscmdlet.ParameterSetName -eq 'single') {
Write-Detail "Processing '$text'" -Prefix PROCESS | Write-Verbose
#get length of text
$adjust = 0
if ($ansiopen.IsMatch($text)) {
$adjust += ($ansiopen.matches($text) | Measure-Object length -sum).sum
$adjust += ($ansiend.matches($text) | Measure-Object length -sum).sum
Write-Detail "Adjusting text length by $adjust." -Prefix PROCESS | Write-Verbose
}
$len = $text.Length - $adjust
if ($PSBoundParameters.ContainsKey("ANSIText")) {
Write-Detail "Using an ANSIText color" -Prefix PROCESS | Write-Verbose
$text = "{0}{1}{2}" -f $PSBoundParameters.ANSIText, $text, $AnsiClear
}
}
else {
Write-Detail "Processing text block" -Prefix PROCESS | Write-Verbose
#test if text block is already using ANSI
if ($ansiopen.IsMatch($TextBlock)) {
Write-Detail "Text block contains ANSI sequences" -Prefix PROCESS | Write-Verbose
$txtarray | ForEach-Object -begin {$tempLen = @()} -process {
$adjust = 0
$adjust += ($ansiopen.matches($_) | Measure-Object length -sum).sum
$adjust += ($ansiend.matches($_) | Measure-Object length -sum).sum
Write-Detail "Length detected as $($_.length)" -Prefix PROCESS | Write-Verbose
Write-Detail "Adding adjustment $adjust" -Prefix PROCESS | Write-Verbose
$tempLen += $_.length - $adjust
}
$len = $tempLen | Sort-Object -Descending | Select-Object -first 1
}
elseif ($PSBoundparameters.ContainsKey("ANSIText")) {
Write-Detail "Using ANSIText for the block" -prefix PROCESS | Write-Verbose
$txtarray = $textblock.split("`n").Trim() | ForEach-Object {"{0}{1}{2}" -f $PSBoundParameters.ANSIText, $_, $AnsiClear}
$len = ($txtarray | Sort-Object -property length -Descending | Select-Object -first 1 -expandProperty length) - ($psboundparameters.ANSIText.length + 4)
}
else {
Write-Detail "Processing simple text block" -prefix PROCESS | Write-Verbose
$txtarray = $textblock.split("`n").Trim()
$len = $txtarray | Sort-Object -property length -Descending | Select-Object -first 1 -expandProperty length
}
Write-Detail "Added $($txtarray.count) text block elements" -Prefix PROCESS | Write-Verbose
}
Write-Detail "Using a length of $len" -Prefix PROCESS | Write-Verbose
#define a horizontal line
$hzline = $Character * ($len + 4)
if ($pscmdlet.ParameterSetName -eq 'single') {
Write-Detail "Defining Single body" -prefix PROCESS | Write-Verbose
$body = "$tabs$Character $text $Character"
}
else {
Write-Detail "Defining Textblock body" -prefix PROCESS | Write-Verbose
[string[]]$body = $null
foreach ($item in $txtarray) {
if ($item) {
Write-Detail "$item [$($item.length)]" -Prefix PROCESS | Write-Verbose
}
else {
Write-Detail "detected blank line" -Prefix PROCESS | Write-Verbose
}
if ($ansiopen.IsMatch($item)) {
$adjust = $len
$adjust += ($ansiopen.matches($item) | Measure-Object length -sum).sum
$adjust += ($ansiend.matches($item) | Measure-Object length -sum).sum
Write-Detail "Adjusting length to $adjust" -prefix PROCESS | Write-Verbose
$body += "$tabs$Character $(($item).PadRight($adjust)) $Character`r"
}
elseif ($PSBoundparameters.ContainsKey("ANSIText")) {
#adjust the padding length to take the ANSI value into account
$adjust = $len + ($psboundparameters.ANSIText.length + 4)
Write-Detail "Adjusting length to $adjust" -prefix PROCESS | Write-Verbose
$body += "$tabs$Character $(($item).PadRight($adjust)) $Character`r"
}
else {
$body += "$tabs$Character $(($item).PadRight($len)) $Character`r"
}
} #foreach item in txtarray
}
Write-Detail "Defining top border" -Prefix PROCESS | Write-Verbose
[string[]]$out = "`n$tabs$hzline"
$lines = $body.split("`n")
Write-Detail "Adding $($lines.count) lines" | Write-Verbose
if ($InsertBlanks) {
Write-Detail "Prepending blank line" -Prefix PROCESS | Write-Verbose
$out += "$tabs$character $((" ")*$len) $character"
}
foreach ($item in $lines ) {
$out += $item
}
if ($InsertBlanks) {
Write-Detail "Appending blank line" -Prefix PROCESS | Write-Verbose
$out += "$tabs$character $((" ")*$len) $character"
}
Write-Detail "Defining bottom border" -Prefix PROCESS | Write-Verbose
$out += "$tabs$hzline"
#write the result to the pipeline
$out
} #process
End {
Write-Detail "Ending $($myinvocation.mycommand)" -prefix END | Write-Verbose
} #end
} #close function