Last active
April 26, 2022 20:54
-
-
Save TechDufus/c4b5dc80f83afdb9861ff0bea0ec18e7 to your computer and use it in GitHub Desktop.
Display a spinner in the console to simulate scrip activity.
This file contains hidden or 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
#Region PREP | |
#Region Formatting | |
$script:YELLOW = "$($PSStyle.Foreground.Yellow)" | |
$script:RED = "$($PSStyle.Foreground.Red)" | |
$script:GREEN = "$($PSStyle.Foreground.Green)" | |
$script:CYAN = "$($PSStyle.Foreground.Cyan)" | |
$script:RESET = "$($PSStyle.Reset)" | |
$script:ARROW = "${CYAN}$([char]9654)${RESET}" | |
$script:CHECK_MARK = "${GREEN}$([char]8730)${RESET}" | |
$script:RIGHT_ANGLE = "${GREEN}$([char]8735)${RESET}" | |
$script:PIN = "$(([char]55357) + ([char]56524))" | |
$script:WARNING = "$(([char]55357) + ([char]57000))" | |
$script:YES_NO = "${YELLOW}[${GREEN}Y${YELLOW}/${RED}N${YELLOW}]${RESET}" | |
$script:SPACE = "$([char]32)" | |
$script:SPINNER = @( | |
"${GREEN}$([char]10251)${RESET}", | |
"${GREEN}$([char]10265)${RESET}", | |
"${GREEN}$([char]10297)${RESET}", | |
"${GREEN}$([char]10296)${RESET}", | |
"${GREEN}$([char]10300)${RESET}", | |
"${GREEN}$([char]10292)${RESET}", | |
"${GREEN}$([char]10278)${RESET}", | |
"${GREEN}$([char]10279)${RESET}", | |
"${GREEN}$([char]10247)${RESET}", | |
"${GREEN}$([char]10255)${RESET}" | |
) | |
#EndRegion Formatting | |
#Region Write-Spinner | |
Function Write-Spinner() { | |
$script:SPINNER | Foreach-Object { | |
Write-Host "`b`b$_ " -NoNewline | |
Start-Sleep -m 50 | |
} | |
} | |
#EndRegion Write-Spinner | |
#Region Write-BlankLine | |
Function Write-BlankLine() { | |
Write-Host "" | |
} | |
#EndRegion Write-BlankLine | |
#EndRegion PREP | |
#Region INCORRECT VERSION | |
Function Test-SlowPath() { | |
Param( | |
$Path | |
) | |
Start-Sleep -Seconds 2 | |
If (Test-Path $Path) { | |
return $true | |
} | |
return $false | |
} | |
Function Get-MyJoblessThing() { | |
Try { | |
$PATH = 'C:\Temp\CUSTOM_FILE_PATH.txt' | |
Write-Host "${ARROW} ${CYAN}Starting Jobless Thing${RESET}" | |
Write-Host "${ARROW} ${CYAN}Waiting for ${YELLOW}$PATH${CYAN} to exist... ${RESET}" -NoNewLine | |
Do { | |
Write-Spinner | |
} Until (Test-SlowPath $PATH) | |
Write-Host "`b`b${CHECK_MARK}" | |
Write-Host "${ARROW} ${GREEN}Jobless Thing completed!${RESET}" | |
} | |
Catch { | |
Write-Error $_ | |
} | |
} | |
#EndRegion INCORRECT VERSION | |
#Region CORRECT VERSION | |
Function Get-MyCustomJobThing() { | |
Try { | |
$MyJob = { | |
Param( | |
[Parameter(Mandatory=$true)] | |
$Path | |
) | |
Do { | |
Start-Sleep 1 | |
} Until (Test-Path $Path) | |
return $true | |
} | |
$Path = 'C:\Temp\CUSTOM_FILE_PATH.txt' | |
Write-Host "${ARROW} ${CYAN}Starting Job${RESET}" | |
$Job = Start-Job -ScriptBlock $MyJob -ArgumentList $Path | |
Write-Host "${ARROW} ${CYAN}Job ID ${YELLOW}$($Job.ID)${CYAN} started...${RESET}" | |
Write-Host "${ARROW} ${CYAN}Waiting for ${YELLOW}$PATH${CYAN} to exist... ${RESET}" -NoNewLine | |
Do { | |
Write-Spinner | |
} Until ($(Get-Job -ID $Job.ID).State -eq "Completed") | |
Write-Host "`b`b${CHECK_MARK}" | |
Write-Host "${ARROW} ${GREEN}Job completed!${RESET}" | |
} | |
Catch { | |
Write-Error $_ | |
} | |
Finally { | |
Write-Host "${ARROW} ${CYAN}Removing job ${YELLOW}$($Job.ID)${CYAN}...${RESET}" | |
Remove-Job -Job $Job | |
} | |
} | |
#EndRegion CORRECT VERSION |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment