-
-
Save sionta/25e6bb07cca76b76f964acb61a6bbdcf to your computer and use it in GitHub Desktop.
PowerShell function to create a processing animation for long running scriptblocks.
This file contains 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
function ProcessingAnimation($scriptBlock) { | |
$cursorTop = [Console]::CursorTop | |
try { | |
[Console]::CursorVisible = $false | |
$counter = 0 | |
$frames = '|', '/', '-', '\' | |
$jobName = Start-Job -ScriptBlock $scriptBlock | |
while($jobName.JobStateInfo.State -eq "Running") { | |
$frame = $frames[$counter % $frames.Length] | |
Write-Host "$frame" -NoNewLine | |
[Console]::SetCursorPosition(0, $cursorTop) | |
$counter += 1 | |
Start-Sleep -Milliseconds 125 | |
} | |
# Only needed if you use a multiline frames | |
Write-Host ($frames[0] -replace '[^\s+]', ' ') | |
} | |
finally { | |
[Console]::SetCursorPosition(0, $cursorTop) | |
[Console]::CursorVisible = $true | |
} | |
} | |
# Example: | |
ProcessingAnimation { Start-Sleep 5 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment