-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWriteAscii.psm1
412 lines (306 loc) · 14.8 KB
/
WriteAscii.psm1
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<#
.SYNOPSIS
Svendsen Tech's PowerShell ASCII art module creates ASCII art characters
from a subset of common letters, numbers and punctuation characters.
You can add new characters by editing the XML (for developers).
MIT license.
Copyright (c) 2012-present, Joakim Borger Svendsen, Svendsen Tech.
All rights reserved.
.DESCRIPTION
This script reads characters from an XML file that's expected to have the name
"letters.xml", be encoded in UTF-8 and to be in the module's working directory.
It was written to be used in conjunction with a modified version of
PowerBot (http://poshcode.org/2510), a simple IRC bot framework written
using SmartIrc4Net; that's why it can prepend an apostrophe - because somewhere
along the way the leading spaces get lost before it hits the IRC channel.
Currently the XML only contains lowercase letters, mostly because PowerShell/
Windows is case-insensitive by default, which isn't an advantage here.
Example:
PS C:\> Import-Module WriteAscii
PS C:\> Write-Ascii "ASCII!"
_ _ _
__ _ ___ ___ (_)(_)| |
/ _` |/ __| / __|| || || |
| (_| |\__ \| (__ | || ||_|
\__,_||___/ \___||_||_|(_)
PS C:\>
.PARAMETER InputText
String(s) to convert to ASCII.
.PARAMETER PrependChar
Optional. Makes the script prepend an apostrophe.
.PARAMETER Compression
Optional. Compress to five lines when possible, even when it causes incorrect
alignment of the letters g, y, p and q (and "¤").
.PARAMETER ForegroundColor
Optional. Console only. Changes text foreground color.
.PARAMETER BackgroundColor
Optional. Console only. Changes text background color.
#>
function Write-Ascii {
# Wrapping the script in a function to make it a module
[CmdletBinding()]
param(
[Parameter(
ValueFromPipeline = $True,
Mandatory = $True)]
[Alias('InputText')]
[String[]] $InputObject,
[Switch] $PrependChar,
[Alias('Compression')] [Switch] $Compress,
[ValidateSet("Black", "Blue", "Cyan", "DarkBlue", "DarkCyan", "DarkGray",
"DarkGreen", "DarkMagenta", "DarkRed", "DarkYellow", "Default", "Gray", "Green",
"Magenta", "Red", "Rainbow", "White", "Yellow")]
[String] $ForegroundColor = 'Default',
[ValidateSet("Black", "Blue", "Cyan", "DarkBlue", "DarkCyan", "DarkGray",
"DarkGreen", "DarkMagenta", "DarkRed", "DarkYellow", "Default", "Gray", "Green",
"Magenta", "Red", "Rainbow", "White", "Yellow")]
[String] $BackgroundColor = 'Default'
#[int] $MaxChars = '25'
)
begin {
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# Algorithm from hell... This was painful. I hope there's a better way.
function Get-Ascii {
param([String] $Text)
$LetterArray = [Char[]] $Text.ToLower()
#Write-Host -fore green $LetterArray
# Find the letter with the most lines.
$MaxLines = 0
$LetterArray | ForEach-Object {
if ($Letters.([String] $_).Lines -gt $MaxLines ) {
$MaxLines = $Letters.([String] $_).Lines
}
}
# Now this sure was a simple way of making sure all letter align tidily without changing a lot of code!
if (-not $Compress) { $MaxLines = 6 }
$LetterWidthArray = $LetterArray | ForEach-Object {
$Letter = [String] $_
$Letters.$Letter.Width
}
$LetterLinesArray = $LetterArray | ForEach-Object {
$Letter = [String] $_
$Letters.$Letter.Lines
}
#$LetterLinesArray
$Lines = @{
'1' = ''
'2' = ''
'3' = ''
'4' = ''
'5' = ''
'6' = ''
}
#$LineLengths = @(0, 0, 0, 0, 0, 0)
# Debug
#Write-Host "MaxLines: $Maxlines"
$LetterPos = 0
foreach ($Letter in $LetterArray) {
# We need to work with strings for indexing the hash by letter
$Letter = [String] $Letter
# Each ASCII letter can be from 4 to 6 lines.
# If the letter has the maximum of 6 lines, populate hash with all lines.
if ($LetterLinesArray[$LetterPos] -eq 6) {
#Write-Host "Six letter letter"
foreach ($Num in 1..6) {
$LineFragment = [String](($Letters.$Letter.ASCII).Split("`n"))[$Num-1]
if ($LineFragment.Length -lt $Letters.$Letter.Width) {
$LineFragment += ' ' * ($Letters.$Letter.Width - $LineFragment.Length)
}
$StringNum = [String] $Num
$Lines.$StringNum += $LineFragment
}
}
# Add padding for line 1 for letters with 5 lines and populate lines 2-6.
## Changed to top-adjust 5-line letters if there are 6 total.
## Added XML properties for letter alignment. Most are "default", which is top-aligned.
## Also added script logic to handle it (2012-12-29): <fixation>bottom</fixation>
elseif ($LetterLinesArray[$LetterPos] -eq 5) {
if ($MaxLines -lt 6 -or $Letters.$Letter.fixation -eq 'bottom') {
$Padding = ' ' * $LetterWidthArray[$LetterPos]
$Lines.'1' += $Padding
foreach ($Num in 2..6) {
$LineFragment = [String](($Letters.$Letter.ASCII).Split("`n"))[$Num-2]
if ($LineFragment.Length -lt $Letters.$Letter.Width) {
$LineFragment += ' ' * ($Letters.$Letter.Width - $LineFragment.Length)
}
$StringNum = [String] $Num
$Lines.$StringNum += $LineFragment
}
}
else {
$Padding = ' ' * $LetterWidthArray[$LetterPos]
$Lines.'6' += $Padding
foreach ($Num in 1..5) {
$StringNum = [String] $Num
$LineFragment = [String](($Letters.$Letter.ASCII).Split("`n"))[$Num-1]
if ($LineFragment.Length -lt $Letters.$Letter.Width) {
$LineFragment += ' ' * ($Letters.$Letter.Width - $LineFragment.Length)
}
$Lines.$StringNum += $LineFragment
}
}
}
# Here we deal with letters with four lines.
# Dynamic algorithm that places four-line letters on the bottom line if there are
# 4 or 5 lines only in the letter with the most lines.
else {
# Default to putting the 4-liners at line 3-6
$StartRange, $EndRange, $IndexSubtract = 3, 6, 3
$Padding = ' ' * $LetterWidthArray[$LetterPos]
# If there are 4 or 5 lines...
if ($MaxLines -lt 6) {
$Lines.'2' += $Padding
}
# There are 6 lines maximum, put 4-line letters in the middle.
else {
$Lines.'1' += $Padding
$Lines.'6' += $Padding
$StartRange, $EndRange, $IndexSubtract = 2, 5, 2
}
# There will always be at least four lines. Populate lines 2-5 or 3-6 in the hash.
foreach ($Num in $StartRange..$EndRange) {
$StringNum = [String] $Num
$LineFragment = [String](($Letters.$Letter.ASCII).Split("`n"))[$Num-$IndexSubtract]
if ($LineFragment.Length -lt $Letters.$Letter.Width) {
$LineFragment += ' ' * ($Letters.$Letter.Width - $LineFragment.Length)
}
$Lines.$StringNum += $LineFragment
}
}
$LetterPos++
} # end of LetterArray foreach
# Return stuff
$Lines.GetEnumerator() |
Sort-Object -Property Name |
Select -ExpandProperty Value |
Where-Object {
$_ -match '\S'
} | ForEach-Object {
if ($PrependChar) {
"'" + $_
}
else {
$_
}
}
}
# Populate the $Letters hashtable with character data from the XML.
Function Get-LetterXML {
$LetterFile = Join-Path $PSScriptRoot 'letters.xml'
$Xml = [xml] (Get-Content $LetterFile)
$Xml.Chars.Char | ForEach-Object {
$Letters.($_.Name) = New-Object PSObject -Property @{
'Fixation' = $_.fixation
'Lines' = $_.lines
'ASCII' = $_.data
'Width' = $_.width
}
}
}
function Write-RainbowString {
param([String] $Line,
[String] $ForegroundColor = '',
[String] $BackgroundColor = '')
$Colors = @('Black', 'DarkBlue', 'DarkGreen', 'DarkCyan', 'DarkRed', 'DarkMagenta', 'DarkYellow',
'Gray', 'DarkGray', 'Blue', 'Green', 'Cyan', 'Red', 'Magenta', 'Yellow', 'White')
# $Colors[(Get-Random -Min 0 -Max 16)]
[Char[]] $Line | %{
if ($ForegroundColor -and $ForegroundColor -ieq 'rainbow') {
if ($BackgroundColor -and $BackgroundColor -ieq 'rainbow') {
Write-Host -ForegroundColor $Colors[(
Get-Random -Min 0 -Max 16
)] -BackgroundColor $Colors[(
Get-Random -Min 0 -Max 16
)] -NoNewline $_
}
elseif ($BackgroundColor) {
Write-Host -ForegroundColor $Colors[(
Get-Random -Min 0 -Max 16
)] -BackgroundColor $BackgroundColor `
-NoNewline $_
}
else {
Write-Host -ForegroundColor $Colors[(
Get-Random -Min 0 -Max 16
)] -NoNewline $_
}
}
# One of them has to be a rainbow, so we know the background is a rainbow here...
else {
if ($ForegroundColor) {
Write-Host -ForegroundColor $ForegroundColor -BackgroundColor $Colors[(
Get-Random -Min 0 -Max 16
)] -NoNewline $_
}
else {
Write-Host -BackgroundColor $Colors[(Get-Random -Min 0 -Max 16)] -NoNewline $_
}
}
}
Write-Host ''
}
# Get ASCII art letters/characters and data from XML. Make it persistent for the module.
if (-not (Get-Variable -EA SilentlyContinue -Scope Script -Name Letters)) {
$script:Letters = @{}
Get-LetterXML
}
# Turn the [string[]] into a [String] the only way I could figure out how... wtf
#$Text = ''
#$InputObject | ForEach-Object { $Text += "$_ " }
# Limit to 30 characters
#$MaxChars = 30
#if ($Text.Length -gt $MaxChars) { "Too long text. There's a maximum of $MaxChars characters."; return }
# Replace spaces with underscores (that's what's used for spaces in the XML).
#$Text = $Text -replace ' ', '_'
# Define accepted characters (which are found in XML).
#$AcceptedChars = '[^a-z0-9 _,!?./;:<>()¤{}\[\]\|\^=\$\-''+`\\"æøåâàáéèêóòôü]' # Some chars only works when sent as UTF-8 on IRC
$LetterArray = [string[]]($Letters.GetEnumerator() | Sort-Object -Property Name | Select-Object -ExpandProperty Name)
$AcceptedChars = [regex] ( '(?i)[^' + ([regex]::Escape(($LetterArray -join '')) -replace '-', '\-' -replace '\]', '\]') + ' ]' )
# Debug
#Write-Host -fore cyan $AcceptedChars.ToString()
}
process {
if ($InputObject -match $AcceptedChars) {
"Unsupported character, using these accepted characters: " + ($LetterArray -replace '^template$' -join ', ') + "."
return
}
# Filthy workaround (now worked around in the foreach creating the string).
#if ($Text.Length -eq 1) { $Text += '_' }
$Lines = @()
foreach ($Text in $InputObject) {
$ASCII = Get-Ascii ($Text -replace ' ', '_')
if ($ForegroundColor -ne 'Default' -and $BackgroundColor -ne 'Default') {
if ($ForegroundColor -ieq 'rainbow' -or $BackGroundColor -ieq 'rainbow') {
$ASCII | ForEach-Object {
Write-RainbowString -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor -Line $_
}
}
else {
Write-Host -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor ($ASCII -join "`n")
}
}
elseif ($ForegroundColor -ne 'Default') {
if ($ForegroundColor -ieq 'rainbow') {
$ASCII | ForEach-Object {
Write-RainbowString -ForegroundColor $ForegroundColor -Line $_
}
}
else {
Write-Host -ForegroundColor $ForegroundColor ($ASCII -join "`n")
}
}
elseif ($BackgroundColor -ne 'Default') {
if ($BackgroundColor -ieq 'rainbow') {
$ASCII | ForEach-Object {
Write-RainbowString -BackgroundColor $BackgroundColor -Line $_
}
}
else {
Write-Host -BackgroundColor $BackgroundColor ($ASCII -join "`n")
}
}
else { $ASCII -replace '\s+$' }
} # end of foreach
} # end of process block
} # end of function