Skip to content

Commit

Permalink
Migrate REST API progress status to use Write-Progress (#167)
Browse files Browse the repository at this point in the history
When this module was originally written, it used a special feature
of Write-Host in order to re-write the previously written line, in
order to show the user an indeterminate waiting indicator while the
module awaited a response from the server for a REST API request.

Unfortunately, this doesn't work well with all PowerShell hosts
(PowerShell ISE is one notable example).  This approach had been taken
originally as it had seemed odd to use Write-Progress (which displays
an absolute progress percentage) for something where we didn't absolutely
know the final duration.

This change migrates the module over to use Write-Progress, and simply
loops through the displayed percentage completed in the same manor that
it loops through and shows an animation.

This method should achieve the same desired goal (letting users know
that something is happening and that they just need to wait a bit
longer) in a more standards-approved way.

Resolves #119
  • Loading branch information
HowardWolosky authored May 12, 2020
1 parent d68503f commit 992f678
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,11 @@ function Wait-JobWithAnimation
$animationFrames = '|','/','-','\'
$framesPerSecond = 9

# We'll wrap the description (if provided) in brackets for display purposes.
if ($Description -ne "")
{
$Description = "[$Description]"
}

$progressId = 1
$iteration = 0
[int] $seconds = 0
$secondWord = 'seconds'

while ($runningJobs.Count -gt 0)
{
# We'll run into issues if we try to modify the same collection we're iterating over
Expand Down Expand Up @@ -92,24 +90,45 @@ function Wait-JobWithAnimation
$runingJobs.Clear()
}

Write-InteractiveHost "`r$($animationFrames[$($iteration % $($animationFrames.Length))]) Elapsed: $([int]($iteration / $framesPerSecond)) second(s) $Description" -NoNewline -f Yellow
Start-Sleep -Milliseconds ([int](1000/$framesPerSecond))
$seconds = [int]($iteration / $framesPerSecond)
$secondWord = 'seconds'
if (1 -eq $seconds)
{
$secondWord = 'second'
}

$animationFrameNumber = $iteration % $($animationFrames.Length)
$progressParams = @{
'Activity' = $Description
'Status' = "$($animationFrames[$animationFrameNumber]) Elapsed: $seconds $secondWord"
'PercentComplete' = $(($animationFrameNumber / $animationFrames.Length) * 100)
'Id' = $progressId
}

Write-Progress @progressParams
Start-Sleep -Milliseconds ([int](1000 / $framesPerSecond))
$iteration++
}

# We'll wrap the description (if provided) in brackets for display purposes.
if (-not [string]::IsNullOrWhiteSpace($Description))
{
$Description = "[$Description]"
}

if ($allJobsCompleted)
{
Write-InteractiveHost "`rDONE - Operation took $([int]($iteration / $framesPerSecond)) second(s) $Description" -NoNewline -f Green
Write-InteractiveHost "`rDONE - Operation took $seconds $secondWord $Description" -NoNewline -f Green

# We forcibly set Verbose to false here since we don't need it printed to the screen, since we just did above -- we just need to log it.
Write-Log -Message "DONE - Operation took $([int]($iteration / $framesPerSecond)) second(s) $Description" -Level Verbose -Verbose:$false
Write-Log -Message "DONE - Operation took $seconds $secondWord $Description" -Level Verbose -Verbose:$false
}
else
{
Write-InteractiveHost "`rDONE (FAILED) - Operation took $([int]($iteration / $framesPerSecond)) second(s) $Description" -NoNewline -f Red
Write-InteractiveHost "`rDONE (FAILED) - Operation took $seconds $secondWord $Description" -NoNewline -f Red

# We forcibly set Verbose to false here since we don't need it printed to the screen, since we just did above -- we just need to log it.
Write-Log -Message "DONE (FAILED) - Operation took $([int]($iteration / $framesPerSecond)) second(s) $Description" -Level Verbose -Verbose:$false
Write-Log -Message "DONE (FAILED) - Operation took $seconds $secondWord $Description" -Level Verbose -Verbose:$false
}

Write-InteractiveHost ""
Expand Down

0 comments on commit 992f678

Please sign in to comment.