forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshow-ansi.ps1
More file actions
215 lines (188 loc) · 6.22 KB
/
show-ansi.ps1
File metadata and controls
215 lines (188 loc) · 6.22 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
Function Show-ANSISequence {
[cmdletbinding(DefaultParameterSetName = "basic")]
[outputtype([system.string])]
Param(
[Parameter(ParameterSetName = "basic", HelpMessage = "Display basic ANSI escape sequences. This is the default setting.")]
[switch]$Basic,
[Parameter(ParameterSetName = "foreback", HelpMessage = "Display foreground ANSI escape sequences")]
[switch]$Foreground,
[Parameter(ParameterSetName = "foreback", HelpMessage = "Display background ANSI escape sequences")]
[switch]$Background,
[Parameter(ParameterSetName = "foreback")]
[ValidateSet("All", "Simple", "8Bit")]
[string]$Type = "All",
[Parameter(ParameterSetName = "RGB", HelpMessage = "Specify an array of RGB values between 0 and 255")]
[int[]]$RGB,
[Parameter(HelpMessage = "Show the value as an unformatted string")]
[switch]$AsString
)
Write-Verbose "Starting $($myinvocation.MyCommand)"
Write-Debug "Using parameter set $($PSCmdlet.ParameterSetName)"
Write-Debug "Bound parameters"
$PSBoundParameters | Out-String | Write-Debug
#default to Basic even if the user doesn't specify the -Basic parameter
if ($PSCmdlet.parametersetname -eq 'basic') {
$Basic = $True
}
elseif ($PSCmdlet.parametersetname -eq 'foreback') {
if ( (-Not ($PSBoundParameters.ContainsKey('foreground'))) -OR (-Not ($PSBoundParameters.ContainsKey('background')))) {
#default to foreground Issue # 110
Write-Debug "Setting Foreground as default"
$Foreground = $True
}
}
# a private function to display results in columns on the screen
Function display {
Param([object]$all, [int]$Max)
$c = 1
[string]$row = ""
for ($i = 0; $i -le $all.count; $i++) {
if ($c -gt $max) {
$row
$c = 1
$row = ""
#need to reset $i otherwise the entry gets omitted
# Issue #125
$i--
}
else {
$row += "$($all[$i]) `t"
$c++
}
}
} #display function
if ($IsCoreCLR) {
Write-Debug "CoreCLR"
$esc = "`e"
$escText = '`e'
$max = 3
}
else {
Write-Debug "Desktop"
$esc = $([char]27)
$escText = '$([char]27)'
$max = 2
}
#region basic
if ($basic) {
Write-Debug "Get basic settings"
Add-Border "Basic Sequences" -ANSIText "$esc[1m" | Write-Host
$basichash = @{
1 = "Bold"
2 = "Faint"
3 = "Italic"
4 = "Underline"
5 = "SlowBlink"
6 = "RapidBlink"
7 = "Reverse"
9 = "CrossedOut"
}
$basichash.GetEnumerator() | ForEach-Object {
$sequence = "$esctext[$($_.name)m$($_.value)$esctext[0m"
if ($AsString) {
$sequence
}
else {
"$esc[$($_.name)m$sequence$esc[0m"
}
}
}
#endregion
#region foreground
If ($Foreground) {
Write-Debug "Getting foreground"
if ($Type -match "All|Simple") {
Add-Border "Foreground" -ANSIText "$esc[93m" | Write-Host
$n = [System.Collections.Generic.list[int]]::new()
30..37 | ForEach-Object {$n.Add($_)}
90..97 | ForEach-Object {$n.Add($_)}
$all = $n | ForEach-Object {
$sequence = "$esctext[$($_)mHello$esctext[0m"
if ($AsString) {
$sequence
}
else {
"$esc[$($_)m$sequence$esc[0m"
}
}
display -all $all -Max $max
Write-Host "`n"
}
if ($Type -match "All|8bit") {
Add-Border "8-Bit Foreground" -ANSIText "$esc[38;5;10m" | Write-Host
$all = 1..255 | ForEach-Object {
$sequence = "$esctext[38;5;$($_)mHello$esctext[0m"
if ($AsString) {
$sequence
}
else {
"$esc[38;5;$($_)m$sequence$esc[0m"
}
}
display -all $all -Max $max
}
}
#endregion
#region background
If ($Background) {
Write-Debug "Getting background"
if ($Type -match "All|Simple") {
Add-Border "Background" -ANSIText "$esc[46m" | Write-Host
$n = 40..47
$n += 100..107
$all = $n | ForEach-Object {
$sequence = "$esctext[$($_)mHello$esctext[0m"
if ($AsString) {
$sequence
}
else {
"$esc[1;$($_)m$sequence$esc[0m"
}
}
display -all $all -Max $max
Write-Host "`n"
}
if ($Type -match "All|8bit") {
Add-Border "8-Bit Background" -ANSIText "$esc[7;38;5;10m" | Write-Host
$all = 1..255 | ForEach-Object {
$sequence = "$esctext[48;5;$($_)mHello$esctext[0m"
if ($AsString) {
$sequence
}
else {
"$esc[1;48;5;$($_)m$sequence$esc[0m"
}
}
display -all $all -Max $max
}
}
#endregion
#region RGB
if ($RGB) {
Write-Debug "Using RBG values"
if ($rgb.count -ne 3) {
Write-Warning "Wrong number of arguments. Specify RED,GREEN and BLUE values"
return
}
$test = $rgb | Where-Object { $_ -gt 255 }
if ($test.count -gt 0) {
Write-Warning "Detected a value above 255"
return $rgb
}
$r = $rgb[0]
$g = $rgb[1]
$b = $rgb[2]
Write-Host "`r"
$sequence = "$esctext[38;2;$r;$g;$($b)m256 Color (R:$r)(G:$g)(B:$b)$esctext[0m"
if ($AsString) {
$sequence
}
else {
"$esc[38;2;$r;$g;$($b)m$sequence$esc[0m"
}
}
#endregionm
#insert a blank line to set off the results
Write-Host "`r"
Write-Verbose "Ending $($myinvocation.MyCommand)"
}