plugins/consoleui/src/Spin-busy.ps1

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
function Global:Spin-Busy {
    param(
        [Int]$msWait = 0,
        [String]$spinStr = '-\|/. ',
        [Char[]]$spinChars = [Char[]] ($spinStr.ToCharArray()),
        [System.Management.Automation.Host.PSHostRawUserInterface] $rawUI = (Get-Host).UI.RawUI,
        [ConsoleColor]$bgColor = $rawUI.BackgroundColor,
        [ConsoleColor]$fgColor = $rawUI.ForegroundColor,
        [System.Management.Automation.Host.Coordinates]$curPos = $rawUI.Get_CursorPosition(),
        [Int32]$startX = $curPos.X,
        [Int32]$maxX = $rawUI.WindowSize.Width,
        [Switch]$trails
    )
 
    begin {
        $trailCell =    $rawUI.NewBufferCellArray(@($spinChars[-2]), $fgColor, $bgColor);
        $blankCell =    $rawUI.NewBufferCellArray(@($spinChars[-1]), $fgColor, $bgColor);
 
        $spinCells =    $spinChars[0..($spinChars.Count - 3)];
 
        for ($i=0; $i -lt ($spinCells.Count); ++$i) {
            $spinCells[$i] = $rawUI.NewBufferCellArray(@($spinCells[$i]), $fgColor, $bgColor)
        }
 
        $charNdx = 0;
    }
 
    process {
        if ($charNdx -lt $spinCells.Count){
            $rawUI.SetBufferContents($curPos, $spinCells[$charNdx++]);                                     
        }
        else {
            $charNdx = 0
            $rawUI.SetBufferContents($curPos, $trailCell)
            if ($trails) {
                if (++$curPos.X -gt $maxX) {
                    do {
                        --$curPos.X;     $rawUI.SetBufferContents($curPos, $blankCell)   
                    } until ($curPos.X -le $startX) 
                }
            }
        }
 
        Start-Sleep -milliseconds $msWait
        $_
    }
 
    end {
        do {
            $rawUI.SetBufferContents($curPos, $blankCell);     
        }
        until (--$curPos.X -le $startX)
    }
}