Skip to content

Instantly share code, notes, and snippets.

@WillemRB
Last active November 15, 2024 11:14
Show Gist options
  • Save WillemRB/5eb18301462ed6eb23bf to your computer and use it in GitHub Desktop.
Save WillemRB/5eb18301462ed6eb23bf 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 }
@Vagrantin
Copy link

Clean and efficient thank you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment