|
1 | 1 | <?xml version="1.0" encoding="utf-16"?> |
2 | | -<!-- Generated with EZOut 1.9.7: Install-Module EZOut or https://github.com/StartAutomating/EZOut --> |
| 2 | +<!-- Generated with EZOut 1.9.9: Install-Module EZOut or https://github.com/StartAutomating/EZOut --> |
3 | 3 | <Configuration> |
4 | 4 | <Controls> |
5 | 5 | <Control> |
|
156 | 156 | .Notes |
157 | 157 | Stylized Output works in two contexts at present: |
158 | 158 | * Rich consoles (Windows Terminal, PowerShell.exe, Pwsh.exe) (when $host.UI.SupportsVirtualTerminal) |
159 | | - * Web pages (Based off the presence of a $Request variable, or when $host.UI.SupportsHTML (you must add this property to $host.UI)) |
| 159 | + * Web pages (Based off the presence of a $Request variable, or when $host.UI.SupportsHTML (you must add this property to $host.UI)) |
160 | 160 | #> |
161 | 161 | [Management.Automation.Cmdlet("Format","Object")] |
162 | 162 | [ValidateScript({ |
|
165 | 165 | if (-not ($canUseANSI -or $canUseHTML)) { return $false} |
166 | 166 | return $true |
167 | 167 | })] |
| 168 | + [OutputType([string])] |
168 | 169 | param( |
169 | 170 | # The input object |
170 | 171 | [Parameter(ValueFromPipeline)] |
171 | 172 | [PSObject] |
172 | 173 | $InputObject, |
173 | | - |
| 174 | + |
174 | 175 | # The foreground color |
175 | 176 | [string]$ForegroundColor, |
176 | 177 |
|
|
205 | 206 |
|
206 | 207 | # If set, will invert text |
207 | 208 | [switch]$Invert, |
| 209 | + |
| 210 | + # If provided, will create a hyperlink to a given uri |
| 211 | + [Alias('Hyperlink', 'Href')] |
| 212 | + [uri] |
| 213 | + $Link, |
| 214 | + |
208 | 215 | # If set, will not clear formatting |
209 | | - [switch]$NoClear |
| 216 | + [switch]$NoClear, |
| 217 | + |
| 218 | + # The alignment. Defaulting to Left. |
| 219 | + # Setting an alignment will pad the remaining space on each line. |
| 220 | + [ValidateSet('Left','Right','Center')] |
| 221 | + [string] |
| 222 | + $Alignment, |
| 223 | + |
| 224 | + # The length of a line. By default, the buffer width |
| 225 | + [int]$LineLength = $($host.UI.RawUI.BufferSize.Width) |
210 | 226 | ) |
211 | 227 |
|
212 | 228 | begin { |
|
216 | 232 | Output='';Error='BrightRed';Warning='BrightYellow'; |
217 | 233 | Verbose='BrightCyan';Debug='Yellow';Progress='Cyan'; |
218 | 234 | Success='BrightGreen';Failure='Red';Default=''} |
| 235 | + |
| 236 | + $ansiCode = [Regex]::new(@' |
| 237 | + (?<ANSI_Code> |
| 238 | + (?-i)\e # An Escape |
| 239 | + \[ # Followed by a bracket |
| 240 | + (?<ParameterBytes>[\d\:\;\<\=\>\?]{0,}) # Followed by zero or more parameter |
| 241 | + bytes |
| 242 | + (?<IntermediateBytes>[\s\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/]{0,}) # Followed by zero or more |
| 243 | + intermediate bytes |
| 244 | + (?<FinalByte>[\@ABCDEFGHIJKLMNOPQRSTUVWXYZ\[\\\]\^_\`abcdefghijklmnopqrstuvwxyz\{\|\}\~]) # Followed by a final byte |
| 245 | + |
| 246 | + ) |
| 247 | +'@) |
219 | 248 | $esc = [char]0x1b |
220 | 249 | $standardColors = 'Black', 'Red', 'Green', 'Yellow', 'Blue','Magenta', 'Cyan', 'White' |
221 | 250 | $brightColors = 'BrightBlack', 'BrightRed', 'BrightGreen', 'BrightYellow', 'BrightBlue','BrightMagenta', 'BrightCyan', 'BrightWhite' |
222 | 251 |
|
| 252 | + $allOutput = @() |
| 253 | + |
223 | 254 | $n =0 |
224 | | - $cssClasses = @() |
| 255 | + $cssClasses = @() |
225 | 256 | $colorAttributes = |
226 | 257 | @(:nextColor foreach ($hc in $ForegroundColor,$BackgroundColor) { |
227 | 258 | $n++ |
|
398 | 429 | if ($canUseHTML) { "border-bottom: 3px double;"} |
399 | 430 | elseif ($canUseANSI) {'' +$esc + "[21m" } |
400 | 431 | } |
| 432 | + |
| 433 | + if ($Alignment -and $canUseHTML) { |
| 434 | + "display:block;text-align:$($Alignment.ToLower())" |
| 435 | + } |
| 436 | + |
| 437 | + if ($Link) { |
| 438 | + if ($canUseHTML) { |
| 439 | + # Hyperlinks need to be a nested element |
| 440 | + # so we will not add it to style attributes for HTML |
| 441 | + } |
| 442 | + elseif ($canUseANSI) { |
| 443 | + # For ANSI, |
| 444 | + '' + $esc + ']8m;;' + $Link + $esc + '\' |
| 445 | + } |
| 446 | + } |
401 | 447 |
|
402 | 448 | ) |
403 | 449 |
|
|
407 | 453 | if ($styleAttributes) { " style='$($styleAttributes -join ';')'"} |
408 | 454 | )$( |
409 | 455 | if ($cssClasses) { " class='$($cssClasses -join ' ')'"} |
410 | | - )>" |
| 456 | + )>" + $( |
| 457 | + if ($Link) { |
| 458 | + "<a href='$link'>" |
| 459 | + } |
| 460 | + ) |
411 | 461 | } elseif ($canUseANSI) { |
412 | 462 | $styleAttributes -join '' |
413 | 463 | } |
414 | 464 | } |
415 | 465 |
|
416 | 466 | process { |
417 | | - if ($header) { |
418 | | - "$header" + "$(if ($inputObject) { $inputObject | Out-String})".Trim() |
419 | | - } |
420 | | - elseif ($inputObject) { |
421 | | - ($inputObject | Out-String).Trim() |
422 | | - } |
| 467 | + $inputObjectAsString = |
| 468 | + "$(if ($inputObject) { $inputObject | Out-String})".Trim() |
| 469 | + |
| 470 | + $inputObjectAsString = |
| 471 | + if ($Alignment -and -not $canUseHTML) { |
| 472 | + (@(foreach ($inputObjectLine in ($inputObjectAsString -split '(?>\r\n|\n)')) { |
| 473 | + $inputObjectLength = $ansiCode.Replace($inputObjectLine, '').Length |
| 474 | + if ($inputObjectLength -lt $LineLength) { |
| 475 | + if ($Alignment -eq 'Left') { |
| 476 | + $inputObjectLine |
| 477 | + } elseif ($Alignment -eq 'Right') { |
| 478 | + (' ' * ($LineLength - $inputObjectLength)) + $inputObjectLine |
| 479 | + } else { |
| 480 | + $half = ($LineLength - $inputObjectLength)/2 |
| 481 | + (' ' * [Math]::Floor($half)) + $inputObjectLine + |
| 482 | + (' ' * [Math]::Ceiling($half)) |
| 483 | + } |
| 484 | + } |
| 485 | + else { |
| 486 | + $inputObjectLine |
| 487 | + } |
| 488 | + }) -join [Environment]::NewLine) + [Environment]::newline |
| 489 | + } else { |
| 490 | + $inputObjectAsString |
| 491 | + } |
| 492 | + |
| 493 | + $allOutput += |
| 494 | + if ($header) { |
| 495 | + "$header" + $inputObjectAsString |
| 496 | + } |
| 497 | + elseif ($inputObject) { |
| 498 | + $inputObjectAsString |
| 499 | + } |
423 | 500 | } |
424 | 501 |
|
425 | 502 | end { |
426 | 503 |
|
427 | 504 | if (-not $NoClear) { |
428 | | - if ($canUseHTML) { |
429 | | - "</span>" |
430 | | - } |
431 | | - elseif ($canUseANSI) { |
432 | | - if ($Bold -or $Faint -or $colorAttributes -match '\[1;') { |
433 | | - "$esc[22m" |
434 | | - } |
435 | | - if ($Italic) { |
436 | | - "$esc[23m" |
437 | | - } |
438 | | - if ($Underline -or $doubleUnderline) { |
439 | | - "$esc[24m" |
440 | | - } |
441 | | - if ($Blink) { |
442 | | - "$esc[25m" |
443 | | - } |
444 | | - if ($Invert) { |
445 | | - "$esc[27m" |
446 | | - } |
447 | | - if ($hide) { |
448 | | - "$esc[28m" |
449 | | - } |
450 | | - if ($Strikethru) { |
451 | | - "$esc[29m" |
452 | | - } |
453 | | - if ($ForegroundColor) { |
454 | | - "$esc[39m" |
455 | | - } |
456 | | - if ($BackgroundColor) { |
457 | | - "$esc[49m" |
| 505 | + $allOutput += |
| 506 | + if ($canUseHTML) { |
| 507 | + if ($Link) { |
| 508 | + "</a>" |
| 509 | + } |
| 510 | + "</span>" |
458 | 511 | } |
459 | | - |
460 | | - if (-not ($Underline -or $Bold -or $Invert -or $ForegroundColor -or $BackgroundColor)) { |
461 | | - '' + $esc + '[0m' |
| 512 | + elseif ($canUseANSI) { |
| 513 | + if ($Bold -or $Faint -or $colorAttributes -match '\[1;') { |
| 514 | + "$esc[22m" |
| 515 | + } |
| 516 | + if ($Italic) { |
| 517 | + "$esc[23m" |
| 518 | + } |
| 519 | + if ($Underline -or $doubleUnderline) { |
| 520 | + "$esc[24m" |
| 521 | + } |
| 522 | + if ($Blink) { |
| 523 | + "$esc[25m" |
| 524 | + } |
| 525 | + if ($Invert) { |
| 526 | + "$esc[27m" |
| 527 | + } |
| 528 | + if ($hide) { |
| 529 | + "$esc[28m" |
| 530 | + } |
| 531 | + if ($Strikethru) { |
| 532 | + "$esc[29m" |
| 533 | + } |
| 534 | + if ($ForegroundColor) { |
| 535 | + "$esc[39m" |
| 536 | + } |
| 537 | + if ($BackgroundColor) { |
| 538 | + "$esc[49m" |
| 539 | + } |
| 540 | + |
| 541 | + if ($Link) { |
| 542 | + "$esc]8;;$esc\" |
| 543 | + } |
| 544 | + |
| 545 | + if (-not ($Underline -or $Bold -or $Invert -or $ForegroundColor -or $BackgroundColor)) { |
| 546 | + '' + $esc + '[0m' |
| 547 | + } |
462 | 548 | } |
463 | | - } |
464 | 549 | } |
| 550 | + |
| 551 | + $allOutput -join '' |
465 | 552 | } |
466 | 553 | </ScriptBlock> |
467 | 554 | </ExpressionBinding> |
|
813 | 900 |
|
814 | 901 | # If set, will create a link. The -InputObject will be used as the link content |
815 | 902 | [Parameter(ValueFromPipelineByPropertyName)] |
| 903 | + [Alias('Hyperlink', 'Href')] |
816 | 904 | [string] |
817 | 905 | $Link, |
818 | 906 |
|
|
0 commit comments