Out-StripAnsi.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#### lolcat for powershell
#### Author Ma Bingyao

function Out-StripAnsi {
    <#
    .SYNOPSIS
    Usage: Out-StripAnsi
    .EXAMPLE
    Get-ChildItem | Out-String -Stream | Out-StripAnsi
    .PARAMETER InputObject
    PipeLine input, it must be a strings
    #>

    param(
        [Parameter(Mandatory=$True,ValueFromPipeline=$True)]
        $InputObject
    )
    BEGIN {
        $Pattern = "[\u001B\u009B][[\]()#;?]*(?:(?:(?:[a-zA-Z\d]*(?:;[a-zA-Z\d]*)*)?\u0007)|(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-ntqry=><~]))";
    }
    PROCESS {
        if ($InputObject -isnot [string]) {
            $InputObject = $InputObject.ToString();
        }
        if ($InputObject.Length -eq 0) {
            ""
        }
        else {
            $InputObject -replace $Pattern, ""
        }
    }
    END {
    }
}