Skip to content

Commit

Permalink
Throttle progress bar output
Browse files Browse the repository at this point in the history
  • Loading branch information
deevus committed Mar 22, 2017
1 parent 29f69b2 commit 01d9b95
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions lib/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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))..."
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 01d9b95

Please sign in to comment.