Skip to content

Commit

Permalink
fix: avoid overflow in progress bar percentage
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah committed Nov 7, 2023
1 parent 92f9c8b commit 78e7ba5
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions cmd/oras/internal/display/progress/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ func (s *status) String(width int) (string, string) {
// todo: doesn't support multiline prompt
total := uint64(s.descriptor.Size)
var percent float64
if s.offset >= 0 {
percent = float64(s.offset) / float64(total)
}

name := s.descriptor.Annotations["org.opencontainers.image.title"]
if name == "" {
Expand All @@ -112,10 +109,14 @@ func (s *status) String(width int) (string, string) {
// mark(1) bar(22) speed(8) action(<=11) name(<=126) size_per_size(<=13) percent(8) time(>=6)
// └─ digest(72)
var offset string
switch percent {
case 1: // 100%, show exact size
switch s.done {
case true: // 100%, show exact size
offset = fmt.Sprint(s.total.Size)
percent = 1
default: // 0% ~ 99%, show 2-digit precision
if s.offset >= 0 {
percent = float64(s.offset) / float64(total)
}
offset = fmt.Sprintf("%.2f", humanize.RoundTo(s.total.Size*percent))
}
right := fmt.Sprintf(" %s/%s %6.2f%% %6s", offset, s.total, percent*100, s.durationString())
Expand Down

0 comments on commit 78e7ba5

Please sign in to comment.