Skip to content

Commit 68823e3

Browse files
fix(cli): fit PowerShell download progress to the terminal
1 parent 1c9bd64 commit 68823e3

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

‎scripts/install.ps1‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,20 @@ function Step([string] $message) {
4646
}
4747
function Draw-Download([long] $bytes, [long] $total) {
4848
if (-not $interactive) { return }
49-
if ($total -gt 0) {
49+
$columns = [Console]::WindowWidth
50+
if ($columns -le 0) { $columns = 80 }
51+
if ($total -gt 0 -and $columns -ge 9) {
5052
$percent = [Math]::Min(100, [Math]::Floor($bytes * 100.0 / $total))
51-
$filled = [int][Math]::Floor($percent * 32 / 100)
53+
$width = [Math]::Min(32, $columns - 8)
54+
$filled = [int][Math]::Floor($percent * $width / 100)
5255
$bar = ([string][char]0x25A0) * $filled
53-
$rest = ([string][char]0x00B7) * (32 - $filled)
54-
[Console]::Error.Write(("`r$esc[2K $accent$bar$reset$muted$rest$reset {0,3}% $muted{1:F1} / {2:F1} MB$reset" -f $percent, ($bytes / 1MB), ($total / 1MB)))
56+
$rest = ([string][char]0x00B7) * ($width - $filled)
57+
$sizes = (" {0:F1} / {1:F1} MB" -f ($bytes / 1MB), ($total / 1MB))
58+
if ($width + 7 + $sizes.Length -ge $columns) { $sizes = "" }
59+
[Console]::Error.Write(("`r$esc[2K $accent$bar$reset$muted$rest$reset {0,3}%" -f $percent) + "$muted$sizes$reset")
5560
} else {
56-
[Console]::Error.Write(("`r$esc[2K ${muted}Downloading$reset {0:F1} MB" -f ($bytes / 1MB)))
61+
$line = if ($columns -ge 32) { " Downloading {0:F1} MB" -f ($bytes / 1MB) } else { " {0:F1} MB" -f ($bytes / 1MB) }
62+
[Console]::Error.Write("`r$esc[2K" + $line.Substring(0, [Math]::Min($line.Length, $columns - 1)))
5763
}
5864
}
5965
function Fetch([string] $uri, [string] $destination, [switch] $progress) {

0 commit comments

Comments
 (0)