Last active
June 26, 2023 21:25
-
-
Save mklement0/42c183a1fa0529068e6c2240c7eac091 to your computer and use it in GitHub Desktop.
$PSStyle polyfill forWindows PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# $PSStyle polyfill for Windows PowerShell: | |
# When run on a PS v7.2+ system, generates source code for Windows PowerShell | |
# that creates a nested ordered hashtable that mimics the v7.2+ $PSStyle object, | |
# albeit only with respect to the properties with general-purpose ANSI (VT) | |
# escape sequences, such as $PSStyle.Italic and $PSStyle.Foreground.Yellow | |
# | |
# Simply copy and paste this snippet and execute it in a PowerShell 7.2+ session, | |
# and it will output source code for Windows PowerShell that defines $PSStyle. | |
@" | |
if (`$null -eq `$PSStyle) { | |
`$PSStyle = [ordered] @{} | |
$( | |
foreach ($p in $PSStyle.psobject.Properties.Name -notmatch '^(Foreground|Background|FileInfo|Formatting|OutputRendering|Progress)$') { | |
" `$PSStyle.$p = `"$($PSStyle.$p -replace "`e", '$([char]27)')`"`n" | |
} | |
foreach ($p in 'Foreground', 'Background') { | |
" `$PSStyle.$p = [ordered] @{}`n" | |
foreach ($sp in $PSStyle.$p.psobject.Properties.Name) { | |
" `$PSStyle.$p.$sp = `"$($PSStyle.$p.$sp -replace "`e", '$([char]27)')`"`n" | |
} | |
} | |
) | |
} | |
"@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Running the above in PowerShell 7.4.0-preview.3 yields the following: | |
if ($null -eq $PSStyle) { | |
$PSStyle = [ordered] @{} | |
$PSStyle.Reset = "$([char]27)[0m" | |
$PSStyle.BlinkOff = "$([char]27)[25m" | |
$PSStyle.Blink = "$([char]27)[5m" | |
$PSStyle.BoldOff = "$([char]27)[22m" | |
$PSStyle.Bold = "$([char]27)[1m" | |
$PSStyle.DimOff = "$([char]27)[22m" | |
$PSStyle.Dim = "$([char]27)[2m" | |
$PSStyle.Hidden = "$([char]27)[8m" | |
$PSStyle.HiddenOff = "$([char]27)[28m" | |
$PSStyle.Reverse = "$([char]27)[7m" | |
$PSStyle.ReverseOff = "$([char]27)[27m" | |
$PSStyle.ItalicOff = "$([char]27)[23m" | |
$PSStyle.Italic = "$([char]27)[3m" | |
$PSStyle.UnderlineOff = "$([char]27)[24m" | |
$PSStyle.Underline = "$([char]27)[4m" | |
$PSStyle.StrikethroughOff = "$([char]27)[29m" | |
$PSStyle.Strikethrough = "$([char]27)[9m" | |
$PSStyle.Foreground = [ordered] @{} | |
$PSStyle.Foreground.Black = "$([char]27)[30m" | |
$PSStyle.Foreground.Red = "$([char]27)[31m" | |
$PSStyle.Foreground.Green = "$([char]27)[32m" | |
$PSStyle.Foreground.Yellow = "$([char]27)[33m" | |
$PSStyle.Foreground.Blue = "$([char]27)[34m" | |
$PSStyle.Foreground.Magenta = "$([char]27)[35m" | |
$PSStyle.Foreground.Cyan = "$([char]27)[36m" | |
$PSStyle.Foreground.White = "$([char]27)[37m" | |
$PSStyle.Foreground.BrightBlack = "$([char]27)[90m" | |
$PSStyle.Foreground.BrightRed = "$([char]27)[91m" | |
$PSStyle.Foreground.BrightGreen = "$([char]27)[92m" | |
$PSStyle.Foreground.BrightYellow = "$([char]27)[93m" | |
$PSStyle.Foreground.BrightBlue = "$([char]27)[94m" | |
$PSStyle.Foreground.BrightMagenta = "$([char]27)[95m" | |
$PSStyle.Foreground.BrightCyan = "$([char]27)[96m" | |
$PSStyle.Foreground.BrightWhite = "$([char]27)[97m" | |
$PSStyle.Background = [ordered] @{} | |
$PSStyle.Background.Black = "$([char]27)[40m" | |
$PSStyle.Background.Red = "$([char]27)[41m" | |
$PSStyle.Background.Green = "$([char]27)[42m" | |
$PSStyle.Background.Yellow = "$([char]27)[43m" | |
$PSStyle.Background.Blue = "$([char]27)[44m" | |
$PSStyle.Background.Magenta = "$([char]27)[45m" | |
$PSStyle.Background.Cyan = "$([char]27)[46m" | |
$PSStyle.Background.White = "$([char]27)[47m" | |
$PSStyle.Background.BrightBlack = "$([char]27)[100m" | |
$PSStyle.Background.BrightRed = "$([char]27)[101m" | |
$PSStyle.Background.BrightGreen = "$([char]27)[102m" | |
$PSStyle.Background.BrightYellow = "$([char]27)[103m" | |
$PSStyle.Background.BrightBlue = "$([char]27)[104m" | |
$PSStyle.Background.BrightMagenta = "$([char]27)[105m" | |
$PSStyle.Background.BrightCyan = "$([char]27)[106m" | |
$PSStyle.Background.BrightWhite = "$([char]27)[107m" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment