From 06377f41aad9b10c62551c02ea21cd933e2bc530 Mon Sep 17 00:00:00 2001 From: Kevin Lin Date: Sun, 28 Jan 2024 06:29:13 -0800 Subject: [PATCH] httputil: Avoid displaying percentage in download progress when content length is not defined --- httputil/progress/progress.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/httputil/progress/progress.go b/httputil/progress/progress.go index 2df66127..ece3d930 100644 --- a/httputil/progress/progress.go +++ b/httputil/progress/progress.go @@ -85,11 +85,19 @@ func (p *progress) Write(buf []byte) (int, error) { // Writes the current download progress to stdout. func (p *progress) ShowProgress() { - message := fmt.Sprintf("%s: %s out of %s (%s)", - p.header, - formatMb(p.current), - formatMb(p.total), - formatPercentage(p.current, p.total)) + var message string + + if p.total > 0 { + message = fmt.Sprintf("%s: %s out of %s (%s)", + p.header, + formatMb(p.current), + formatMb(p.total), + formatPercentage(p.current, p.total)) + } else { + message = fmt.Sprintf("%s: %s", + p.header, + formatMb(p.current)) + } if message == p.lastMessage { return