Last active
November 7, 2017 08:12
-
-
Save midnightfreddie/25efaf14114b37d30061169e04080bca to your computer and use it in GitHub Desktop.
In reply to https://www.reddit.com/r/PowerShell/comments/7ap1tz/speeding_up_colorful_output_to_the_console/ , and using codes from https://gist.github.com/mlocati/fdabcaeb8071d5c75a2d51712db24011
This file contains hidden or 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
# PowerShell 5 and above | |
Enum MyConColor { | |
Black | |
Red | |
Green | |
Yellow | |
Blue | |
Magenta | |
Cyan | |
White | |
} | |
# Pre-PowerShell-5 uses C# for enums | |
#Add-Type -TypeDefinition @" | |
# public enum MyConColor { | |
# Black, | |
# Red, | |
# Green, | |
# Yellow, | |
# Blue, | |
# Magenta, | |
# Cyan, | |
# White | |
# } | |
#"@ | |
# "Strong black" is gray | |
function Get-ConColors { | |
param ( | |
[Parameter(Position=1,Mandatory=$true)] | |
[String]$Text, | |
[MyConColor]$ForegroundColor = [MyConColor]::White, | |
[MyConColor]$BackgroundColor = [MyConColor]::Blue, | |
[switch]$ForegroundStrong, | |
[switch]$BackgroundStrong | |
) | |
$ConsoleTemplate = [char]27 + '[{0}m' | |
$FG = 30 + [int]$ForegroundColor | |
if ($ForegroundStrong) { $FG += 60 } | |
$BG = 40 + [int]$BackgroundColor | |
if ($BackgroundStrong) { $BG += 60 } | |
Write-Output ( | |
$ConsoleTemplate -f $FG + | |
$ConsoleTemplate -f $BG + | |
$Text + | |
$ConsoleTemplate -f 0 | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment