Skip to content

Instantly share code, notes, and snippets.

@sionta
Forked from WillemRB/ProcessingAnimation
Last active February 3, 2024 23:36
Show Gist options
  • Save sionta/25e6bb07cca76b76f964acb61a6bbdcf to your computer and use it in GitHub Desktop.
Save sionta/25e6bb07cca76b76f964acb61a6bbdcf to your computer and use it in GitHub Desktop.
PowerShell function to create a processing animation for long running scriptblocks.
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