Skip to content

kzrnm/Kurukuru-PowerShell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kurukuru-pwsh

PowerShell Gallery Version

PowerShell wrapper of Kurukuru.

demo.mp4

Get Started

PowerShell Gallery.

Install-Module kurukuru-pwsh

Usage

Simple

Start-Kurukuru {
  param($spinner)
  $spinner.Text = "Start"
  Start-Sleep -Seconds 1.5
  $spinner.Text = "Foo"
  Start-Sleep -Seconds 1.5
  $spinner.Text = "End"
}

Many Params

Start-Kurukuru -Text "Initialize" -SucceedText "Success" -Pattern Moon -SymbolFailed ([Kurukuru.SymbolDefinition]::new("🌑", "O")) {
    param([Kurukuru.Spinner]$spinner)
    if ((([datetime]::Now.Hour + 18) % 24) -gt 12) {
        $spinner.SymbolSucceed = [Kurukuru.SymbolDefinition]::new("🌕️", "O")
    }
    else {
        $spinner.SymbolSucceed = [Kurukuru.SymbolDefinition]::new("🌅", "O")
    }
} {
    param([Kurukuru.Spinner]$spinner)
    Start-Sleep -Seconds 1.5
    $spinner.Text = "Foo"
    Start-Sleep -Seconds 1.5
}

Custom pattern

Start-Kurukuru -Pattern ([Kurukuru.Pattern]::new(@("_", ""), 150)) {
  param($spinner)
  $spinner.Text = "Start"
  Start-Sleep -Seconds 1.5
  $spinner.Text = "Foo"
  Start-Sleep -Seconds 1.5
  $spinner.Text = "End"
}

Parallel

1..20 | ForEach-Object { 
    $i = $_
    New-Spinner -Text "Start: $i and Wait: 300 ms" -Pattern (Get-Random (Get-KurukuruPattern)) -SucceedText "Finish:$i" -FailedText "Failed:$i" -ScriptBlock ({
            param([Kurukuru.Spinner]$s)
            $s.Text = "Starting: $i"
            if ($i % 2 -eq 0) {
                $waitMills = 250 * $i 
            }
            else {
                $waitMills = 500 + 100 * $i
            }
            Start-Sleep -Milliseconds 300
            $s.Text = "Running: $i Wait: $waitMills ms"
            Start-Sleep -Milliseconds $waitMills
            if ($i -eq 2) {
                throw "Error 2"
            }

            $s.Text = "Closing: $i"
        }).GetNewClosure()
} | Start-Kurukuru

Start-Sleep

function Start-KurukuruSleep {
    [CmdletBinding(DefaultParameterSetName = 'Seconds')]
    param (
        [Parameter(ParameterSetName = 'FromTimeSpan', Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [Alias('ts')]
        [timespan]
        $Duration,
        [Parameter(ParameterSetName = 'Milliseconds', Mandatory, ValueFromPipelineByPropertyName)]
        [Alias('ms')]
        [int]
        $Milliseconds,
        [Parameter(ParameterSetName = 'Seconds', Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [double]
        $Seconds,
        $Pattern = $null
    )
    
    switch ($PSCmdlet.ParameterSetName) {
        'Milliseconds' { 
            $Duration = [timespan]::FromMilliseconds($Milliseconds)
        }
        'Seconds' {
            $Duration = [timespan]::FromSeconds($Seconds)
        }
    }
    
    $until = [datetime]::Now.Add($Duration)
    $untilText = "Waiting until $($until.ToString())..."
    Start-Kurukuru -Pattern $Pattern -Text $untilText -SucceedText 'Finish.' {
        param($s)
    
        try {
            do {
                $remaining = $until - (Get-Date)
                $s.Text = "$untilText Ramaining $remaining"
                Start-Sleep -Milliseconds 300
            }while ($remaining -gt 0) 
        }
        catch {
        }
    }
}

Register-ArgumentCompleter -CommandName Start-KurukuruSleep -ParameterName Pattern -ScriptBlock {
    param(
        $commandName,
        $parameterName,
        $wordToComplete,
        $commandAst,
        $fakeBoundParameter
    )
    foreach ($p in ($KurukuruPatterns)) {
        if ($p.Name.ToLower().StartsWith($wordToComplete.ToLower())) {
            [System.Management.Automation.CompletionResult]::new(
                $p.Name,
                $p.Name,
                [System.Management.Automation.CompletionResultType]::ParameterValue, 
                "$($p.Frames) Interval=$($p.Interval)"
            )
        }
    }
}