Created
February 23, 2023 17:13
-
-
Save daisyUniverse/6de2f4cfa19b9a2d5805f8ace9ddee5a to your computer and use it in GitHub Desktop.
Animate-Job Powershell Module
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
# Animate Job | |
# Show a ticker animation on any long winded jobs to it's not just a blank screen | |
# Pass args to this as $ArgumentList and access them in the job via $args[] | |
# Robin Universe [X] | |
# 02 . 23 . 23 | |
# ANIMATIONS | |
Function anim { # Run one animation cycle | |
param ( | |
[String[]] $Animation, | |
[String] $Message = "", | |
[String] $FGColor = "White", | |
[String] $BGColor = "Black", | |
[String] $FGMColor = "White", | |
[String] $BGMColor = "Black", | |
[Bool] $NewLine = $false, | |
[Int] $Delay = 30 | |
) | |
foreach ($Frame in $Animation){ | |
Write-Host "`r$frame" -NoNewline -ForegroundColor $FGColor -BackgroundColor $BGColor | |
if ($Message -ne ""){ Write-Host $Message -NoNewline -ForegroundColor $FGMColor -BackgroundColor $BGMColor } | |
Start-Sleep -Milliseconds $Delay | |
} | |
if ($NewLine -eq $true){ | |
Write-Host "`n" -NoNewline | |
} | |
} | |
$ping_anim = | |
" [. ] ", | |
" [.. ] ", | |
" [... ] ", | |
" [ ... ] ", | |
" [ ... ] ", | |
" [ ... ] ", | |
" [ ... ] ", | |
" [ ...] ", | |
" [ ..] " | |
$complete_anim = | |
" [ COMPLETE ] ", | |
" [ COMPLETE ] ", | |
" [ ] ", | |
" [ ] ", | |
" [ COMPLETE ] ", | |
" [ COMPLETE ] " | |
$failed_anim = | |
" [ XXXXXX ] ", | |
" [ XXXXXX ] ", | |
" [ ] ", | |
" [ ] ", | |
" [ XXXXXX ] ", | |
" [ XXXXXX ] " | |
function Animate-Job { # Main Function | |
param ( | |
[scriptblock]$command, | |
[string]$message, | |
$ArgumentList | |
) | |
$win = $true | |
$job = Start-Job -ScriptBlock $command -ArgumentList $ArgumentList # Start a background job with paramters and Script Block | |
while ($true) { # Wait to see if the job fails or not while running the ping animation | |
anim -Animation $ping_anim -FGColor Magenta -Message "Running $message..." -Delay 30 | |
if ( $job.State -eq "Completed" ) { | |
$testResults = $job | Receive-Job | |
$win = $true | |
break | |
} elseif ( $job.State -eq "Failed" ) { | |
$testResults = $job | Receive-Job | |
$win = $false | |
break | |
} | |
} | |
if ($win = $true) { | |
anim -Animation $complete_anim -FGColor Green -Message "$message Completed! " -Delay 60 -NewLine $true | |
} else { | |
anim -Animation $failed_anim -FGColor Red -Message "$message Failed! " -Delay 60 -NewLine $true | |
} | |
return $testResults | |
} | |
Set-Alias anime Animate-Job | |
Export-ModuleMember -Function Animate-Job -Alias anime |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment