Skip to content

Commit

Permalink
Merge pull request #99 from StartAutomating/EZ-Hyperlinks
Browse files Browse the repository at this point in the history
EZOut 1.9.9
  • Loading branch information
StartAutomating authored Dec 15, 2022
2 parents de5e11a + 7dc2897 commit 1967c05
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 73 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 1.9.9:

* Format-RichText -Hyperlink/-Link/-HRef now works (Fixes #93)
* Format-RichText now supports -Alignment and -LineLength (Fixes #45)
* GitHub Action no longer output extra information (Fixes #98)

---

## 1.9.8:

* Format-RichText now supports -Hyperlink (Fixes #93)
Expand Down
24 changes: 5 additions & 19 deletions EZOut.GitHubAction.PSDevOps.ps1
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
#requires -Module PSDevOps
#requires -Module EZOut
Import-BuildStep -ModuleName EZOut
New-GitHubAction -Name "UseEZOut" -Description 'Generate Formatting and Types .ps1xml for PowerShell Modules, using EZOut' -Action EZOutAction -Icon code -ActionOutput ([Ordered]@{
EZOutScriptRuntime = [Ordered]@{
description = "The time it took the .EZOutScript parameter to run"
value = '${{steps.EZOutAction.outputs.EZOutScriptRuntime}}'
}
EZOutPS1Runtime = [Ordered]@{
description = "The time it took all .EZOut.ps1 files to run"
value = '${{steps.EZOutAction.outputs.EZOutPS1Runtime}}'
}
EZOutPS1Files = [Ordered]@{
description = "The .EZOut.ps1 files that were run (separated by semicolons)"
value = '${{steps.EZOutAction.outputs.EZOutPS1Files}}'
}
EZOutPS1Count = [Ordered]@{
description = "The number of .EZOut.ps1 files that were run"
value = '${{steps.EZOutAction.outputs.EZOutPS1Count}}'
}
}) |
Set-Content .\action.yml -Encoding UTF8 -PassThru
Push-Location $PSScriptRoot
New-GitHubAction -Name "UseEZOut" -Description @'
Generate Formatting and Types .ps1xml for PowerShell Modules, using EZOut
'@ -Action EZOutAction -Icon code -OutputPath .\action.yml
Pop-Location
76 changes: 64 additions & 12 deletions EZOut.format.ps1xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-16"?>
<!-- Generated with EZOut 1.9.8: Install-Module EZOut or https://github.com/StartAutomating/EZOut -->
<!-- Generated with EZOut 1.9.9: Install-Module EZOut or https://github.com/StartAutomating/EZOut -->
<Configuration>
<SelectionSets>
<SelectionSet>
Expand Down Expand Up @@ -975,7 +975,7 @@ if ($Request -or $Host.UI.SupportsHTML) {
.Notes
Stylized Output works in two contexts at present:
* Rich consoles (Windows Terminal, PowerShell.exe, Pwsh.exe) (when $host.UI.SupportsVirtualTerminal)
* Web pages (Based off the presence of a $Request variable, or when $host.UI.SupportsHTML (you must add this property to $host.UI))
* Web pages (Based off the presence of a $Request variable, or when $host.UI.SupportsHTML (you must add this property to $host.UI))
#&gt;
[Management.Automation.Cmdlet("Format","Object")]
[ValidateScript({
Expand Down Expand Up @@ -1032,7 +1032,16 @@ if ($Request -or $Host.UI.SupportsHTML) {
$Link,

# If set, will not clear formatting
[switch]$NoClear
[switch]$NoClear,

# The alignment. Defaulting to Left.
# Setting an alignment will pad the remaining space on each line.
[ValidateSet('Left','Right','Center')]
[string]
$Alignment,

# The length of a line. By default, the buffer width
[int]$LineLength = $($host.UI.RawUI.BufferSize.Width)
)

begin {
Expand All @@ -1042,6 +1051,19 @@ if ($Request -or $Host.UI.SupportsHTML) {
Output='';Error='BrightRed';Warning='BrightYellow';
Verbose='BrightCyan';Debug='Yellow';Progress='Cyan';
Success='BrightGreen';Failure='Red';Default=''}

$ansiCode = [Regex]::new(@'
(?&lt;ANSI_Code&gt;
(?-i)\e # An Escape
\[ # Followed by a bracket
(?&lt;ParameterBytes&gt;[\d\:\;\&lt;\=\&gt;\?]{0,}) # Followed by zero or more parameter
bytes
(?&lt;IntermediateBytes&gt;[\s\!\"\#\$\%\&amp;\'\(\)\*\+\,\-\.\/]{0,}) # Followed by zero or more
intermediate bytes
(?&lt;FinalByte&gt;[\@ABCDEFGHIJKLMNOPQRSTUVWXYZ\[\\\]\^_\`abcdefghijklmnopqrstuvwxyz\{\|\}\~]) # Followed by a final byte

)
'@)
$esc = [char]0x1b
$standardColors = 'Black', 'Red', 'Green', 'Yellow', 'Blue','Magenta', 'Cyan', 'White'
$brightColors = 'BrightBlack', 'BrightRed', 'BrightGreen', 'BrightYellow', 'BrightBlue','BrightMagenta', 'BrightCyan', 'BrightWhite'
Expand Down Expand Up @@ -1227,14 +1249,18 @@ if ($Request -or $Host.UI.SupportsHTML) {
elseif ($canUseANSI) {'' +$esc + "[21m" }
}

if ($Hyperlink) {
if ($Alignment -and $canUseHTML) {
"display:block;text-align:$($Alignment.ToLower())"
}

if ($Link) {
if ($canUseHTML) {
# Hyperlinks need to be a nested element
# so we will not add it to style attributes for HTML
}
elseif ($canUseANSI) {
# For ANSI,
'' + $esc + ']8m;;' + $Hyperlink + $esc + '\'
'' + $esc + ']8m;;' + $Link + $esc + '\'
}
}

Expand All @@ -1247,8 +1273,8 @@ if ($Request -or $Host.UI.SupportsHTML) {
)$(
if ($cssClasses) { " class='$($cssClasses -join ' ')'"}
)&gt;" + $(
if ($Hyperlink) {
"&lt;a href='$hyperLink'&gt;"
if ($Link) {
"&lt;a href='$link'&gt;"
}
)
} elseif ($canUseANSI) {
Expand All @@ -1257,21 +1283,47 @@ if ($Request -or $Host.UI.SupportsHTML) {
}

process {
$inputObjectAsString =
"$(if ($inputObject) { $inputObject | Out-String})".Trim()

$inputObjectAsString =
if ($Alignment -and -not $canUseHTML) {
(@(foreach ($inputObjectLine in ($inputObjectAsString -split '(?&gt;\r\n|\n)')) {
$inputObjectLength = $ansiCode.Replace($inputObjectLine, '').Length
if ($inputObjectLength -lt $LineLength) {
if ($Alignment -eq 'Left') {
$inputObjectLine
} elseif ($Alignment -eq 'Right') {
(' ' * ($LineLength - $inputObjectLength)) + $inputObjectLine
} else {
$half = ($LineLength - $inputObjectLength)/2
(' ' * [Math]::Floor($half)) + $inputObjectLine +
(' ' * [Math]::Ceiling($half))
}
}
else {
$inputObjectLine
}
}) -join [Environment]::NewLine) + [Environment]::newline
} else {
$inputObjectAsString
}

$allOutput +=
if ($header) {
"$header" + "$(if ($inputObject) { $inputObject | Out-String})".Trim()
"$header" + $inputObjectAsString
}
elseif ($inputObject) {
($inputObject | Out-String).Trim()
}
$inputObjectAsString
}
}

end {

if (-not $NoClear) {
$allOutput +=
if ($canUseHTML) {
if ($Hyperlink) {
if ($Link) {
"&lt;/a&gt;"
}
"&lt;/span&gt;"
Expand Down Expand Up @@ -1305,7 +1357,7 @@ if ($Request -or $Host.UI.SupportsHTML) {
"$esc[49m"
}

if ($Hyperlink) {
if ($Link) {
"$esc]8;;$esc\"
}

Expand Down
10 changes: 9 additions & 1 deletion EZOut.psd1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@{
ModuleToProcess = 'EZOut.psm1'
ModuleVersion = '1.9.8'
ModuleVersion = '1.9.9'
GUID = 'cef786f0-8a0b-4a5d-a2c6-b433095354cd'
Author = 'James Brundage'
CompanyName = 'Start-Automating'
Expand Down Expand Up @@ -65,6 +65,14 @@

Tags = '.ps1xml', 'Format','Output','Types', 'Colorized'
ReleaseNotes = @'
## 1.9.9:
* Format-RichText -Hyperlink/-Link/-HRef now works (Fixes #93)
* Format-RichText now supports -Alignment and -LineLength (Fixes #45)
* GitHub Action no longer output extra information (Fixes #98)
---
## 1.9.8:
* Format-RichText now supports -Hyperlink (Fixes #93)
Expand Down
40 changes: 35 additions & 5 deletions EZOut.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -438,13 +438,13 @@ describe "Write-FormatCustomView" {
[PSCustomObject]@{PSTypeName=$tn;n=1} | Out-String | should -Belike '*This is true*'
}

it "Will render an -Action that has only one token, which is a literal string, as <Text>" {
it "Will render an -Action that has only one token, which is a literal string, as a Text element" {
$fv = Write-formatCustomview -Action { "foobar" }
$fvXml = [xml]$fv
$fvXml.CustomControl.CustomEntries.CustomEntry.CustomItem.Text | should -Be foobar
}

it "Will render an -Action that uses the mythical command Write-NewLine will become a <Newline/>" {
it "Will render an -Action that uses the mythical command Write-NewLine will become a Newline element" {
$fv = Write-FormatCustomView -Action { Write-NewLine }
$fvXml = [xml]$fv
if (-not $fvXml.CustomControl.CustomEntries.CustomEntry.CustomItem.ChildNodes[0].Name -eq 'Newline') {
Expand All @@ -470,7 +470,7 @@ describe "Write-FormatCustomView" {
Add-FormatData


[PSCustomObject]@{PSTypeName='HostAwareFormatter';N=1} | Out-String | should -Belike "*normal host*"
[PSCustomObject]@{PSTypeName='HostAwareFormatter';N=1} | Out-String | should -Belike "*normal*host*"
}

it 'Can use a -ViewCondition with a -ViewSelectionSet to match multiple typenames (piping is still required)' {
Expand All @@ -486,7 +486,7 @@ describe "Write-FormatCustomView" {
Add-FormatData


[PSCustomObject]@{PSTypeName='HostAwareFormatter';N=1} | Out-String | should -Belike "*normal host*"
[PSCustomObject]@{PSTypeName='HostAwareFormatter';N=1} | Out-String | should -Belike "*normal*host*"
}

it 'Can just run a command with no parameters in -Action' {
Expand Down Expand Up @@ -1022,7 +1022,7 @@ describe 'Add-FormatData' {
Out-FormatData |
Add-FormatData -PassThru |
Select-Object -ExpandProperty Name |
should match 'FormatModule\d+'
should -Match 'FormatModule\d+'
}
}
}
Expand Down Expand Up @@ -1203,4 +1203,34 @@ describe 'Format-Hashtable' {
it 'Can format a hashtable' {
Format-Hashtable -InputObject @{a='a'} | Should -Match "^\@\{\s+a\s=\s'a'\s+}"
}
}

describe 'Format-RichText' {
it 'Can format rich text' {
Format-RichText -ForegroundColor success -InputObject 'yay' | Should -Match '\e\[1;32'
}

it 'Can format any RGB color' {
$r, $g, $b = foreach ($n in 1..3) { Get-Random -Minimum 0 -Maximum 255 }
$rgb = "#{0:x2}{1:x2}{2:x2}" -f $r,$g,$b
Format-RichText -InputObject $rgb -ForegroundColor $rgb |
Should -Match "^\e\[38;2;$r;$g;$b"
}

it 'Can make a hyperlink' {
Format-RichText -Hyperlink https://github.com/StartAutomating/EZOut -InputObject EZOut |
Should -Match '^\e\]8m;;'
}

it 'Can make text bold' {
Format-RichText -InputObject "bold" -Bold | Should -Match '\e\[1'
}

it 'Can make text italic' {
Format-RichText -InputObject 'italic' -Italic | Should -Match '\e\[2'
}

it 'Can make text underlined' {
Format-RichText -InputObject 'underline' -Underline | Should -Match '\e\[4'
}
}
Loading

0 comments on commit 1967c05

Please sign in to comment.