Skip to content

Instantly share code, notes, and snippets.

@milichev
Last active April 24, 2020 23:55
Show Gist options
  • Save milichev/ad23b68d9d8f4731df8a29b560d43cf7 to your computer and use it in GitHub Desktop.
Save milichev/ad23b68d9d8f4731df8a29b560d43cf7 to your computer and use it in GitHub Desktop.
ANSI Colors
@echo off
:: main work done by @mlocati here https://gist.github.com/mlocati/fdabcaeb8071d5c75a2d51712db24011#file-win10colors-cmd
cls
setlocal enableextensions
call :setESC
echo %ESC%[101;93m Test %ESC%[0m
echo %ESC%[1m Test 1 %ESC%[0m
echo %ESC%[2m Test 2 %ESC%[0m
echo %ESC%[3m Test 3 %ESC%[0m
echo %ESC%[4m Test 4 %ESC%[0m
echo %ESC%[5m Test 5 %ESC%[0m
echo %ESC%[6m Test 6 %ESC%[0m
echo %ESC%[7m Test 7 %ESC%[0m
echo %ESC%[8m Test 8 %ESC%[0m
echo %ESC%[9m Test 9 %ESC%[0m
pause
:setESC
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set ESC=%%b
exit /B 0
)
exit /B 0
<#
.SYNOPSIS
Provides possibility to colorize text in console using ANSI sequences.
#>
using namespace System.Drawing
function ToColor {
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName, Position = 0)]
[ValidateNotNullOrEmpty()]
[object] $ColorInput
)
if ($ColorInput -is [Color]) {
return $ColorInput
}
if ($ColorInput -is [string]) {
if ($ColorInput -imatch '^[a-z]+$') {
return [System.Drawing.Color]::FromName($ColorInput)
}
if ($ColorInput -match '^#([0-9A-F]{3}|[0-9A-F]{6})$') {
return [System.Drawing.ColorTranslator]::FromHtml($ColorInput)
}
}
throw "Could not convert the value `"$ColorInput`" of type `"$($ColorInput.GetType().FullName)`" to Color"
}
<#
.SYNOPSIS
Colorizes text using ANSI console feature.
.PARAMETER Text
String text to colorize.
.PARAMETER ForecroundColor
Foreground color to be applied to the Text. Can be a color name, hex value (#112233), or System.Drawing.Color.
.PARAMETER BackgroundColor
Background color to be applied to the Text. Can be a color name, hex value (#112233), or System.Drawing.Color.
.EXAMPLE
# Returns the string "Alarma" wrapped with ANSI color sequences.
Set-TextAnsiCodes 'Alarma' -ForecroundColor 'LightYellow' -BackgroundColor 'DarkGreen'
.EXAMPLE
# A bit more complex output with aliases:
"Value: $(AnsiTxt 56 -fc '#eeaaaa' -bc '#884444')`nCount: $(AnsiTxt 123 -fc '#aaeeaa' -bc '#448844')"
#>
function Set-TextAnsiCodes {
[CmdletBinding()]
param(
[Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[string] $Text,
[Alias("fc")]
[Parameter(Position = 1)]
[object]$ForecroundColor,
[Alias("bc")]
[Parameter(Position = 2)]
[object]$BackgroundColor,
[Alias("r")]
[Parameter()]
[switch] $Reversed,
[Alias("b")]
[Parameter()]
[switch] $Bold,
[Alias("u")]
[Parameter()]
[switch] $Underline
)
begin {
if ($ForecroundColor) {
$ForecroundColor = ToColor $ForecroundColor
}
if ($BackgroundColor) {
$BackgroundColor = ToColor $BackgroundColor
}
}
process {
$Esc = [char]27
$Terminate = "$Esc[0m"
if ($ForecroundColor) {
$pre = "$Esc[38;2;$($ForecroundColor.R);$($ForecroundColor.G);$($ForecroundColor.B)m"
$Text = "$pre$Text$Terminate"
}
if ($BackgroundColor) {
$pre = "$Esc[48;2;$($BackgroundColor.R);$($BackgroundColor.G);$($BackgroundColor.B)m"
$Text = "$pre$Text$Terminate"
}
if ($Reversed) {
$pre = "$Esc[7m"
$Text = "$pre$Text$Terminate"
}
if ($Bold) {
$pre = "$Esc[1m"
$Text = "$pre$Text$Terminate"
}
if ($Underline) {
$pre = "$Esc[4m"
$Text = "$pre$Text$Terminate"
}
$Text
}
end { }
}
function Remove-TextAnsiCodes {
[CmdletBinding()]
param(
[Parameter(Mandatory, Position = 0)]
[string] $Text
)
$Text -replace "\x1b\[[\w;]+m", ""
}
Set-Alias "AnsiTxt" Set-TextAnsiCodes
Export-ModuleMember -Function ToColor,Set-TextAnsiCodes,Remove-TextAnsiCodes -Alias AnsiTxt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment