From 01d9b95d4e066c79617435787a77615f52af49b8 Mon Sep 17 00:00:00 2001 From: Simon Hartcher Date: Wed, 22 Mar 2017 20:26:14 +1100 Subject: [PATCH] Throttle progress bar output - Fixes #1389 --- lib/install.ps1 | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/install.ps1 b/lib/install.ps1 index 8ec8c44450..ae59a96017 100644 --- a/lib/install.ps1 +++ b/lib/install.ps1 @@ -164,8 +164,20 @@ function dl($url, $to, $cookies, $progress) { if ($progress -and ($total -gt 0)) { [console]::CursorVisible = $false - function dl_onProgress($read) { - dl_progress $read $total $url + function dl_onProgress($read, $last) { + # get datetime now + $now = [System.DateTime]::Now; + # subtract from last datetime + $delta = $now - $last; + + # output progress only every 100ms + if ($delta.TotalMilliseconds -gt 100) { + dl_progress $read $total $url + + return $now; + } + + return $last; } } else { write-host "Downloading $url ($(filesize $total))..." @@ -180,13 +192,16 @@ function dl($url, $to, $cookies, $progress) { $buffer = new-object byte[] 2048 $totalRead = 0 - dl_onProgress $totalRead + # print starting progress + $last = dl_onProgress $totalRead ([DateTime]::MinValue) while(($read = $s.read($buffer, 0, $buffer.length)) -gt 0) { $fs.write($buffer, 0, $read) $totalRead += $read - dl_onProgress $totalRead + $last = dl_onProgress $totalRead $last } + # make sure we print ending progress + dl_onProgress $totalRead ([DateTime]::MinValue) } finally { if ($progress) { [console]::CursorVisible = $true @@ -254,9 +269,9 @@ function dl_progress_output($url, $read, $total, $console) { function dl_progress($read, $total, $url) { $console = $host.UI.RawUI; - $left = $console.CursorPosition.X; - $top = $console.CursorPosition.Y; - $width = $console.BufferSize.Width; + $left = $console.CursorPosition.X; + $top = $console.CursorPosition.Y; + $width = $console.BufferSize.Width; if($read -eq 0) { $maxOutputLength = $(dl_progress_output $url 100 $total $console).length