Skip to content

Commit

Permalink
feat: Export-4BitCss $psStyle classes ( Fixes #43, Fixes #58 )
Browse files Browse the repository at this point in the history
Improving Fault Tolerance
  • Loading branch information
James Brundage committed Apr 21, 2024
1 parent d92e935 commit 13b585b
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions Commands/Export-4BitCSS.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ function Export-4BitCSS
$myCmd = $MyInvocation.MyCommand

$jsonObject = [Ordered]@{}

$ColorOrder = @(
'Black', 'Red', 'Green', 'Yellow', 'Blue', 'Purple', 'Cyan', 'White'
)

# Walk over each parameter
:nextParam foreach ($keyValue in $PSBoundParameters.GetEnumerator()) {
Expand Down Expand Up @@ -245,10 +249,6 @@ function Export-4BitCSS
$NoBackgroundColor = $true
$NoStyle = $true
}

$ColorOrder = @(
'Black', 'Red', 'Green', 'Yellow', 'Blue', 'Purple', 'Cyan', 'White'
)

$rgb = ($Background -replace "#", "0x" -replace ';') -as [UInt32]
$r, $g, $b = ([float][byte](($rgb -band 0xff0000) -shr 16)/255),
Expand Down Expand Up @@ -431,7 +431,7 @@ s, strike, .strike, .Strike, .strikethrough, .Strikethrough { text-decoration: l
"@

foreach ($subproperty in 'Formatting', 'FileInfo','Progress') {
foreach ($styleProperty in $PSStyle.$subproperty.psobject.properties) {
:nextStyleProperty foreach ($styleProperty in $PSStyle.$subproperty.psobject.properties) {
if ($styleProperty.Value -notmatch '\e') { continue }
$null = $styleProperty.Value -match '\e\[(?<n>[\d;]+)m'
$styleBytes = $matches.n -split ';' -as [byte[]]
Expand All @@ -447,10 +447,18 @@ foreach ($subproperty in 'Formatting', 'FileInfo','Progress') {
default {
if ($_ -in 30..37) {
$colorName = $ColorOrder[$_ - 30]
if (-not $colorName) {
Write-Warning "Could not translate `$psStyle.$($subproperty).$($styleProperty.Name) to a color."
continue nextStyleProperty
}
$colorName = $colorName.Substring(0, 1).ToLower() + $colorName.Substring(1)
"color: var(--$colorName);"
} elseif ($_ -in 40..47) {
$colorName = $ColorOrder[$_ - 30]
if (-not $colorName) {
Write-Warning "Could not translate `$psStyle.$($subproperty).$($styleProperty.Name) to a color."
continue nextStyleProperty
}
$colorName = $colorName.Substring(0, 1).ToLower() + $colorName.Substring(1)
"background-color: var(--$colorName);"
} elseif ($_ -eq 38) {
Expand All @@ -460,9 +468,17 @@ foreach ($subproperty in 'Formatting', 'FileInfo','Progress') {
}
elseif ($_ -in 90..97) {
$colorName = "bright$($ColorOrder[$_ - 90])"
if (-not $colorName) {
Write-Warning "Could not translate `$psStyle.$($subproperty).$($styleProperty.Name) to a color."
continue nextStyleProperty
}
"color: var(--$colorName);"
} elseif ($_ -in 100..107) {
$colorName = "bright$($ColorOrder[$_ - 100])"
if (-not $colorName) {
Write-Warning "Could not translate `$psStyle.$($subproperty).$($styleProperty.Name) to a color."
continue nextStyleProperty
}
"background-color: var(--$colorName);"
}
}
Expand Down

0 comments on commit 13b585b

Please sign in to comment.