mirror of
				https://github.com/yeongpin/cursor-free-vip.git
				synced 2025-10-31 20:42:36 +08:00 
			
		
		
		
	修復 progress bar 下載狀態不對稱 install.ps1
This commit is contained in:
		| @@ -124,76 +124,62 @@ function Install-CursorFreeVIP { | |||||||
|         } |         } | ||||||
|          |          | ||||||
|         Write-Styled "No existing installation file found, starting download..." -Color $Theme.Primary -Prefix "Download" |         Write-Styled "No existing installation file found, starting download..." -Color $Theme.Primary -Prefix "Download" | ||||||
|          |  | ||||||
|         # Create WebClient and add progress event |  | ||||||
|         $webClient = New-Object System.Net.WebClient |  | ||||||
|         $webClient.Headers.Add("User-Agent", "PowerShell Script") |  | ||||||
|  |  | ||||||
|         # Define progress variables |         # Use HttpWebRequest for chunked download with real-time progress bar | ||||||
|         $Global:downloadedBytes = 0 |         $url = $asset.browser_download_url | ||||||
|         $Global:totalBytes = 0 |         $outputFile = $downloadPath | ||||||
|         $Global:lastProgress = 0 |         Write-Styled "Downloading from: $url" -Color $Theme.Info -Prefix "URL" | ||||||
|         $Global:lastBytes = 0 |         Write-Styled "Saving to: $outputFile" -Color $Theme.Info -Prefix "Path" | ||||||
|         $Global:lastTime = Get-Date |  | ||||||
|  |  | ||||||
|         # Download progress event |         $request = [System.Net.HttpWebRequest]::Create($url) | ||||||
|         $eventId = [guid]::NewGuid() |         $request.UserAgent = "PowerShell Script" | ||||||
|         Register-ObjectEvent -InputObject $webClient -EventName DownloadProgressChanged -Action { |         $response = $request.GetResponse() | ||||||
|             $Global:downloadedBytes = $EventArgs.BytesReceived |         $totalLength = $response.ContentLength | ||||||
|             $Global:totalBytes = $EventArgs.TotalBytesToReceive |         $responseStream = $response.GetResponseStream() | ||||||
|             $progress = [math]::Round(($Global:downloadedBytes / $Global:totalBytes) * 100, 1) |         $fileStream = [System.IO.File]::OpenWrite($outputFile) | ||||||
|              |         $buffer = New-Object byte[] 8192 | ||||||
|             # Only update display when progress changes by more than 1% |         $bytesRead = 0 | ||||||
|             if ($progress -gt $Global:lastProgress + 1) { |         $totalRead = 0 | ||||||
|                 $Global:lastProgress = $progress |         $lastProgress = -1 | ||||||
|                 $downloadedMB = [math]::Round($Global:downloadedBytes / 1MB, 2) |         $startTime = Get-Date | ||||||
|                 $totalMB = [math]::Round($Global:totalBytes / 1MB, 2) |         try { | ||||||
|                  |             do { | ||||||
|                 # Calculate download speed |                 $bytesRead = $responseStream.Read($buffer, 0, $buffer.Length) | ||||||
|                 $currentTime = Get-Date |                 if ($bytesRead -gt 0) { | ||||||
|                 $timeSpan = ($currentTime - $Global:lastTime).TotalSeconds |                     $fileStream.Write($buffer, 0, $bytesRead) | ||||||
|                 if ($timeSpan -gt 0) { |                     $totalRead += $bytesRead | ||||||
|                     $bytesChange = $Global:downloadedBytes - $Global:lastBytes |                     $progress = [math]::Round(($totalRead / $totalLength) * 100, 1) | ||||||
|                     $speed = $bytesChange / $timeSpan |                     if ($progress -ne $lastProgress) { | ||||||
|                      |                         $elapsed = (Get-Date) - $startTime | ||||||
|                     # Choose appropriate unit based on speed |                         $speed = if ($elapsed.TotalSeconds -gt 0) { $totalRead / $elapsed.TotalSeconds } else { 0 } | ||||||
|                     $speedDisplay = if ($speed -gt 1MB) { |                         $speedDisplay = if ($speed -gt 1MB) { | ||||||
|                         "$([math]::Round($speed / 1MB, 2)) MB/s" |                             "{0:N2} MB/s" -f ($speed / 1MB) | ||||||
|                     } elseif ($speed -gt 1KB) { |                         } elseif ($speed -gt 1KB) { | ||||||
|                         "$([math]::Round($speed / 1KB, 2)) KB/s" |                             "{0:N2} KB/s" -f ($speed / 1KB) | ||||||
|                     } else { |                         } else { | ||||||
|                         "$([math]::Round($speed, 2)) B/s" |                             "{0:N2} B/s" -f $speed | ||||||
|  |                         } | ||||||
|  |                         $downloadedMB = [math]::Round($totalRead / 1MB, 2) | ||||||
|  |                         $totalMB = [math]::Round($totalLength / 1MB, 2) | ||||||
|  |                         Write-Progress -Activity "Downloading CursorFreeVIP" -Status "$downloadedMB MB / $totalMB MB ($progress%) - $speedDisplay" -PercentComplete $progress | ||||||
|  |                         $lastProgress = $progress | ||||||
|                     } |                     } | ||||||
|                      |  | ||||||
|                     Write-Host "`rDownloading: $downloadedMB MB / $totalMB MB ($progress%) - $speedDisplay" -NoNewline -ForegroundColor Cyan |  | ||||||
|                      |  | ||||||
|                     # Update last data |  | ||||||
|                     $Global:lastBytes = $Global:downloadedBytes |  | ||||||
|                     $Global:lastTime = $currentTime |  | ||||||
|                 } |                 } | ||||||
|             } |             } while ($bytesRead -gt 0) | ||||||
|         } | Out-Null |         } finally { | ||||||
|  |             $fileStream.Close() | ||||||
|         # Download completed event |             $responseStream.Close() | ||||||
|         Register-ObjectEvent -InputObject $webClient -EventName DownloadFileCompleted -Action { |             $response.Close() | ||||||
|             Write-Host "`r" -NoNewline |  | ||||||
|             Write-Styled "Download completed!" -Color $Theme.Success -Prefix "Complete" |  | ||||||
|             Unregister-Event -SourceIdentifier $eventId |  | ||||||
|         } | Out-Null |  | ||||||
|  |  | ||||||
|         # Start download |  | ||||||
|         $webClient.DownloadFileAsync([Uri]$asset.browser_download_url, $downloadPath) |  | ||||||
|  |  | ||||||
|         # Wait for download to complete |  | ||||||
|         while ($webClient.IsBusy) { |  | ||||||
|             Start-Sleep -Milliseconds 100 |  | ||||||
|         } |         } | ||||||
|          |         Write-Progress -Activity "Downloading CursorFreeVIP" -Completed | ||||||
|         Write-Styled "File location: $downloadPath" -Color $Theme.Info -Prefix "Location" |         # Check file exists and is not zero size | ||||||
|  |         if (!(Test-Path $outputFile) -or ((Get-Item $outputFile).Length -eq 0)) { | ||||||
|  |             throw "Download failed or file is empty." | ||||||
|  |         } | ||||||
|  |         Write-Styled "Download completed!" -Color $Theme.Success -Prefix "Complete" | ||||||
|  |         Write-Styled "File location: $outputFile" -Color $Theme.Info -Prefix "Location" | ||||||
|         Write-Styled "Starting program..." -Color $Theme.Primary -Prefix "Launch" |         Write-Styled "Starting program..." -Color $Theme.Primary -Prefix "Launch" | ||||||
|          |         Start-Process $outputFile | ||||||
|         # Run program |  | ||||||
|         Start-Process $downloadPath |  | ||||||
|     } |     } | ||||||
|     catch { |     catch { | ||||||
|         Write-Styled $_.Exception.Message -Color $Theme.Error -Prefix "Error" |         Write-Styled $_.Exception.Message -Color $Theme.Error -Prefix "Error" | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 LoveDoLove
					LoveDoLove