Last active
August 29, 2015 13:58
-
-
Save fzed51/10022078 to your computer and use it in GitHub Desktop.
Format-ANSI, formate une sortie console ANSI
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
<# | |
.Synopsis | |
Formate une sortie console ANSI | |
.DESCRIPTION | |
Cette commande interprête les commande ansi | |
ESC[PL;PcH | |
ESC[PL;Pcf | |
ESC[PnA | |
ESC[PnB | |
ESC[PnC | |
ESC[PnD | |
ESC[s | |
ESC[u | |
ESC[2J | |
ESC[K | |
ESC[Ps;...;Psm | |
.EXAMPLE | |
"←[H←[7mCoucou!!!" | Format-ANSI | |
.EXAMPLE | |
composer --ansi | Format-ANSI | |
#> | |
[CmdletBinding()] | |
Param | |
( | |
[Parameter(ValueFromPipeline=$true, | |
Position=0)] | |
[string] $in = '' | |
) | |
Begin | |
{ | |
$DefautForegroundColor = $Host.ui.RawUI.ForegroundColor | |
$DefautBackgroundColor = $Host.ui.RawUI.BackgroundColor | |
} | |
Process | |
{ | |
[Collections.ArrayList]$BackupPosX = @() | |
[Collections.ArrayList]$BackupPosY = @() | |
[int]$PosX = 0 | |
[int]$PosY = 0 | |
$fore = $DefautForegroundColor | |
$back = $DefautBackgroundColor | |
$in.Split([char]27+'←') | % { | |
[int]$x = $host.UI.RawUI.CursorPosition.X | |
[int]$y = $host.UI.RawUI.CursorPosition.Y | |
switch -Regex ($_) { | |
"^\[((\d+);(\d))?H(.*)" { | |
if ( $Matches[1] -ne $null ){ | |
$host.UI.RawUI.CursorPosition = $(New-Object Management.Automation.Host.Coordinates([int]$Matches[2],[int]$Matches[3])) | |
} else { | |
$host.UI.RawUI.CursorPosition = $(New-Object Management.Automation.Host.Coordinates(0,0)) | |
} | |
Write-Host $Matches[4] -no -fo $fore -ba $back | |
} | |
"^\[((\d+);(\d))?f(.*)" { | |
if ( $Matches[1] -ne $null ){ | |
$host.UI.RawUI.CursorPosition = $(New-Object Management.Automation.Host.Coordinates([int]$Matches[2],[int]$Matches[3])) | |
} else { | |
$host.UI.RawUI.CursorPosition = $(New-Object Management.Automation.Host.Coordinates(0,0)) | |
} | |
Write-Host $Matches[4] -no -fo $fore -ba $back | |
} | |
"^\[(\d+)A(.*)" { | |
$y = [Math]::Max(0, ($y-[int]$Matche[1])) | |
$host.UI.RawUI.CursorPosition = $(New-Object Management.Automation.Host.Coordinates($x,$y)) | |
Write-Host $Matches[2] -no -fo $fore -ba $back | |
} | |
"^\[(\d+)B(.*)" { | |
$y = $y+[int]$Matche[1] | |
$host.UI.RawUI.CursorPosition = $(New-Object Management.Automation.Host.Coordinates($x,$y)) | |
Write-Host $Matches[2] -no -fo $fore -ba $back | |
} | |
"^\[(\d+)C(.*)" { | |
$x = [Math]::Min(([int]$host.ui.RawUI.WindowSize.Width - 1), ($y + [int]$Matche[1])) | |
$host.UI.RawUI.CursorPosition = $(New-Object Management.Automation.Host.Coordinates($x,$y)) | |
Write-Host $Matches[2] -no -fo $fore -ba $back | |
} | |
"^\[(\d+)D(.*)" { | |
$x = [Math]::Max(0, ($y-[int]$Matche[1])) | |
$host.UI.RawUI.CursorPosition = $(New-Object Management.Automation.Host.Coordinates($x,$y)) | |
Write-Host $Matches[2] -no -fo $fore -ba $back | |
} | |
"^\[s(.*)" { | |
$BackupPosX.Insert(0,$x) | |
$BackupPosY.Insert(0,$y) | |
Write-Host $Matches[1] -no -fo $fore -ba $back | |
} | |
"^\[u(.*)" { | |
if( $BackupPosX.Count -gt 0 ){ | |
$x = $BackupPosX[0] | |
$y = $BackupPosY[0] | |
$BackupPosX.Remove(0,1) | |
$BackupPosY.Remove(0,1) | |
$host.UI.RawUI.CursorPosition = $(New-Object Management.Automation.Host.Coordinates($x,$y)) | |
} | |
Write-Host $Matches[1] -no -fo $fore -ba $back | |
} | |
"^\[2J(.*)" { | |
Clear-Host | |
Write-Host $Matches[1] -no -fo $fore -ba $back | |
} | |
"^\[K(.*)" { | |
$l = $host.ui.RawUI.WindowSize.Width -1 - $x | |
Write-Host " " * $l -no -fo $fore -ba $back | |
$host.UI.RawUI.CursorPosition = $(New-Object Management.Automation.Host.Coordinates($x,$y)) | |
Write-Host $Matches[1] -no -fo $fore -ba $back | |
} | |
"^\[([0-9;]+)m(.*)" { | |
$Matches[1].split(';') | % { | |
switch ($_) { | |
'0' { | |
$fore = $DefautForegroundColor | |
$back = $DefautBackgroundColor | |
} | |
'7' { | |
$fore, $back = $back, $fore | |
} | |
'30' { | |
$fore = 'Black' | |
} | |
'31' { | |
$fore = 'Red' | |
} | |
'32' { | |
$fore = 'Green' | |
} | |
'33' { | |
$fore = 'Yellow' | |
} | |
'34' { | |
$fore = 'Blue' | |
} | |
'35' { | |
$fore = 'Magenta' | |
} | |
'36' { | |
$fore = 'Cyan' | |
} | |
'37' { | |
$fore = 'White' | |
} | |
'40' { | |
$back = 'Black' | |
} | |
'41' { | |
$back = 'Red' | |
} | |
'42' { | |
$back = 'Green' | |
} | |
'43' { | |
$back = 'Yellow' | |
} | |
'44' { | |
$back = 'Blue' | |
} | |
'45' { | |
$back = 'Magenta' | |
} | |
'46' { | |
$back = 'Cyan' | |
} | |
'47' { | |
$back = 'White' | |
} | |
} | |
Write-Host $Matches[2] -no -fo $fore -ba $back | |
} | |
} | |
Default {Write-Host $_ -no -fo $fore -ba $back} | |
} | |
Remove-Variable x, y | |
} | |
Write-Host | |
} | |
End | |
{ | |
$Host.ui.RawUI.ForegroundColor = $DefautForegroundColor | |
$Host.ui.RawUI.BackgroundColor = $DefautBackgroundColor | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment