Out-Rainbow.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#### lolcat for powershell
#### Author Ma Bingyao

function Out-Rainbow {
    <#
    .SYNOPSIS
    Usage: Out-Rainbow [OPTION]...
    .EXAMPLE
    Out-Rainbow README
    .EXAMPLE
    Get-Process | Out-String -Stream | Out-Rainbow
    .PARAMETER InputObject
    PipeLine input, it must be a strings
    .PARAMETER Spread
    Rainbow Spread
    .PARAMETER freq
    Rainbow frequency
    .PARAMETER Seed
    Rainbow Seed, 0 = random
    .PARAMETER Animate
    Enable psychedelics
    .PARAMETER Duration
    Animation duration
    .PARAMETER Speed
    Animation speed
    #>

    param(
        [Parameter(Mandatory=$True,ValueFromPipeline=$True)]
        $InputObject,
        [Alias('p')]
        [double]$Spread = 3.0,
        [Alias('f')]
        [double]$Freq = 0.1,
        [Alias('i')]
        [int32]$Seed = 0,
        [Alias('a')]
        [switch]$Animate,
        [Alias('d')]
        [int32]$Duration = 12,
        [Alias('s')]
        [double]$Speed = 20.0
    )
    BEGIN {
        $ESC = [char]27;
        if ($Seed -eq 0) {
            $Seed = Get-Random -max 255;
        }
        if ($MyInvocation.PipelinePosition -ne $MyInvocation.PipelineLength) {
            $Animate = $False;
        }
        if ($Animate) {
            [Console]::Write("$ESC[?25l");
        }
        else {
            $Duration = 1;
        }
    }
    PROCESS {
        if ($InputObject -isnot [string]) {
            $InputObject = $InputObject.ToString();
        }
        if ($InputObject.Length -eq 0) {
            if ($Animate) {
                [Console]::WriteLine();
            }
            else {
                Write-Output ""
            }
            return;
        }
        $ENTER = [char]13;
        $NEWLINE = [char]10;
        [char[]] $Separators = $NEWLINE;
        $Lines = $InputObject.TrimEnd($ENTER, $NEWLINE).Replace("$ENTER$NEWLINE", "$NEWLINE").Replace($ENTER, $NEWLINE).Split($Separators, [StringSplitOptions]::None);
        foreach ($Line in $Lines) {
            $Seed++;
            $Length = $Line.Length;
            $Out = "";
            if ($Length -gt 0) {
                $s = $Seed;
                for ($x = 1; $x -le $Duration; $x++) {
                    $Out = "";
                    if ($x -lt $Duration) {
                        $Out += "$ESC[s";
                    }
                    if ($Animate) {
                        $s+=$Spread;
                    }
                    for ($i = 0; $i -lt $Length; $i++) {
                        $n = ($s+$i/$Spread);
                        $c = $Line[$i];
                        if ([Char]::IsSurrogatePair($c, $Line[$i+1])) {
                            $c = $c + $Line[$i+1];
                            $i++;
                        }
                        $Red   = [int]([Math]::Sin($Freq*$n + 0)*127 + 128);
                        $Green = [int]([Math]::Sin($Freq*$n + 2*[Math]::PI/3)*127 + 128);
                        $Blue  = [int]([Math]::Sin($Freq*$n + 4*[Math]::PI/3)*127 + 128);
                        $Out += "$ESC[38;2;$Red;$Green;$Blue;1m$c$ESC[0m";
                    }
                    if ($x -lt $Duration) {
                        $Out += "$ESC[u"
                    }
                    if ($Animate) {
                        [Console]::Write($Out)
                        Start-Sleep -Milliseconds ([int](1000/$Speed))
                    }
                }
            }
            if ($Animate) {
                [Console]::WriteLine()
            }
            else {
                Write-Output $Out
            }
        }
    }
    END {
        if ($Animate) {
            [Console]::Write("$ESC[?25h");
        }
    }
}