Skip to content

Commit 992f678

Browse files
Migrate REST API progress status to use Write-Progress (#167)
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
1 parent d68503f commit 992f678

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

Helpers.ps1

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,11 @@ function Wait-JobWithAnimation
4848
$animationFrames = '|','/','-','\'
4949
$framesPerSecond = 9
5050

51-
# We'll wrap the description (if provided) in brackets for display purposes.
52-
if ($Description -ne "")
53-
{
54-
$Description = "[$Description]"
55-
}
56-
51+
$progressId = 1
5752
$iteration = 0
53+
[int] $seconds = 0
54+
$secondWord = 'seconds'
55+
5856
while ($runningJobs.Count -gt 0)
5957
{
6058
# We'll run into issues if we try to modify the same collection we're iterating over
@@ -92,24 +90,45 @@ function Wait-JobWithAnimation
9290
$runingJobs.Clear()
9391
}
9492

95-
Write-InteractiveHost "`r$($animationFrames[$($iteration % $($animationFrames.Length))]) Elapsed: $([int]($iteration / $framesPerSecond)) second(s) $Description" -NoNewline -f Yellow
96-
Start-Sleep -Milliseconds ([int](1000/$framesPerSecond))
93+
$seconds = [int]($iteration / $framesPerSecond)
94+
$secondWord = 'seconds'
95+
if (1 -eq $seconds)
96+
{
97+
$secondWord = 'second'
98+
}
99+
100+
$animationFrameNumber = $iteration % $($animationFrames.Length)
101+
$progressParams = @{
102+
'Activity' = $Description
103+
'Status' = "$($animationFrames[$animationFrameNumber]) Elapsed: $seconds $secondWord"
104+
'PercentComplete' = $(($animationFrameNumber / $animationFrames.Length) * 100)
105+
'Id' = $progressId
106+
}
107+
108+
Write-Progress @progressParams
109+
Start-Sleep -Milliseconds ([int](1000 / $framesPerSecond))
97110
$iteration++
98111
}
99112

113+
# We'll wrap the description (if provided) in brackets for display purposes.
114+
if (-not [string]::IsNullOrWhiteSpace($Description))
115+
{
116+
$Description = "[$Description]"
117+
}
118+
100119
if ($allJobsCompleted)
101120
{
102-
Write-InteractiveHost "`rDONE - Operation took $([int]($iteration / $framesPerSecond)) second(s) $Description" -NoNewline -f Green
121+
Write-InteractiveHost "`rDONE - Operation took $seconds $secondWord $Description" -NoNewline -f Green
103122

104123
# 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.
105-
Write-Log -Message "DONE - Operation took $([int]($iteration / $framesPerSecond)) second(s) $Description" -Level Verbose -Verbose:$false
124+
Write-Log -Message "DONE - Operation took $seconds $secondWord $Description" -Level Verbose -Verbose:$false
106125
}
107126
else
108127
{
109-
Write-InteractiveHost "`rDONE (FAILED) - Operation took $([int]($iteration / $framesPerSecond)) second(s) $Description" -NoNewline -f Red
128+
Write-InteractiveHost "`rDONE (FAILED) - Operation took $seconds $secondWord $Description" -NoNewline -f Red
110129

111130
# 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.
112-
Write-Log -Message "DONE (FAILED) - Operation took $([int]($iteration / $framesPerSecond)) second(s) $Description" -Level Verbose -Verbose:$false
131+
Write-Log -Message "DONE (FAILED) - Operation took $seconds $secondWord $Description" -Level Verbose -Verbose:$false
113132
}
114133

115134
Write-InteractiveHost ""

0 commit comments

Comments
 (0)